Compare commits
5 Commits
02dd064d99
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b93c2e7e95 | ||
|
|
105b0c514f | ||
|
|
c6dd174376 | ||
|
|
27f6e627d4 | ||
|
|
76f2a43bd0 |
@@ -7,11 +7,10 @@
|
||||
"start": "tsx src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@effect/platform": "^0.93.2",
|
||||
"@effect/platform-node": "^0.101.1",
|
||||
"@money/shared": "workspace:*",
|
||||
"@hono/node-server": "^1.19.5",
|
||||
"@money/shared": "*",
|
||||
"better-auth": "^1.3.27",
|
||||
"effect": "^3.19.4",
|
||||
"hono": "^4.9.12",
|
||||
"plaid": "^39.0.0",
|
||||
"tsx": "^4.20.6"
|
||||
},
|
||||
|
||||
@@ -3,7 +3,7 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { bearer, deviceAuthorization, genericOAuth } from "better-auth/plugins";
|
||||
import { expo } from "@better-auth/expo";
|
||||
import { drizzleSchema } from "@money/shared/db";
|
||||
import { db } from "../db";
|
||||
import { db } from "./db";
|
||||
import { BASE_URL, HOST } from "@money/shared";
|
||||
|
||||
export const auth = betterAuth({
|
||||
@@ -1,23 +0,0 @@
|
||||
import { Effect, Layer } from "effect";
|
||||
import { Authorization } from "./context";
|
||||
import { auth } from "./config";
|
||||
import { AuthorizationError, AuthorizationUnknownError } from "./errors";
|
||||
|
||||
export const make = () =>
|
||||
Effect.gen(function* () {
|
||||
return Authorization.of({
|
||||
use: (fn) =>
|
||||
Effect.gen(function* () {
|
||||
const data = yield* Effect.tryPromise({
|
||||
try: () => fn(auth),
|
||||
catch: (error) =>
|
||||
error instanceof Error
|
||||
? new AuthorizationError({ message: error.message })
|
||||
: new AuthorizationUnknownError(),
|
||||
});
|
||||
return data;
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
export const BetterAuthLive = Layer.scoped(Authorization, make());
|
||||
@@ -1,14 +0,0 @@
|
||||
import { Context, type Effect } from "effect";
|
||||
import type { AuthorizationError, AuthorizationUnknownError } from "./errors";
|
||||
import type { auth } from "./config";
|
||||
|
||||
export class Authorization extends Context.Tag("Authorization")<
|
||||
Authorization,
|
||||
AuthorizationImpl
|
||||
>() {}
|
||||
|
||||
export interface AuthorizationImpl {
|
||||
use: <T>(
|
||||
fn: (client: typeof auth) => Promise<T>,
|
||||
) => Effect.Effect<T, AuthorizationUnknownError | AuthorizationError, never>;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Data } from "effect";
|
||||
|
||||
export class AuthorizationUnknownError extends Data.TaggedError(
|
||||
"AuthClientUnknownError",
|
||||
) {}
|
||||
export class AuthorizationError extends Data.TaggedError("AuthorizationError")<{
|
||||
message: string;
|
||||
}> {}
|
||||
@@ -1,70 +0,0 @@
|
||||
import {
|
||||
HttpApi,
|
||||
HttpApiBuilder,
|
||||
HttpApiEndpoint,
|
||||
HttpApiGroup,
|
||||
HttpLayerRouter,
|
||||
HttpServerRequest,
|
||||
} from "@effect/platform";
|
||||
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
|
||||
import { toNodeHandler } from "better-auth/node";
|
||||
import { Effect, Layer } from "effect";
|
||||
import { AuthorizationError } from "./errors";
|
||||
import { auth } from "./config";
|
||||
|
||||
const authHandler = Effect.gen(function* () {
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
const nodeRequest = NodeHttpServerRequest.toIncomingMessage(request);
|
||||
const nodeResponse = NodeHttpServerRequest.toServerResponse(request);
|
||||
|
||||
nodeResponse.setHeader("Access-Control-Allow-Origin", "http://laptop:8081");
|
||||
nodeResponse.setHeader(
|
||||
"Access-Control-Allow-Methods",
|
||||
"GET, POST, PUT, DELETE, OPTIONS",
|
||||
);
|
||||
nodeResponse.setHeader(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Content-Type, Authorization, B3, traceparent, Cookie",
|
||||
);
|
||||
nodeResponse.setHeader("Access-Control-Max-Age", "600");
|
||||
nodeResponse.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
|
||||
// Handle preflight requests
|
||||
if (nodeRequest.method === "OPTIONS") {
|
||||
nodeResponse.statusCode = 200;
|
||||
nodeResponse.end();
|
||||
|
||||
return;
|
||||
// return nodeResponse;
|
||||
}
|
||||
|
||||
yield* Effect.tryPromise({
|
||||
try: () => toNodeHandler(auth)(nodeRequest, nodeResponse),
|
||||
catch: (error) => {
|
||||
return new AuthorizationError({ message: `${error}` });
|
||||
},
|
||||
});
|
||||
|
||||
// return nodeResponse;
|
||||
});
|
||||
|
||||
export class AuthContractGroup extends HttpApiGroup.make("auth")
|
||||
.add(HttpApiEndpoint.get("get", "/*"))
|
||||
.add(HttpApiEndpoint.post("post", "/*"))
|
||||
.add(HttpApiEndpoint.options("options", "/*"))
|
||||
.prefix("/api/auth") {}
|
||||
|
||||
export class DomainApi extends HttpApi.make("domain").add(AuthContractGroup) {}
|
||||
|
||||
export const Api = HttpApi.make("api").addHttpApi(DomainApi);
|
||||
|
||||
const AuthLive = HttpApiBuilder.group(Api, "auth", (handlers) =>
|
||||
handlers
|
||||
.handle("get", () => authHandler.pipe(Effect.orDie))
|
||||
.handle("post", () => authHandler.pipe(Effect.orDie))
|
||||
.handle("options", () => authHandler.pipe(Effect.orDie)),
|
||||
);
|
||||
|
||||
export const AuthRoute = HttpLayerRouter.addHttpApi(Api).pipe(
|
||||
Layer.provide(AuthLive),
|
||||
);
|
||||
5
apps/api/src/hono.ts
Normal file
5
apps/api/src/hono.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { AuthData } from "@money/shared/auth";
|
||||
import { Hono } from "hono";
|
||||
|
||||
export const getHono = () =>
|
||||
new Hono<{ Variables: { auth: AuthData | null } }>();
|
||||
@@ -1,36 +1,58 @@
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as HttpLayerRouter from "@effect/platform/HttpLayerRouter";
|
||||
import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
|
||||
import { createServer } from "http";
|
||||
import { CorsMiddleware } from "./middleware/cors";
|
||||
import { AuthRoute } from "./auth/handler";
|
||||
import { BetterAuthLive } from "./auth/better-auth";
|
||||
import { WebhookReceiverRoute } from "./webhook";
|
||||
import { ZeroMutateRoute, ZeroQueryRoute } from "./zero/handler";
|
||||
import { serve } from "@hono/node-server";
|
||||
import { authDataSchema } from "@money/shared/auth";
|
||||
import { BASE_URL } from "@money/shared";
|
||||
import { cors } from "hono/cors";
|
||||
import { auth } from "./auth";
|
||||
import { getHono } from "./hono";
|
||||
import { zero } from "./zero";
|
||||
import { webhook } from "./webhook";
|
||||
|
||||
const RootRoute = HttpLayerRouter.add(
|
||||
"GET",
|
||||
"/",
|
||||
Effect.gen(function* () {
|
||||
return HttpServerResponse.text("OK");
|
||||
const app = getHono();
|
||||
|
||||
app.use(
|
||||
"/api/*",
|
||||
cors({
|
||||
origin: ["https://money.koon.us", `${BASE_URL}:8081`],
|
||||
allowMethods: ["POST", "GET", "OPTIONS"],
|
||||
allowHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
|
||||
const AllRoutes = Layer.mergeAll(
|
||||
RootRoute,
|
||||
AuthRoute,
|
||||
ZeroQueryRoute,
|
||||
ZeroMutateRoute,
|
||||
WebhookReceiverRoute,
|
||||
);
|
||||
app.on(["GET", "POST"], "/api/auth/*", (c) => auth.handler(c.req.raw));
|
||||
|
||||
HttpLayerRouter.serve(AllRoutes).pipe(
|
||||
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
|
||||
Layer.provide(BetterAuthLive),
|
||||
Layer.provide(CorsMiddleware.layer),
|
||||
Layer.launch,
|
||||
NodeRuntime.runMain,
|
||||
app.use("*", async (c, next) => {
|
||||
const authHeader = c.req.raw.headers.get("Authorization");
|
||||
const cookie = authHeader?.split("Bearer ")[1];
|
||||
|
||||
const newHeaders = new Headers(c.req.raw.headers);
|
||||
|
||||
if (cookie) {
|
||||
newHeaders.set("Cookie", cookie);
|
||||
}
|
||||
|
||||
const session = await auth.api.getSession({ headers: newHeaders });
|
||||
|
||||
if (!session) {
|
||||
c.set("auth", null);
|
||||
return next();
|
||||
}
|
||||
c.set("auth", authDataSchema.parse(session));
|
||||
return next();
|
||||
});
|
||||
|
||||
app.route("/api/zero", zero);
|
||||
|
||||
app.get("/api", (c) => c.text("OK"));
|
||||
app.get("/api/webhook_receiver", webhook);
|
||||
app.get("/", (c) => c.text("OK"));
|
||||
|
||||
serve(
|
||||
{
|
||||
fetch: app.fetch,
|
||||
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
|
||||
},
|
||||
(info) => {
|
||||
console.log(`Server is running on ${info.address}:${info.port}`);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { HttpLayerRouter, HttpMiddleware } from "@effect/platform";
|
||||
import { BASE_URL } from "@money/shared";
|
||||
|
||||
export const CorsMiddleware = HttpLayerRouter.middleware(
|
||||
HttpMiddleware.cors({
|
||||
allowedOrigins: ["https://money.koon.us", `${BASE_URL}:8081`],
|
||||
allowedMethods: ["POST", "GET", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
@@ -1,61 +0,0 @@
|
||||
import { AuthSchema } from "@money/shared/auth";
|
||||
import { Context, Effect, Schema, Console } from "effect";
|
||||
import { Authorization } from "../auth/context";
|
||||
import { HttpLayerRouter, HttpServerRequest } from "@effect/platform";
|
||||
|
||||
export class CurrentSession extends Context.Tag("CurrentSession")<
|
||||
CurrentSession,
|
||||
{ readonly auth: Schema.Schema.Type<typeof AuthSchema> | null }
|
||||
>() {}
|
||||
|
||||
export const SessionMiddleware = HttpLayerRouter.middleware<{
|
||||
provides: CurrentSession;
|
||||
}>()(
|
||||
Effect.gen(function* () {
|
||||
const auth = yield* Authorization;
|
||||
|
||||
return (httpEffect) =>
|
||||
Effect.gen(function* () {
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
const headers = { ...request.headers };
|
||||
|
||||
const token = yield* HttpServerRequest.schemaHeaders(
|
||||
Schema.Struct({
|
||||
authorization: Schema.optional(Schema.String),
|
||||
}),
|
||||
).pipe(
|
||||
Effect.tap(Console.debug),
|
||||
Effect.flatMap(({ authorization }) =>
|
||||
authorization != undefined
|
||||
? parseAuthorization(authorization)
|
||||
: Effect.succeed(undefined),
|
||||
),
|
||||
);
|
||||
if (token) {
|
||||
headers["cookie"] = token;
|
||||
}
|
||||
|
||||
const session = yield* auth
|
||||
.use((auth) => auth.api.getSession({ headers }))
|
||||
.pipe(
|
||||
Effect.flatMap((s) =>
|
||||
s == null ? Effect.succeed(null) : Schema.decode(AuthSchema)(s),
|
||||
),
|
||||
// Effect.tap((s) => Console.debug("Auth result", s)),
|
||||
);
|
||||
|
||||
return yield* Effect.provideService(httpEffect, CurrentSession, {
|
||||
auth: session,
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const parseAuthorization = (input: string) =>
|
||||
Effect.gen(function* () {
|
||||
const m = /^Bearer\s+(.+)$/.exec(input);
|
||||
if (!m) {
|
||||
return yield* Effect.fail(new Error("Invalid token"));
|
||||
}
|
||||
return m[1];
|
||||
});
|
||||
3
apps/api/src/plaid/sync.ts
Normal file
3
apps/api/src/plaid/sync.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
async function sync() {}
|
||||
|
||||
sync();
|
||||
23
apps/api/src/plaid/tx.ts
Normal file
23
apps/api/src/plaid/tx.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { transaction } from "@money/shared/db";
|
||||
import type { Transaction } from "plaid";
|
||||
import { type InferInsertModel } from "drizzle-orm";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export function transactionFromPlaid(
|
||||
userId: string,
|
||||
tx: Transaction,
|
||||
): InferInsertModel<typeof transaction> {
|
||||
return {
|
||||
id: randomUUID(),
|
||||
user_id: userId,
|
||||
plaid_id: tx.transaction_id,
|
||||
account_id: tx.account_id,
|
||||
name: tx.name,
|
||||
amount: tx.amount as any,
|
||||
datetime: tx.datetime ? new Date(tx.datetime) : new Date(tx.date),
|
||||
authorized_datetime: tx.authorized_datetime
|
||||
? new Date(tx.authorized_datetime)
|
||||
: undefined,
|
||||
json: JSON.stringify(tx),
|
||||
};
|
||||
}
|
||||
@@ -1,21 +1,11 @@
|
||||
import {
|
||||
HttpLayerRouter,
|
||||
HttpServerRequest,
|
||||
HttpServerResponse,
|
||||
} from "@effect/platform";
|
||||
import { Effect } from "effect";
|
||||
|
||||
import type { Context } from "hono";
|
||||
import { plaidClient } from "./plaid";
|
||||
// import { LinkSessionFinishedWebhook, WebhookType } from "plaid";
|
||||
|
||||
export const WebhookReceiverRoute = HttpLayerRouter.add(
|
||||
"*",
|
||||
"/api/webhook_receiver",
|
||||
export const webhook = async (c: Context) => {
|
||||
console.log("Got webhook");
|
||||
const b = await c.req.text();
|
||||
console.log("body:", b);
|
||||
|
||||
Effect.gen(function* () {
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
const body = yield* request.json;
|
||||
Effect.log("Got a webhook!", body);
|
||||
return HttpServerResponse.text("HELLO THERE");
|
||||
}),
|
||||
);
|
||||
return c.text("Hi");
|
||||
};
|
||||
|
||||
358
apps/api/src/zero.ts
Normal file
358
apps/api/src/zero.ts
Normal file
@@ -0,0 +1,358 @@
|
||||
import {
|
||||
type ReadonlyJSONValue,
|
||||
type Transaction,
|
||||
withValidation,
|
||||
} from "@rocicorp/zero";
|
||||
import {
|
||||
handleGetQueriesRequest,
|
||||
PushProcessor,
|
||||
ZQLDatabase,
|
||||
} from "@rocicorp/zero/server";
|
||||
import { PostgresJSConnection } from "@rocicorp/zero/pg";
|
||||
import postgres from "postgres";
|
||||
import {
|
||||
createMutators as createMutatorsShared,
|
||||
isLoggedIn,
|
||||
queries,
|
||||
schema,
|
||||
type Mutators,
|
||||
type Schema,
|
||||
} from "@money/shared";
|
||||
import type { AuthData } from "@money/shared/auth";
|
||||
import { getHono } from "./hono";
|
||||
import {
|
||||
Configuration,
|
||||
CountryCode,
|
||||
PlaidApi,
|
||||
PlaidEnvironments,
|
||||
Products,
|
||||
SandboxItemFireWebhookRequestWebhookCodeEnum,
|
||||
WebhookType,
|
||||
} from "plaid";
|
||||
import { randomUUID } from "crypto";
|
||||
import { db } from "./db";
|
||||
import {
|
||||
balance,
|
||||
plaidAccessTokens,
|
||||
plaidLink,
|
||||
transaction,
|
||||
} from "@money/shared/db";
|
||||
import {
|
||||
and,
|
||||
eq,
|
||||
inArray,
|
||||
sql,
|
||||
type InferInsertModel,
|
||||
type InferSelectModel,
|
||||
} from "drizzle-orm";
|
||||
import { plaidClient } from "./plaid";
|
||||
import { transactionFromPlaid } from "./plaid/tx";
|
||||
|
||||
const processor = new PushProcessor(
|
||||
new ZQLDatabase(
|
||||
new PostgresJSConnection(postgres(process.env.ZERO_UPSTREAM_DB! as string)),
|
||||
schema,
|
||||
),
|
||||
);
|
||||
|
||||
type Tx = Transaction<Schema>;
|
||||
|
||||
const createMutators = (authData: AuthData | null) => {
|
||||
const mutators = createMutatorsShared(authData);
|
||||
return {
|
||||
...mutators,
|
||||
link: {
|
||||
...mutators.link,
|
||||
async create() {
|
||||
isLoggedIn(authData);
|
||||
const r = await plaidClient.linkTokenCreate({
|
||||
user: {
|
||||
client_user_id: authData.user.id,
|
||||
},
|
||||
client_name: "Koon Money",
|
||||
language: "en",
|
||||
products: [Products.Transactions],
|
||||
country_codes: [CountryCode.Us],
|
||||
webhook: "https://webhooks.koon.us/api/webhook_receiver",
|
||||
hosted_link: {},
|
||||
});
|
||||
const { link_token, hosted_link_url } = r.data;
|
||||
|
||||
if (!hosted_link_url) throw Error("No link in response");
|
||||
|
||||
await db.insert(plaidLink).values({
|
||||
id: randomUUID() as string,
|
||||
user_id: authData.user.id,
|
||||
link: hosted_link_url,
|
||||
token: link_token,
|
||||
});
|
||||
},
|
||||
|
||||
async get(_, { link_token }) {
|
||||
isLoggedIn(authData);
|
||||
|
||||
try {
|
||||
const token = await db.query.plaidLink.findFirst({
|
||||
where: and(
|
||||
eq(plaidLink.token, link_token),
|
||||
eq(plaidLink.user_id, authData.user.id),
|
||||
),
|
||||
});
|
||||
if (!token) throw Error("Link not found");
|
||||
if (token.completeAt) return;
|
||||
|
||||
const linkResp = await plaidClient.linkTokenGet({
|
||||
link_token,
|
||||
});
|
||||
if (!linkResp) throw Error("No link respo");
|
||||
|
||||
console.log(JSON.stringify(linkResp.data, null, 4));
|
||||
|
||||
const item_add_result = linkResp.data.link_sessions
|
||||
?.at(0)
|
||||
?.results?.item_add_results.at(0);
|
||||
|
||||
// We will assume its not done yet.
|
||||
if (!item_add_result) return;
|
||||
|
||||
const { data } = await plaidClient.itemPublicTokenExchange({
|
||||
public_token: item_add_result.public_token,
|
||||
});
|
||||
|
||||
await db.insert(plaidAccessTokens).values({
|
||||
id: randomUUID(),
|
||||
userId: authData.user.id,
|
||||
token: data.access_token,
|
||||
logoUrl: "",
|
||||
name: item_add_result.institution?.name || "Unknown",
|
||||
});
|
||||
|
||||
await db
|
||||
.update(plaidLink)
|
||||
.set({
|
||||
completeAt: new Date(),
|
||||
})
|
||||
.where(eq(plaidLink.token, link_token));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw Error("Plaid error");
|
||||
}
|
||||
},
|
||||
async webhook() {
|
||||
isLoggedIn(authData);
|
||||
|
||||
const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
});
|
||||
if (accounts.length == 0) {
|
||||
console.error("No accounts");
|
||||
return;
|
||||
}
|
||||
|
||||
const account = accounts.at(0)!;
|
||||
|
||||
const { data } = await plaidClient.sandboxItemFireWebhook({
|
||||
access_token: account.token,
|
||||
webhook_type: WebhookType.Transactions,
|
||||
webhook_code:
|
||||
SandboxItemFireWebhookRequestWebhookCodeEnum.DefaultUpdate,
|
||||
});
|
||||
|
||||
console.log(data);
|
||||
},
|
||||
async sync() {
|
||||
isLoggedIn(authData);
|
||||
|
||||
const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
});
|
||||
if (accounts.length == 0) {
|
||||
console.error("No accounts");
|
||||
return;
|
||||
}
|
||||
|
||||
const account = accounts.at(0)!;
|
||||
|
||||
const { data } = await plaidClient.transactionsSync({
|
||||
access_token: account.token,
|
||||
cursor: account.syncCursor || undefined,
|
||||
});
|
||||
|
||||
const added = data.added.map((tx) =>
|
||||
transactionFromPlaid(authData.user.id, tx),
|
||||
);
|
||||
|
||||
const updated = data.modified.map((tx) =>
|
||||
transactionFromPlaid(authData.user.id, tx),
|
||||
);
|
||||
|
||||
console.log("added", added.length);
|
||||
console.log("updated", updated.length);
|
||||
console.log("removed", data.removed.length);
|
||||
console.log("next cursor", data.next_cursor);
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
if (added.length) {
|
||||
await tx.insert(transaction).values(added);
|
||||
}
|
||||
|
||||
if (updated.length) {
|
||||
await tx
|
||||
.insert(transaction)
|
||||
.values(updated)
|
||||
.onConflictDoUpdate({
|
||||
target: transaction.plaid_id,
|
||||
set: {
|
||||
name: sql.raw(`excluded.${transaction.name.name}`),
|
||||
amount: sql.raw(`excluded.${transaction.amount.name}`),
|
||||
json: sql.raw(`excluded.${transaction.json.name}`),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (data.removed.length) {
|
||||
await tx.delete(transaction).where(
|
||||
inArray(
|
||||
transaction.id,
|
||||
data.removed.map((tx) => tx.transaction_id),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await tx
|
||||
.update(plaidAccessTokens)
|
||||
.set({ syncCursor: data.next_cursor })
|
||||
.where(eq(plaidAccessTokens.id, account.id));
|
||||
});
|
||||
},
|
||||
|
||||
// async updateTransactions() {
|
||||
// isLoggedIn(authData);
|
||||
// const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
// where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
// });
|
||||
// if (accounts.length == 0) {
|
||||
// console.error("No accounts");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// for (const account of accounts) {
|
||||
// const { data } = await plaidClient.transactionsGet({
|
||||
// access_token: account.token,
|
||||
// start_date: "2025-10-01",
|
||||
// end_date: new Date().toISOString().split("T")[0],
|
||||
// });
|
||||
//
|
||||
// const transactions = data.transactions.map(
|
||||
// (tx) =>
|
||||
// ({
|
||||
// id: randomUUID(),
|
||||
// user_id: authData.user.id,
|
||||
// plaid_id: tx.transaction_id,
|
||||
// account_id: tx.account_id,
|
||||
// name: tx.name,
|
||||
// amount: tx.amount as any,
|
||||
// datetime: tx.datetime
|
||||
// ? new Date(tx.datetime)
|
||||
// : new Date(tx.date),
|
||||
// authorized_datetime: tx.authorized_datetime
|
||||
// ? new Date(tx.authorized_datetime)
|
||||
// : undefined,
|
||||
// json: JSON.stringify(tx),
|
||||
// }) satisfies InferInsertModel<typeof transaction>,
|
||||
// );
|
||||
//
|
||||
// await db
|
||||
// .insert(transaction)
|
||||
// .values(transactions)
|
||||
// .onConflictDoNothing({
|
||||
// target: transaction.plaid_id,
|
||||
// });
|
||||
//
|
||||
// const txReplacingPendingIds = data.transactions
|
||||
// .filter((t) => t.pending_transaction_id)
|
||||
// .map((t) => t.pending_transaction_id!);
|
||||
//
|
||||
// await db
|
||||
// .delete(transaction)
|
||||
// .where(inArray(transaction.plaid_id, txReplacingPendingIds));
|
||||
// }
|
||||
// },
|
||||
//
|
||||
// async updateBalences() {
|
||||
// isLoggedIn(authData);
|
||||
// const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
// where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
// });
|
||||
// if (accounts.length == 0) {
|
||||
// console.error("No accounts");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// for (const account of accounts) {
|
||||
// const { data } = await plaidClient.accountsBalanceGet({
|
||||
// access_token: account.token,
|
||||
// });
|
||||
// await db
|
||||
// .insert(balance)
|
||||
// .values(
|
||||
// data.accounts.map((bal) => ({
|
||||
// id: randomUUID(),
|
||||
// user_id: authData.user.id,
|
||||
// plaid_id: bal.account_id,
|
||||
// avaliable: bal.balances.available as any,
|
||||
// current: bal.balances.current as any,
|
||||
// name: bal.name,
|
||||
// tokenId: account.id,
|
||||
// })),
|
||||
// )
|
||||
// .onConflictDoUpdate({
|
||||
// target: balance.plaid_id,
|
||||
// set: {
|
||||
// current: sql.raw(`excluded.${balance.current.name}`),
|
||||
// avaliable: sql.raw(`excluded.${balance.avaliable.name}`),
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
},
|
||||
} as const satisfies Mutators;
|
||||
};
|
||||
|
||||
const zero = getHono()
|
||||
.post("/mutate", async (c) => {
|
||||
const authData = c.get("auth");
|
||||
|
||||
const result = await processor.process(createMutators(authData), c.req.raw);
|
||||
|
||||
return c.json(result);
|
||||
})
|
||||
.post("/get-queries", async (c) => {
|
||||
const authData = c.get("auth");
|
||||
|
||||
const result = await handleGetQueriesRequest(
|
||||
(name, args) => ({ query: getQuery(authData, name, args) }),
|
||||
schema,
|
||||
c.req.raw,
|
||||
);
|
||||
|
||||
return c.json(result);
|
||||
});
|
||||
|
||||
const validatedQueries = Object.fromEntries(
|
||||
Object.values(queries).map((q) => [q.queryName, withValidation(q)]),
|
||||
);
|
||||
|
||||
function getQuery(
|
||||
authData: AuthData | null,
|
||||
name: string,
|
||||
args: readonly ReadonlyJSONValue[],
|
||||
) {
|
||||
if (name in validatedQueries) {
|
||||
const q = validatedQueries[name];
|
||||
return q(authData, ...args);
|
||||
}
|
||||
throw new Error(`Unknown query: ${name}`);
|
||||
}
|
||||
|
||||
export { zero };
|
||||
@@ -1,6 +0,0 @@
|
||||
import { Data } from "effect";
|
||||
|
||||
export class ZeroUnknownError extends Data.TaggedError("ZeroUnknownError") {}
|
||||
export class ZeroError extends Data.TaggedError("ZeroError")<{
|
||||
error: Error;
|
||||
}> {}
|
||||
@@ -1,106 +0,0 @@
|
||||
import {
|
||||
HttpLayerRouter,
|
||||
HttpServerRequest,
|
||||
HttpServerResponse,
|
||||
Url,
|
||||
} from "@effect/platform";
|
||||
import { Console, Effect, Layer } from "effect";
|
||||
import { CurrentSession, SessionMiddleware } from "../middleware/session";
|
||||
import { ZeroError, ZeroUnknownError } from "./errors";
|
||||
import { withValidation, type ReadonlyJSONValue } from "@rocicorp/zero";
|
||||
import {
|
||||
handleGetQueriesRequest,
|
||||
PushProcessor,
|
||||
ZQLDatabase,
|
||||
} from "@rocicorp/zero/server";
|
||||
import { BASE_URL, queries, schema } from "@money/shared";
|
||||
import type { AuthSchemaType } from "@money/shared/auth";
|
||||
import { PostgresJSConnection } from "@rocicorp/zero/pg";
|
||||
import postgres from "postgres";
|
||||
import { createMutators } from "./mutators";
|
||||
|
||||
const processor = new PushProcessor(
|
||||
new ZQLDatabase(
|
||||
new PostgresJSConnection(postgres(process.env.ZERO_UPSTREAM_DB! as string)),
|
||||
schema,
|
||||
),
|
||||
);
|
||||
|
||||
export const ZeroQueryRoute = HttpLayerRouter.add(
|
||||
"POST",
|
||||
"/api/zero/get-queries",
|
||||
|
||||
Effect.gen(function* () {
|
||||
const { auth } = yield* CurrentSession;
|
||||
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
const body = yield* request.json;
|
||||
|
||||
const result = yield* Effect.tryPromise({
|
||||
try: () =>
|
||||
handleGetQueriesRequest(
|
||||
(name, args) => ({ query: getQuery(auth, name, args) }),
|
||||
schema,
|
||||
body as ReadonlyJSONValue,
|
||||
),
|
||||
catch: (error) =>
|
||||
error instanceof Error
|
||||
? new ZeroError({ error })
|
||||
: new ZeroUnknownError(),
|
||||
}).pipe(
|
||||
Effect.tapErrorTag("ZeroError", (err) =>
|
||||
Console.error("Zero Error", err.error),
|
||||
),
|
||||
);
|
||||
|
||||
return yield* HttpServerResponse.json(result);
|
||||
}),
|
||||
).pipe(Layer.provide(SessionMiddleware.layer));
|
||||
|
||||
function getQuery(
|
||||
authData: AuthSchemaType | null,
|
||||
name: string,
|
||||
args: readonly ReadonlyJSONValue[],
|
||||
) {
|
||||
if (name in validatedQueries) {
|
||||
const q = validatedQueries[name];
|
||||
return q(authData, ...args);
|
||||
}
|
||||
throw new Error(`Unknown query: ${name}`);
|
||||
}
|
||||
|
||||
export const validatedQueries = Object.fromEntries(
|
||||
Object.values(queries).map((q) => [q.queryName, withValidation(q)]),
|
||||
);
|
||||
|
||||
export const ZeroMutateRoute = HttpLayerRouter.add(
|
||||
"POST",
|
||||
"/api/zero/mutate",
|
||||
|
||||
Effect.gen(function* () {
|
||||
const { auth } = yield* CurrentSession;
|
||||
|
||||
const request = yield* HttpServerRequest.HttpServerRequest;
|
||||
const url = yield* Url.fromString(`${BASE_URL}${request.url}`);
|
||||
const body = yield* request.json;
|
||||
|
||||
const result = yield* Effect.tryPromise({
|
||||
try: () =>
|
||||
processor.process(
|
||||
createMutators(auth),
|
||||
url.searchParams,
|
||||
body as ReadonlyJSONValue,
|
||||
),
|
||||
catch: (error) =>
|
||||
error instanceof Error
|
||||
? new ZeroError({ error })
|
||||
: new ZeroUnknownError(),
|
||||
}).pipe(
|
||||
Effect.tapErrorTag("ZeroError", (err) =>
|
||||
Console.error("Zero Error", err.error),
|
||||
),
|
||||
);
|
||||
|
||||
return yield* HttpServerResponse.json(result);
|
||||
}),
|
||||
).pipe(Layer.provide(SessionMiddleware.layer));
|
||||
@@ -1,202 +0,0 @@
|
||||
import {
|
||||
createMutators as createMutatorsShared,
|
||||
isLoggedIn,
|
||||
queries,
|
||||
schema,
|
||||
type Mutators,
|
||||
type Schema,
|
||||
} from "@money/shared";
|
||||
import type { AuthSchemaType } from "@money/shared/auth";
|
||||
import {
|
||||
type ReadonlyJSONValue,
|
||||
type Transaction,
|
||||
withValidation,
|
||||
} from "@rocicorp/zero";
|
||||
import { plaidClient } from "../plaid";
|
||||
import { CountryCode, Products } from "plaid";
|
||||
import {
|
||||
balance,
|
||||
plaidAccessTokens,
|
||||
plaidLink,
|
||||
transaction,
|
||||
} from "@money/shared/db";
|
||||
import { db } from "../db";
|
||||
import { randomUUID } from "crypto";
|
||||
import { and, eq, inArray, sql, type InferInsertModel } from "drizzle-orm";
|
||||
|
||||
type Tx = Transaction<Schema>;
|
||||
|
||||
export const createMutators = (authData: AuthSchemaType | null) => {
|
||||
const mutators = createMutatorsShared(authData);
|
||||
return {
|
||||
...mutators,
|
||||
link: {
|
||||
...mutators.link,
|
||||
async create() {
|
||||
isLoggedIn(authData);
|
||||
const r = await plaidClient.linkTokenCreate({
|
||||
user: {
|
||||
client_user_id: authData.user.id,
|
||||
},
|
||||
client_name: "Koon Money",
|
||||
language: "en",
|
||||
products: [Products.Transactions],
|
||||
country_codes: [CountryCode.Us],
|
||||
webhook: "https://webhooks.koon.us/api/webhook_receiver",
|
||||
hosted_link: {},
|
||||
});
|
||||
const { link_token, hosted_link_url } = r.data;
|
||||
|
||||
if (!hosted_link_url) throw Error("No link in response");
|
||||
|
||||
await db.insert(plaidLink).values({
|
||||
id: randomUUID() as string,
|
||||
user_id: authData.user.id,
|
||||
link: hosted_link_url,
|
||||
token: link_token,
|
||||
});
|
||||
},
|
||||
|
||||
async get(_, { link_token }) {
|
||||
isLoggedIn(authData);
|
||||
|
||||
try {
|
||||
const token = await db.query.plaidLink.findFirst({
|
||||
where: and(
|
||||
eq(plaidLink.token, link_token),
|
||||
eq(plaidLink.user_id, authData.user.id),
|
||||
),
|
||||
});
|
||||
if (!token) throw Error("Link not found");
|
||||
if (token.completeAt) return;
|
||||
|
||||
const linkResp = await plaidClient.linkTokenGet({
|
||||
link_token,
|
||||
});
|
||||
if (!linkResp) throw Error("No link respo");
|
||||
|
||||
console.log(JSON.stringify(linkResp.data, null, 4));
|
||||
|
||||
const item_add_result = linkResp.data.link_sessions
|
||||
?.at(0)
|
||||
?.results?.item_add_results.at(0);
|
||||
|
||||
// We will assume its not done yet.
|
||||
if (!item_add_result) return;
|
||||
|
||||
const { data } = await plaidClient.itemPublicTokenExchange({
|
||||
public_token: item_add_result.public_token,
|
||||
});
|
||||
|
||||
await db.insert(plaidAccessTokens).values({
|
||||
id: randomUUID(),
|
||||
userId: authData.user.id,
|
||||
token: data.access_token,
|
||||
logoUrl: "",
|
||||
name: item_add_result.institution?.name || "Unknown",
|
||||
});
|
||||
|
||||
await db
|
||||
.update(plaidLink)
|
||||
.set({
|
||||
completeAt: new Date(),
|
||||
})
|
||||
.where(eq(plaidLink.token, link_token));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw Error("Plaid error");
|
||||
}
|
||||
},
|
||||
|
||||
async updateTransactions() {
|
||||
isLoggedIn(authData);
|
||||
const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
});
|
||||
if (accounts.length == 0) {
|
||||
console.error("No accounts");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const account of accounts) {
|
||||
const { data } = await plaidClient.transactionsGet({
|
||||
access_token: account.token,
|
||||
start_date: "2025-10-01",
|
||||
end_date: new Date().toISOString().split("T")[0],
|
||||
});
|
||||
|
||||
const transactions = data.transactions.map(
|
||||
(tx) =>
|
||||
({
|
||||
id: randomUUID(),
|
||||
user_id: authData.user.id,
|
||||
plaid_id: tx.transaction_id,
|
||||
account_id: tx.account_id,
|
||||
name: tx.name,
|
||||
amount: tx.amount as any,
|
||||
datetime: tx.datetime
|
||||
? new Date(tx.datetime)
|
||||
: new Date(tx.date),
|
||||
authorized_datetime: tx.authorized_datetime
|
||||
? new Date(tx.authorized_datetime)
|
||||
: undefined,
|
||||
json: JSON.stringify(tx),
|
||||
}) satisfies InferInsertModel<typeof transaction>,
|
||||
);
|
||||
|
||||
await db
|
||||
.insert(transaction)
|
||||
.values(transactions)
|
||||
.onConflictDoNothing({
|
||||
target: transaction.plaid_id,
|
||||
});
|
||||
|
||||
const txReplacingPendingIds = data.transactions
|
||||
.filter((t) => t.pending_transaction_id)
|
||||
.map((t) => t.pending_transaction_id!);
|
||||
|
||||
await db
|
||||
.delete(transaction)
|
||||
.where(inArray(transaction.plaid_id, txReplacingPendingIds));
|
||||
}
|
||||
},
|
||||
|
||||
async updateBalences() {
|
||||
isLoggedIn(authData);
|
||||
const accounts = await db.query.plaidAccessTokens.findMany({
|
||||
where: eq(plaidAccessTokens.userId, authData.user.id),
|
||||
});
|
||||
if (accounts.length == 0) {
|
||||
console.error("No accounts");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const account of accounts) {
|
||||
const { data } = await plaidClient.accountsBalanceGet({
|
||||
access_token: account.token,
|
||||
});
|
||||
await db
|
||||
.insert(balance)
|
||||
.values(
|
||||
data.accounts.map((bal) => ({
|
||||
id: randomUUID(),
|
||||
user_id: authData.user.id,
|
||||
plaid_id: bal.account_id,
|
||||
avaliable: bal.balances.available as any,
|
||||
current: bal.balances.current as any,
|
||||
name: bal.name,
|
||||
tokenId: account.id,
|
||||
})),
|
||||
)
|
||||
.onConflictDoUpdate({
|
||||
target: balance.plaid_id,
|
||||
set: {
|
||||
current: sql.raw(`excluded.${balance.current.name}`),
|
||||
avaliable: sql.raw(`excluded.${balance.avaliable.name}`),
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
} as const satisfies Mutators;
|
||||
};
|
||||
@@ -38,7 +38,8 @@
|
||||
}
|
||||
}
|
||||
],
|
||||
"expo-sqlite"
|
||||
"expo-sqlite",
|
||||
"expo-secure-store"
|
||||
],
|
||||
"experiments": {
|
||||
"typedRoutes": true,
|
||||
|
||||
@@ -13,7 +13,7 @@ export default function Page() {
|
||||
|
||||
useEffect(() => {
|
||||
const handler = () => {
|
||||
const newRoute = window.location.pathname.slice(1);
|
||||
const newRoute = window.location.pathname.slice(1) + "/";
|
||||
setRoute(newRoute);
|
||||
};
|
||||
|
||||
|
||||
@@ -10,14 +10,14 @@
|
||||
"web": "expo start --web",
|
||||
"build": "expo export --platform web",
|
||||
"lint": "expo lint",
|
||||
"db:migrate": "dotenv -- pnpm run --dir=shared db:migrate",
|
||||
"db:gen": "dotenv -- pnpm run --dir=shared generate:zero"
|
||||
"db:migrate": "dotenv -- bun run --dir=shared db:migrate",
|
||||
"db:gen": "dotenv -- bun run --dir=shared generate:zero"
|
||||
},
|
||||
"dependencies": {
|
||||
"@better-auth/expo": "^1.3.27",
|
||||
"@expo/vector-icons": "^15.0.2",
|
||||
"@money/shared": "workspace:*",
|
||||
"@money/ui": "workspace:*",
|
||||
"@money/shared": "*",
|
||||
"@money/ui": "*",
|
||||
"@react-navigation/bottom-tabs": "^7.4.0",
|
||||
"@react-navigation/elements": "^2.6.3",
|
||||
"@react-navigation/native": "^7.1.8",
|
||||
@@ -31,7 +31,9 @@
|
||||
"expo-haptics": "~15.0.7",
|
||||
"expo-image": "~3.0.9",
|
||||
"expo-linking": "~8.0.8",
|
||||
"expo-network": "~8.0.8",
|
||||
"expo-router": "~6.0.11",
|
||||
"expo-secure-store": "~15.0.8",
|
||||
"expo-splash-screen": "~31.0.10",
|
||||
"expo-sqlite": "~16.0.8",
|
||||
"expo-status-bar": "~3.0.8",
|
||||
|
||||
@@ -11,10 +11,9 @@ import {
|
||||
} from "effect";
|
||||
import { FileSystem } from "@effect/platform";
|
||||
import { config } from "./config";
|
||||
import { AuthState } from "./schema";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import type { BetterFetchResponse } from "@better-fetch/fetch";
|
||||
import { AuthSchema } from "@money/shared/auth";
|
||||
import { encode } from "node:punycode";
|
||||
|
||||
class AuthClientUnknownError extends Data.TaggedError(
|
||||
"AuthClientUnknownError",
|
||||
@@ -130,7 +129,7 @@ const pollToken = ({ device_code }: { device_code: string }) =>
|
||||
const getFromFromDisk = Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const content = yield* fs.readFileString(config.authPath);
|
||||
const auth = yield* Schema.decode(Schema.parseJson(AuthSchema))(content);
|
||||
const auth = yield* Schema.decode(Schema.parseJson(AuthState))(content);
|
||||
if (auth.session.expiresAt < new Date())
|
||||
yield* Effect.fail(new AuthClientExpiredToken());
|
||||
return auth;
|
||||
@@ -161,11 +160,10 @@ const requestAuth = Effect.gen(function* () {
|
||||
);
|
||||
if (sessionData == null) return yield* Effect.fail(new AuthClientNoData());
|
||||
|
||||
const result = yield* Schema.decodeUnknown(AuthSchema)(sessionData);
|
||||
const encoded = yield* Schema.encode(AuthSchema)(result);
|
||||
const result = yield* Schema.decodeUnknown(AuthState)(sessionData);
|
||||
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
yield* fs.writeFileString(config.authPath, JSON.stringify(encoded));
|
||||
yield* fs.writeFileString(config.authPath, JSON.stringify(result));
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
@@ -1,36 +1,26 @@
|
||||
import { createCliRenderer } from "@opentui/core";
|
||||
import { createRoot, useKeyboard } from "@opentui/react";
|
||||
import { createRoot, useKeyboard, useRenderer } from "@opentui/react";
|
||||
import { App, type Route } from "@money/ui";
|
||||
import { ZeroProvider } from "@rocicorp/zero/react";
|
||||
import { schema } from "@money/shared";
|
||||
import { schema, createMutators } from "@money/shared";
|
||||
import { useState } from "react";
|
||||
import { AuthClientLayer, getAuth } from "./auth";
|
||||
import { Effect, Redacted } from "effect";
|
||||
import { Effect } from "effect";
|
||||
import { BunContext } from "@effect/platform-bun";
|
||||
import type { AuthData } from "./schema";
|
||||
import { kvStore } from "./store";
|
||||
import { config } from "./config";
|
||||
import { type AuthSchemaType } from "@money/shared/auth";
|
||||
|
||||
function Main({ auth }: { auth: AuthSchemaType }) {
|
||||
function Main({ auth }: { auth: AuthData }) {
|
||||
const [route, setRoute] = useState<Route>("/");
|
||||
const renderer = useRenderer();
|
||||
|
||||
useKeyboard((key) => {
|
||||
if (key.name == "c" && key.ctrl) process.exit(0);
|
||||
if (key.name == "i" && key.meta) renderer.console.toggle();
|
||||
});
|
||||
|
||||
return (
|
||||
<ZeroProvider
|
||||
{...{
|
||||
userID: auth.user.id,
|
||||
auth: Redacted.value(auth.session.token),
|
||||
server: config.zeroUrl,
|
||||
schema,
|
||||
kvStore,
|
||||
}}
|
||||
>
|
||||
<App auth={auth} route={route} setRoute={setRoute} />
|
||||
</ZeroProvider>
|
||||
);
|
||||
return <App auth={auth} route={route} setRoute={setRoute} />;
|
||||
}
|
||||
|
||||
const auth = await Effect.runPromise(
|
||||
@@ -40,4 +30,17 @@ const auth = await Effect.runPromise(
|
||||
),
|
||||
);
|
||||
const renderer = await createCliRenderer({ exitOnCtrlC: false });
|
||||
createRoot(renderer).render(<Main auth={auth} />);
|
||||
createRoot(renderer).render(
|
||||
<ZeroProvider
|
||||
{...{
|
||||
userID: auth.user.id,
|
||||
auth: auth.session.token,
|
||||
server: config.zeroUrl,
|
||||
schema,
|
||||
mutators: createMutators(auth),
|
||||
kvStore,
|
||||
}}
|
||||
>
|
||||
<Main auth={auth} />
|
||||
</ZeroProvider>,
|
||||
);
|
||||
|
||||
34
apps/tui/src/schema.ts
Normal file
34
apps/tui/src/schema.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Schema } from "effect";
|
||||
|
||||
const DateFromDateOrString = Schema.Union(
|
||||
Schema.DateFromString,
|
||||
Schema.DateFromSelf,
|
||||
);
|
||||
|
||||
const SessionSchema = Schema.Struct({
|
||||
expiresAt: DateFromDateOrString,
|
||||
token: Schema.String,
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
ipAddress: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userAgent: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userId: Schema.String,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
const UserSchema = Schema.Struct({
|
||||
name: Schema.String,
|
||||
email: Schema.String,
|
||||
emailVerified: Schema.Boolean,
|
||||
image: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
export const AuthState = Schema.Struct({
|
||||
session: SessionSchema,
|
||||
user: UserSchema,
|
||||
});
|
||||
|
||||
export type AuthData = typeof AuthState.Type;
|
||||
20
package.json
20
package.json
@@ -3,16 +3,14 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "process-compose up -p 0",
|
||||
"tui": "bun run --hot apps/tui/src/index.tsx"
|
||||
"tui": "bun --filter=@money/tui run build && bun --filter=@money/tui run start",
|
||||
"db:gen": "bun --filter=@money/shared db:gen",
|
||||
"db:push": "bun --filter=@money/shared db:push"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"@rocicorp/zero-sqlite3"
|
||||
],
|
||||
"ignoredBuiltDependencies": [
|
||||
"esbuild",
|
||||
"protobufjs",
|
||||
"unrs-resolver"
|
||||
]
|
||||
}
|
||||
"workspaces": ["apps/*", "packages/*"],
|
||||
"trustedDependencies": [
|
||||
"@rocicorp/zero-sqlite3",
|
||||
"protobufjs",
|
||||
"unrs-resolver"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@ import type {
|
||||
StyleProp,
|
||||
ViewStyle,
|
||||
LinkingImpl,
|
||||
TextInputProps,
|
||||
} from "react-native";
|
||||
import { useTerminalDimensions } from "@opentui/react";
|
||||
import { RGBA } from "@opentui/core";
|
||||
import { BorderSides, RGBA } from "@opentui/core";
|
||||
import { platform } from "node:os";
|
||||
import { exec } from "node:child_process";
|
||||
|
||||
@@ -65,6 +66,27 @@ export function View({ children, style }: ViewProps) {
|
||||
: undefined;
|
||||
|
||||
const padding = attr(style, "padding", "number");
|
||||
const paddingTop = attr(style, "paddingTop", "number");
|
||||
const paddingLeft = attr(style, "paddingLeft", "number");
|
||||
const paddingBottom = attr(style, "paddingBottom", "number");
|
||||
const paddingRight = attr(style, "paddingRight", "number");
|
||||
const gap = attr(style, "gap", "number");
|
||||
|
||||
const borderBottomWidth = attr(style, "borderBottomWidth", "number");
|
||||
const borderTopWidth = attr(style, "borderTopWidth", "number");
|
||||
const borderLeftWidth = attr(style, "borderLeftWidth", "number");
|
||||
const borderRightWidth = attr(style, "borderRightWidth", "number");
|
||||
|
||||
const borderBottomColor = attr(style, "borderBottomColor", "string");
|
||||
const borderTopColor = attr(style, "borderTopColor", "string");
|
||||
const borderLeftColor = attr(style, "borderLeftColor", "string");
|
||||
const borderRightColor = attr(style, "borderRightColor", "string");
|
||||
|
||||
const borderColor = attr(style, "borderColor", "string");
|
||||
|
||||
const top = attr(style, "top", "number");
|
||||
|
||||
const width = attr(style, "width", "number");
|
||||
|
||||
const props = {
|
||||
overflow: attr(style, "overflow", "string"),
|
||||
@@ -74,17 +96,48 @@ export function View({ children, style }: ViewProps) {
|
||||
justifyContent: attr(style, "justifyContent", "string"),
|
||||
flexShrink: attr(style, "flexShrink", "number"),
|
||||
flexDirection: attr(style, "flexDirection", "string"),
|
||||
zIndex: attr(style, "zIndex", "number"),
|
||||
left: attr(style, "left", "number"),
|
||||
right: attr(style, "right", "number"),
|
||||
bottom: attr(style, "bottom", "number"),
|
||||
flexGrow:
|
||||
attr(style, "flex", "number") || attr(style, "flexGrow", "number"),
|
||||
};
|
||||
|
||||
const border = (() => {
|
||||
const sides: BorderSides[] = [];
|
||||
if (borderBottomWidth) sides.push("bottom");
|
||||
if (borderTopWidth) sides.push("top");
|
||||
if (borderLeftWidth) sides.push("left");
|
||||
if (borderRightWidth) sides.push("right");
|
||||
if (!sides.length) return undefined;
|
||||
return sides;
|
||||
})();
|
||||
|
||||
return (
|
||||
<box
|
||||
backgroundColor={bg}
|
||||
paddingTop={padding && Math.round(padding / RATIO_HEIGHT)}
|
||||
paddingBottom={padding && Math.round(padding / RATIO_HEIGHT)}
|
||||
paddingLeft={padding && Math.round(padding / RATIO_WIDTH)}
|
||||
paddingRight={padding && Math.round(padding / RATIO_WIDTH)}
|
||||
paddingTop={
|
||||
(paddingTop && Math.round(paddingTop / RATIO_HEIGHT)) ||
|
||||
(padding && Math.round(padding / RATIO_HEIGHT))
|
||||
}
|
||||
paddingBottom={
|
||||
(paddingBottom && Math.round(paddingBottom / RATIO_HEIGHT)) ||
|
||||
(padding && Math.round(padding / RATIO_HEIGHT))
|
||||
}
|
||||
paddingLeft={
|
||||
(paddingLeft && Math.round(paddingLeft / RATIO_WIDTH)) ||
|
||||
(padding && Math.round(padding / RATIO_WIDTH))
|
||||
}
|
||||
paddingRight={
|
||||
(paddingRight && Math.round(paddingRight / RATIO_WIDTH)) ||
|
||||
(padding && Math.round(padding / RATIO_WIDTH))
|
||||
}
|
||||
gap={gap && Math.round(gap / RATIO_HEIGHT)}
|
||||
border={border}
|
||||
borderColor={borderColor}
|
||||
width={width && Math.round(width / RATIO_WIDTH)}
|
||||
top={top && Math.round(top / RATIO_HEIGHT)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
@@ -221,6 +274,33 @@ export function Modal({ children, visible }: ModalProps) {
|
||||
);
|
||||
}
|
||||
|
||||
export function TextInput({
|
||||
defaultValue,
|
||||
onChangeText,
|
||||
onKeyPress,
|
||||
}: TextInputProps) {
|
||||
return (
|
||||
<input
|
||||
minWidth={20}
|
||||
minHeight={1}
|
||||
backgroundColor="white"
|
||||
textColor="black"
|
||||
focused={true}
|
||||
cursorColor={"black"}
|
||||
onInput={onChangeText}
|
||||
onKeyDown={(key) =>
|
||||
// @ts-ignore
|
||||
onKeyPress({
|
||||
nativeEvent: {
|
||||
key: key.name == "return" ? "Enter" : key.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
placeholder={defaultValue}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const Platform = {
|
||||
OS: "tui",
|
||||
};
|
||||
|
||||
@@ -13,6 +13,6 @@
|
||||
},
|
||||
"scripts": {
|
||||
"db:gen": "drizzle-zero generate -s ./src/db/schema/public.ts -o ./src/zero-schema.gen.ts -f && sed -i 's/enableLegacyQueries: true,/enableLegacyQueries: false,/g' src/zero-schema.gen.ts && sed -i 's/enableLegacyMutators: true,/enableLegacyMutators: false,/g' src/zero-schema.gen.ts",
|
||||
"db:migrate": "drizzle-kit push"
|
||||
"db:push": "drizzle-kit push"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { Schema } from "effect";
|
||||
|
||||
export const sessionSchema = z.object({
|
||||
id: z.string(),
|
||||
@@ -21,39 +20,6 @@ export const authDataSchema = z.object({
|
||||
user: userSchema,
|
||||
});
|
||||
|
||||
const DateFromDateOrString = Schema.Union(
|
||||
Schema.DateFromString,
|
||||
Schema.DateFromSelf,
|
||||
);
|
||||
|
||||
export const SessionSchema = Schema.Struct({
|
||||
expiresAt: DateFromDateOrString,
|
||||
token: Schema.Redacted(Schema.String),
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
ipAddress: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userAgent: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userId: Schema.String,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
export const UserSchema = Schema.Struct({
|
||||
name: Schema.String,
|
||||
email: Schema.String,
|
||||
emailVerified: Schema.Boolean,
|
||||
image: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
export const AuthSchema = Schema.Struct({
|
||||
session: SessionSchema,
|
||||
user: UserSchema,
|
||||
});
|
||||
|
||||
export type AuthSchemaType = Schema.Schema.Type<typeof AuthSchema>;
|
||||
|
||||
export type Session = z.infer<typeof sessionSchema>;
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
export type AuthData = z.infer<typeof authDataSchema>;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import {
|
||||
boolean,
|
||||
decimal,
|
||||
@@ -6,6 +7,7 @@ import {
|
||||
timestamp,
|
||||
pgEnum,
|
||||
uniqueIndex,
|
||||
numeric,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
export const users = pgTable(
|
||||
@@ -63,5 +65,41 @@ export const plaidAccessTokens = pgTable("plaidAccessToken", {
|
||||
logoUrl: text("logoUrl").notNull(),
|
||||
userId: text("user_id").notNull(),
|
||||
token: text("token").notNull(),
|
||||
syncCursor: text("sync_cursor"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const budget = pgTable("budget", {
|
||||
id: text("id").primaryKey(),
|
||||
orgId: text("org_id").notNull(),
|
||||
label: text("label").notNull(),
|
||||
createdBy: text("created_by").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const category = pgTable("category", {
|
||||
id: text("id").primaryKey(),
|
||||
budgetId: text("budget_id").notNull(),
|
||||
amount: decimal("amount").notNull(),
|
||||
every: text("every", { enum: ["year", "month", "week"] }).notNull(),
|
||||
order: numeric("order").notNull(),
|
||||
label: text("label").notNull(),
|
||||
color: text("color").notNull(),
|
||||
createdBy: text("created_by").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
removedBy: text("removed_by"),
|
||||
removedAt: timestamp("removed_at"),
|
||||
});
|
||||
|
||||
export const budgetRelations = relations(budget, ({ many }) => ({
|
||||
categories: many(category),
|
||||
}));
|
||||
|
||||
export const categoryRelations = relations(category, ({ one }) => ({
|
||||
budget: one(budget, {
|
||||
fields: [category.budgetId],
|
||||
references: [budget.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Transaction } from "@rocicorp/zero";
|
||||
import type { AuthData } from "./auth";
|
||||
import { type Schema } from "./zero-schema.gen";
|
||||
import { authDataSchema, type AuthData } from "./auth";
|
||||
import { type Category, type Schema } from "./zero-schema.gen";
|
||||
import { isLoggedIn } from "./zql";
|
||||
|
||||
type Tx = Transaction<Schema>;
|
||||
@@ -10,8 +10,10 @@ export function createMutators(authData: AuthData | null) {
|
||||
link: {
|
||||
async create() {},
|
||||
async get(tx: Tx, { link_token }: { link_token: string }) {},
|
||||
async updateTransactions() {},
|
||||
async updateBalences() {},
|
||||
async webhook() {},
|
||||
async sync() {},
|
||||
// async updateTransactions() {},
|
||||
// async updateBalences() {},
|
||||
async deleteAccounts(tx: Tx, { accountIds }: { accountIds: string[] }) {
|
||||
isLoggedIn(authData);
|
||||
for (const id of accountIds) {
|
||||
@@ -39,6 +41,114 @@ export function createMutators(authData: AuthData | null) {
|
||||
}
|
||||
},
|
||||
},
|
||||
budget: {
|
||||
async create(
|
||||
tx: Tx,
|
||||
{ id, categoryId }: { id: string; categoryId: string },
|
||||
) {
|
||||
isLoggedIn(authData);
|
||||
await tx.mutate.budget.insert({
|
||||
id,
|
||||
orgId: authData.user.id,
|
||||
label: "New Budget",
|
||||
createdBy: authData.user.id,
|
||||
});
|
||||
await tx.mutate.category.insert({
|
||||
id: categoryId,
|
||||
budgetId: id,
|
||||
amount: 0,
|
||||
every: "week",
|
||||
order: 1000,
|
||||
label: "My category",
|
||||
color: "#f06",
|
||||
createdBy: authData.user.id,
|
||||
});
|
||||
},
|
||||
async delete(tx: Tx, { id }: { id: string }) {
|
||||
isLoggedIn(authData);
|
||||
await tx.mutate.budget.delete({
|
||||
id,
|
||||
});
|
||||
},
|
||||
async createCategory(
|
||||
tx: Tx,
|
||||
{
|
||||
id,
|
||||
budgetId,
|
||||
order,
|
||||
}: { id: string; budgetId: string; order: number },
|
||||
) {
|
||||
isLoggedIn(authData);
|
||||
|
||||
if (order != undefined) {
|
||||
const after = await tx.query.category
|
||||
.where("budgetId", "=", budgetId)
|
||||
.where("order", ">", order);
|
||||
|
||||
after.forEach((item) => {
|
||||
tx.mutate.category.update({
|
||||
id: item.id,
|
||||
order: item.order + 1,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
tx.mutate.category.insert({
|
||||
id,
|
||||
budgetId,
|
||||
amount: 0,
|
||||
every: "week",
|
||||
order: order + 1,
|
||||
label: "My category",
|
||||
color: "#f06",
|
||||
createdBy: authData.user.id,
|
||||
});
|
||||
},
|
||||
async deleteCategory(tx: Tx, { id }: { id: string }) {
|
||||
isLoggedIn(authData);
|
||||
const item = await tx.query.category.where("id", "=", id).one();
|
||||
if (!item) throw Error("Item does not exist");
|
||||
tx.mutate.category.update({
|
||||
id,
|
||||
removedAt: new Date().getTime(),
|
||||
removedBy: authData.user.id,
|
||||
});
|
||||
const after = await tx.query.category
|
||||
.where("budgetId", "=", item.budgetId)
|
||||
.where("order", ">", item.order)
|
||||
.run();
|
||||
for (const item of after) {
|
||||
tx.mutate.category.update({ id: item.id, order: item.order - 1 });
|
||||
}
|
||||
// after.forEach((item) => {
|
||||
// });
|
||||
},
|
||||
async updateCategory(
|
||||
tx: Tx,
|
||||
{
|
||||
id,
|
||||
label,
|
||||
order,
|
||||
amount,
|
||||
every,
|
||||
}: {
|
||||
id: string;
|
||||
label?: string;
|
||||
order?: number;
|
||||
amount?: number;
|
||||
every?: Category["every"];
|
||||
},
|
||||
) {
|
||||
isLoggedIn(authData);
|
||||
tx.mutate.category.update({
|
||||
id,
|
||||
label,
|
||||
order,
|
||||
amount,
|
||||
every,
|
||||
});
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,4 +60,24 @@ export const queries = {
|
||||
.orderBy("createdAt", "desc");
|
||||
},
|
||||
),
|
||||
getBudgets: syncedQueryWithContext(
|
||||
"getBudgets",
|
||||
z.tuple([]),
|
||||
(authData: AuthData | null) => {
|
||||
isLoggedIn(authData);
|
||||
return builder.budget
|
||||
.related("categories", (q) =>
|
||||
q.where("removedAt", "IS", null).orderBy("order", "asc"),
|
||||
)
|
||||
.limit(10);
|
||||
},
|
||||
),
|
||||
getBudgetCategories: syncedQueryWithContext(
|
||||
"getBudgetCategories",
|
||||
z.tuple([]),
|
||||
(authData: AuthData | null) => {
|
||||
isLoggedIn(authData);
|
||||
return builder.category.orderBy("order", "desc");
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
@@ -112,6 +112,190 @@ export const schema = {
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
budget: {
|
||||
name: "budget",
|
||||
columns: {
|
||||
id: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"id"
|
||||
>,
|
||||
},
|
||||
orgId: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"orgId"
|
||||
>,
|
||||
serverName: "org_id",
|
||||
},
|
||||
label: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"label"
|
||||
>,
|
||||
},
|
||||
createdBy: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"createdBy"
|
||||
>,
|
||||
serverName: "created_by",
|
||||
},
|
||||
createdAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"createdAt"
|
||||
>,
|
||||
serverName: "created_at",
|
||||
},
|
||||
updatedAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"budget",
|
||||
"updatedAt"
|
||||
>,
|
||||
serverName: "updated_at",
|
||||
},
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
category: {
|
||||
name: "category",
|
||||
columns: {
|
||||
id: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"id"
|
||||
>,
|
||||
},
|
||||
budgetId: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"budgetId"
|
||||
>,
|
||||
serverName: "budget_id",
|
||||
},
|
||||
amount: {
|
||||
type: "number",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"amount"
|
||||
>,
|
||||
},
|
||||
every: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"every"
|
||||
>,
|
||||
},
|
||||
order: {
|
||||
type: "number",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"order"
|
||||
>,
|
||||
},
|
||||
label: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"label"
|
||||
>,
|
||||
},
|
||||
color: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"color"
|
||||
>,
|
||||
},
|
||||
createdBy: {
|
||||
type: "string",
|
||||
optional: false,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"createdBy"
|
||||
>,
|
||||
serverName: "created_by",
|
||||
},
|
||||
createdAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"createdAt"
|
||||
>,
|
||||
serverName: "created_at",
|
||||
},
|
||||
updatedAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"updatedAt"
|
||||
>,
|
||||
serverName: "updated_at",
|
||||
},
|
||||
removedBy: {
|
||||
type: "string",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"removedBy"
|
||||
>,
|
||||
serverName: "removed_by",
|
||||
},
|
||||
removedAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"category",
|
||||
"removedAt"
|
||||
>,
|
||||
serverName: "removed_at",
|
||||
},
|
||||
},
|
||||
primaryKey: ["id"],
|
||||
},
|
||||
plaidAccessTokens: {
|
||||
name: "plaidAccessTokens",
|
||||
columns: {
|
||||
@@ -161,6 +345,16 @@ export const schema = {
|
||||
"token"
|
||||
>,
|
||||
},
|
||||
syncCursor: {
|
||||
type: "string",
|
||||
optional: true,
|
||||
customType: null as unknown as ZeroCustomType<
|
||||
ZeroSchema,
|
||||
"plaidAccessTokens",
|
||||
"syncCursor"
|
||||
>,
|
||||
serverName: "sync_cursor",
|
||||
},
|
||||
createdAt: {
|
||||
type: "number",
|
||||
optional: true,
|
||||
@@ -418,7 +612,28 @@ export const schema = {
|
||||
serverName: "user",
|
||||
},
|
||||
},
|
||||
relationships: {},
|
||||
relationships: {
|
||||
budget: {
|
||||
categories: [
|
||||
{
|
||||
sourceField: ["id"],
|
||||
destField: ["budgetId"],
|
||||
destSchema: "category",
|
||||
cardinality: "many",
|
||||
},
|
||||
],
|
||||
},
|
||||
category: {
|
||||
budget: [
|
||||
{
|
||||
sourceField: ["budgetId"],
|
||||
destField: ["id"],
|
||||
destSchema: "budget",
|
||||
cardinality: "one",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
enableLegacyQueries: false,
|
||||
enableLegacyMutators: false,
|
||||
} as const;
|
||||
@@ -433,6 +648,16 @@ export type Schema = typeof schema;
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Balance = Row<Schema["tables"]["balance"]>;
|
||||
/**
|
||||
* Represents a row from the "budget" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Budget = Row<Schema["tables"]["budget"]>;
|
||||
/**
|
||||
* Represents a row from the "category" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
*/
|
||||
export type Category = Row<Schema["tables"]["category"]>;
|
||||
/**
|
||||
* Represents a row from the "plaidAccessTokens" table.
|
||||
* This type is auto-generated from your Drizzle schema definition.
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { useKeyboard } from "../src/useKeyboard";
|
||||
import type { ReactNode } from "react";
|
||||
import { useEffect, type ReactNode } from "react";
|
||||
import { Text, Pressable } from "react-native";
|
||||
import { useShortcut, type Key } from "../lib/shortcuts";
|
||||
|
||||
type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
|
||||
|
||||
export interface ButtonProps {
|
||||
children: ReactNode;
|
||||
onPress?: () => void;
|
||||
variant?: "default" | "secondary" | "destructive";
|
||||
shortcut?: string;
|
||||
shortcut?: Key;
|
||||
}
|
||||
|
||||
const STYLES: Record<
|
||||
@@ -21,10 +23,9 @@ const STYLES: Record<
|
||||
export function Button({ children, variant, onPress, shortcut }: ButtonProps) {
|
||||
const { backgroundColor, color } = STYLES[variant || "default"];
|
||||
|
||||
useKeyboard((key) => {
|
||||
if (!shortcut || !onPress) return;
|
||||
if (key.name == shortcut) onPress();
|
||||
});
|
||||
if (shortcut && onPress) {
|
||||
useShortcut(shortcut, onPress);
|
||||
}
|
||||
|
||||
return (
|
||||
<Pressable onPress={onPress} style={{ backgroundColor }}>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createContext, type ReactNode } from "react";
|
||||
import { createContext, use, type ReactNode } from "react";
|
||||
import { Modal, View, Text } from "react-native";
|
||||
import { useKeyboard } from "../src/useKeyboard";
|
||||
import { useShortcut } from "../lib/shortcuts";
|
||||
|
||||
export interface DialogState {
|
||||
close?: () => void;
|
||||
@@ -15,19 +15,13 @@ interface ProviderProps {
|
||||
close?: () => void;
|
||||
}
|
||||
export function Provider({ children, visible, close }: ProviderProps) {
|
||||
useKeyboard((key) => {
|
||||
if (key.name == "escape") {
|
||||
if (close) close();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Context.Provider value={{ close }}>
|
||||
<Modal transparent visible={visible}>
|
||||
{/* <Pressable onPress={() => close && close()} style={{ justifyContent: 'center', alignItems: 'center', flex: 1, backgroundColor: 'rgba(0,0,0,0.2)', }}> */}
|
||||
<View
|
||||
style={{
|
||||
justifyContent: "center",
|
||||
// justifyContent: "center",
|
||||
alignItems: "center",
|
||||
flex: 1,
|
||||
backgroundColor: "rgba(0,0,0,0.2)",
|
||||
@@ -44,10 +38,11 @@ interface ContentProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
export function Content({ children }: ContentProps) {
|
||||
const { close } = use(Context);
|
||||
useShortcut("escape", () => close?.(), "dialog");
|
||||
|
||||
return (
|
||||
<View
|
||||
style={{ backgroundColor: "white", padding: 12, alignItems: "center" }}
|
||||
>
|
||||
<View style={{ backgroundColor: "white", alignItems: "center", top: 120 }}>
|
||||
{children}
|
||||
</View>
|
||||
);
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { createContext, use, useState, type ReactNode } from "react";
|
||||
import { createContext, use, useEffect, useState, type ReactNode } from "react";
|
||||
import { View, Text } from "react-native";
|
||||
import { useKeyboard } from "../src/useKeyboard";
|
||||
import type { KeyEvent } from "@opentui/core";
|
||||
import { useShortcut } from "../lib/shortcuts/hooks";
|
||||
import type { Key } from "../lib/shortcuts";
|
||||
|
||||
const HEADER_COLOR = "#7158e2";
|
||||
const TABLE_COLORS = ["#ddd", "#eee"];
|
||||
const SELECTED_COLOR = "#f7b730";
|
||||
|
||||
const COLORS = {
|
||||
focused: "#ddd",
|
||||
selected: "#eaebf6",
|
||||
focused_selected: "#d5d7ef",
|
||||
};
|
||||
|
||||
const EXTRA = 5;
|
||||
|
||||
@@ -16,7 +20,7 @@ interface TableState {
|
||||
columns: Column[];
|
||||
columnMap: Map<string, number>;
|
||||
idx: number;
|
||||
selectedFrom: number | undefined;
|
||||
selectedIdx: Set<number>;
|
||||
}
|
||||
|
||||
const INITAL_STATE = {
|
||||
@@ -24,7 +28,7 @@ const INITAL_STATE = {
|
||||
columns: [],
|
||||
columnMap: new Map(),
|
||||
idx: 0,
|
||||
selectedFrom: undefined,
|
||||
selectedIdx: new Set(),
|
||||
} satisfies TableState;
|
||||
|
||||
export const Context = createContext<TableState>(INITAL_STATE);
|
||||
@@ -43,48 +47,64 @@ function renderCell(row: ValidRecord, column: Column): string {
|
||||
return cell.toString();
|
||||
}
|
||||
|
||||
interface TableShortcut<T> {
|
||||
key: Key;
|
||||
handler: (params: { selected: T[]; index: number }) => void;
|
||||
}
|
||||
|
||||
export interface ProviderProps<T> {
|
||||
data: T[];
|
||||
columns: Column[];
|
||||
children: ReactNode;
|
||||
onKey?: (event: KeyEvent, selected: T[]) => void;
|
||||
shortcuts?: TableShortcut<T>[];
|
||||
}
|
||||
export function Provider<T extends ValidRecord>({
|
||||
data,
|
||||
columns,
|
||||
children,
|
||||
onKey,
|
||||
shortcuts,
|
||||
}: ProviderProps<T>) {
|
||||
const [idx, setIdx] = useState(0);
|
||||
const [selectedFrom, setSelectedFrom] = useState<number>();
|
||||
const [selectedIdx, setSelectedIdx] = useState(new Set<number>());
|
||||
|
||||
useKeyboard(
|
||||
(key) => {
|
||||
if (key.name == "j" || key.name == "down") {
|
||||
if (key.shift && selectedFrom == undefined) {
|
||||
setSelectedFrom(idx);
|
||||
}
|
||||
setIdx((prev) => Math.min(prev + 1, data.length - 1));
|
||||
} else if (key.name == "k" || key.name == "up") {
|
||||
if (key.shift && selectedFrom == undefined) {
|
||||
setSelectedFrom(idx);
|
||||
}
|
||||
setIdx((prev) => Math.max(prev - 1, 0));
|
||||
} else if (key.name == "g" && key.shift) {
|
||||
setIdx(data.length - 1);
|
||||
} else if (key.name == "v") {
|
||||
setSelectedFrom(idx);
|
||||
} else if (key.name == "escape") {
|
||||
setSelectedFrom(undefined);
|
||||
} else {
|
||||
const from = selectedFrom ? Math.min(idx, selectedFrom) : idx;
|
||||
const to = selectedFrom ? Math.max(idx, selectedFrom) : idx;
|
||||
const selected = data.slice(from, to + 1);
|
||||
if (onKey) onKey(key, selected);
|
||||
}
|
||||
},
|
||||
[data, idx, selectedFrom],
|
||||
);
|
||||
useShortcut("j", () => {
|
||||
setIdx((prev) => Math.min(prev + 1, data.length - 1));
|
||||
});
|
||||
useShortcut("down", () => {
|
||||
setIdx((prev) => Math.min(prev + 1, data.length - 1));
|
||||
});
|
||||
useShortcut("k", () => {
|
||||
setIdx((prev) => Math.max(prev - 1, 0));
|
||||
});
|
||||
useShortcut("up", () => {
|
||||
setIdx((prev) => Math.max(prev - 1, 0));
|
||||
});
|
||||
|
||||
useShortcut("escape", () => {
|
||||
setSelectedIdx(new Set());
|
||||
});
|
||||
useShortcut("x", () => {
|
||||
setSelectedIdx((last) => {
|
||||
const newSelected = new Set(last);
|
||||
newSelected.add(idx);
|
||||
return newSelected;
|
||||
});
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
setIdx((prev) => Math.max(Math.min(prev, data.length - 1), 0));
|
||||
}, [data]);
|
||||
|
||||
if (shortcuts) {
|
||||
for (const shortcut of shortcuts) {
|
||||
useShortcut(shortcut.key, () => {
|
||||
const selected = data.filter(
|
||||
(_, index) => idx == index || selectedIdx.has(index),
|
||||
);
|
||||
shortcut.handler({ selected, index: idx });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const columnMap = new Map(
|
||||
columns.map((col) => {
|
||||
@@ -99,14 +119,14 @@ export function Provider<T extends ValidRecord>({
|
||||
);
|
||||
|
||||
return (
|
||||
<Context.Provider value={{ data, columns, columnMap, idx, selectedFrom }}>
|
||||
<Context.Provider value={{ data, columns, columnMap, idx, selectedIdx }}>
|
||||
{children}
|
||||
</Context.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function Body() {
|
||||
const { columns, data, columnMap, idx, selectedFrom } = use(Context);
|
||||
const { columns, data, columnMap, idx, selectedIdx } = use(Context);
|
||||
return (
|
||||
<View>
|
||||
<View style={{ backgroundColor: HEADER_COLOR, flexDirection: "row" }}>
|
||||
@@ -123,19 +143,21 @@ export function Body() {
|
||||
))}
|
||||
</View>
|
||||
{data.map((row, index) => {
|
||||
const isSelected =
|
||||
index == idx ||
|
||||
(selectedFrom != undefined &&
|
||||
((selectedFrom <= index && index <= idx) ||
|
||||
(idx <= index && index <= selectedFrom)));
|
||||
const isSelected = selectedIdx.has(index);
|
||||
const isFocused = index == idx;
|
||||
|
||||
return (
|
||||
<View
|
||||
key={index}
|
||||
style={{
|
||||
backgroundColor: isSelected
|
||||
? SELECTED_COLOR
|
||||
: TABLE_COLORS[index % 2],
|
||||
backgroundColor:
|
||||
isSelected && isFocused
|
||||
? COLORS.focused_selected
|
||||
: isFocused
|
||||
? COLORS.focused
|
||||
: isSelected
|
||||
? COLORS.selected
|
||||
: undefined,
|
||||
}}
|
||||
>
|
||||
<TableRow
|
||||
|
||||
41
packages/ui/lib/shortcuts/Debug.tsx
Normal file
41
packages/ui/lib/shortcuts/Debug.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { View, Text } from "react-native";
|
||||
import { keysStore, type ScopeKeys } from "./store";
|
||||
|
||||
export function ShortcutDebug() {
|
||||
const entries = useSyncExternalStore(
|
||||
keysStore.subscribe,
|
||||
keysStore.getSnapshot,
|
||||
);
|
||||
|
||||
return (
|
||||
<View
|
||||
style={{
|
||||
position: "absolute",
|
||||
zIndex: 100,
|
||||
bottom: 0,
|
||||
right: 0,
|
||||
backgroundColor: "black",
|
||||
padding: 10,
|
||||
}}
|
||||
>
|
||||
<Text style={{ color: "red", fontFamily: "mono" }}>Scopes:</Text>
|
||||
{entries.map(([scope, keys]) => (
|
||||
<ScopeView key={scope} scope={scope} keys={keys} />
|
||||
))}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function ScopeView({ scope, keys }: { scope: string; keys: ScopeKeys }) {
|
||||
return (
|
||||
<Text style={{ color: "red", fontFamily: "mono", textAlign: "right" }}>
|
||||
{scope}:
|
||||
{keys
|
||||
.entries()
|
||||
.map(([key, _]) => key)
|
||||
.toArray()
|
||||
.join(",")}
|
||||
</Text>
|
||||
);
|
||||
}
|
||||
12
packages/ui/lib/shortcuts/Provider.tsx
Normal file
12
packages/ui/lib/shortcuts/Provider.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useKeyboard } from "@opentui/react";
|
||||
import { keysStore } from "./store";
|
||||
|
||||
export function ShortcutProvider({ children }: { children: ReactNode }) {
|
||||
useKeyboard((e) => {
|
||||
const fn = keysStore.getHandler(e.name);
|
||||
fn?.();
|
||||
});
|
||||
|
||||
return children;
|
||||
}
|
||||
26
packages/ui/lib/shortcuts/Provider.web.tsx
Normal file
26
packages/ui/lib/shortcuts/Provider.web.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { keysStore } from "./store";
|
||||
import type { KeyName } from "./types";
|
||||
|
||||
const KEY_MAP: { [k: string]: KeyName } = {
|
||||
Escape: "escape",
|
||||
ArrowUp: "up",
|
||||
ArrowDown: "down",
|
||||
ArrowLeft: "left",
|
||||
ArrowRight: "right",
|
||||
};
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("keydown", (e) => {
|
||||
const key = Object.hasOwn(KEY_MAP, e.key) ? KEY_MAP[e.key]! : e.key;
|
||||
const fn = keysStore.getHandler(key);
|
||||
// console.log(e.key);
|
||||
if (!fn) return;
|
||||
e.preventDefault();
|
||||
fn();
|
||||
});
|
||||
}
|
||||
|
||||
export function ShortcutProvider({ children }: { children: ReactNode }) {
|
||||
return children;
|
||||
}
|
||||
22
packages/ui/lib/shortcuts/hooks.ts
Normal file
22
packages/ui/lib/shortcuts/hooks.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { keysStore } from "./store";
|
||||
import type { Key } from "./types";
|
||||
import { enforceKeyOptions } from "./util";
|
||||
|
||||
export const useShortcut = (
|
||||
key: Key,
|
||||
handler: () => void,
|
||||
scope: string = "global",
|
||||
) => {
|
||||
const keyOptions = enforceKeyOptions(key);
|
||||
const keyName = keyOptions.name;
|
||||
const ref = useRef(handler);
|
||||
ref.current = handler;
|
||||
|
||||
useEffect(() => {
|
||||
keysStore.register(keyName, ref, scope);
|
||||
return () => {
|
||||
keysStore.deregister(keyName, scope);
|
||||
};
|
||||
}, []);
|
||||
};
|
||||
4
packages/ui/lib/shortcuts/index.ts
Normal file
4
packages/ui/lib/shortcuts/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./Debug";
|
||||
export * from "./Provider";
|
||||
export * from "./hooks";
|
||||
export * from "./types";
|
||||
58
packages/ui/lib/shortcuts/store.ts
Normal file
58
packages/ui/lib/shortcuts/store.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { type RefObject } from "react";
|
||||
|
||||
export type ScopeKeys = Map<string, RefObject<() => void>>;
|
||||
|
||||
// outer reactive container
|
||||
const scopes = new Map<string, ScopeKeys>();
|
||||
|
||||
// stable snapshot for subscribers
|
||||
let snapshot: [string, ScopeKeys][] = [];
|
||||
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function emit() {
|
||||
// replace identity so subscribers re-render
|
||||
snapshot = Array.from(scopes.entries());
|
||||
for (const fn of listeners) fn();
|
||||
}
|
||||
|
||||
export const keysStore = {
|
||||
subscribe(fn: () => void) {
|
||||
listeners.add(fn);
|
||||
return () => listeners.delete(fn);
|
||||
},
|
||||
|
||||
getSnapshot() {
|
||||
return snapshot;
|
||||
},
|
||||
|
||||
register(key: string, ref: RefObject<() => void>, scope: string) {
|
||||
const prev = scopes.get(scope);
|
||||
const next = new Map(prev); // <-- important: new identity
|
||||
next.set(key, ref);
|
||||
|
||||
scopes.set(scope, next); // <-- outer identity also changes
|
||||
emit();
|
||||
},
|
||||
|
||||
deregister(key: string, scope: string) {
|
||||
const prev = scopes.get(scope);
|
||||
if (!prev) return;
|
||||
|
||||
const next = new Map(prev);
|
||||
next.delete(key);
|
||||
|
||||
if (next.size === 0) {
|
||||
scopes.delete(scope);
|
||||
} else {
|
||||
scopes.set(scope, next);
|
||||
}
|
||||
emit();
|
||||
},
|
||||
|
||||
getHandler(key: string) {
|
||||
// last scope wins — clarify this logic as needed
|
||||
const last = Array.from(scopes.values()).at(-1);
|
||||
return last?.get(key)?.current;
|
||||
},
|
||||
};
|
||||
52
packages/ui/lib/shortcuts/types.ts
Normal file
52
packages/ui/lib/shortcuts/types.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
export type KeyName =
|
||||
| "0"
|
||||
| "1"
|
||||
| "2"
|
||||
| "3"
|
||||
| "4"
|
||||
| "5"
|
||||
| "6"
|
||||
| "7"
|
||||
| "8"
|
||||
| "9"
|
||||
| "a"
|
||||
| "b"
|
||||
| "c"
|
||||
| "d"
|
||||
| "e"
|
||||
| "f"
|
||||
| "g"
|
||||
| "h"
|
||||
| "i"
|
||||
| "j"
|
||||
| "k"
|
||||
| "l"
|
||||
| "m"
|
||||
| "n"
|
||||
| "o"
|
||||
| "p"
|
||||
| "q"
|
||||
| "r"
|
||||
| "s"
|
||||
| "t"
|
||||
| "u"
|
||||
| "v"
|
||||
| "w"
|
||||
| "x"
|
||||
| "y"
|
||||
| "z"
|
||||
| ":"
|
||||
| "up"
|
||||
| "down"
|
||||
| "left"
|
||||
| "right"
|
||||
| "return"
|
||||
| "escape";
|
||||
|
||||
export type Key = KeyName | KeyOptions;
|
||||
|
||||
export interface KeyOptions {
|
||||
name: KeyName;
|
||||
ctrl?: boolean;
|
||||
shift?: boolean;
|
||||
}
|
||||
9
packages/ui/lib/shortcuts/util.ts
Normal file
9
packages/ui/lib/shortcuts/util.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Key, KeyOptions } from "./types";
|
||||
|
||||
export function enforceKeyOptions(key: Key): KeyOptions {
|
||||
return typeof key == "string"
|
||||
? {
|
||||
name: key,
|
||||
}
|
||||
: key;
|
||||
}
|
||||
160
packages/ui/src/budget.tsx
Normal file
160
packages/ui/src/budget.tsx
Normal file
@@ -0,0 +1,160 @@
|
||||
import { use, useRef, useState } from "react";
|
||||
import { View, Text, TextInput } from "react-native";
|
||||
import { RouterContext } from ".";
|
||||
import {
|
||||
queries,
|
||||
type Category,
|
||||
type Mutators,
|
||||
type Schema,
|
||||
} from "@money/shared";
|
||||
import { useQuery, useZero } from "@rocicorp/zero/react";
|
||||
import * as Table from "../components/Table";
|
||||
import { Button } from "../components/Button";
|
||||
import { RenameCategoryDialog } from "./budget/RenameCategoryDialog";
|
||||
import {
|
||||
UpdateCategoryAmountDialog,
|
||||
type CategoryWithComputed,
|
||||
type Updating,
|
||||
} from "./budget/UpdateCategoryAmountDialog";
|
||||
|
||||
const COLUMNS: Table.Column[] = [
|
||||
{ name: "label", label: "Name" },
|
||||
{ name: "week", label: "Week" },
|
||||
{ name: "month", label: "Month" },
|
||||
{ name: "year", label: "Year" },
|
||||
{ name: "order", label: "Order" },
|
||||
];
|
||||
|
||||
export function Budget() {
|
||||
const { auth } = use(RouterContext);
|
||||
const [budgets] = useQuery(queries.getBudgets(auth));
|
||||
const [renaming, setRenaming] = useState<Category>();
|
||||
const [editCategoryAmount, setEditCategoryAmount] = useState<Updating>();
|
||||
|
||||
const z = useZero<Schema, Mutators>();
|
||||
|
||||
const newBudget = () => {
|
||||
const id = new Date().getTime().toString();
|
||||
const categoryId = new Date().getTime().toString();
|
||||
z.mutate.budget.create({
|
||||
id,
|
||||
categoryId,
|
||||
});
|
||||
};
|
||||
|
||||
if (budgets.length == 0)
|
||||
return (
|
||||
<View
|
||||
style={{
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
flex: 1,
|
||||
gap: 10,
|
||||
}}
|
||||
>
|
||||
<Text style={{ fontFamily: "mono" }}>
|
||||
No budgets, please create a new budget
|
||||
</Text>
|
||||
<Button onPress={newBudget} shortcut="n">
|
||||
New budget
|
||||
</Button>
|
||||
</View>
|
||||
);
|
||||
|
||||
const budget = budgets[0]!;
|
||||
|
||||
const data = budget.categories.slice().map((category) => {
|
||||
const { amount } = category;
|
||||
const week = amount / 4;
|
||||
const month = amount;
|
||||
const year = amount * 12;
|
||||
|
||||
return {
|
||||
...category,
|
||||
...{
|
||||
week,
|
||||
month,
|
||||
year,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const newCategory = ({ index }: { index: number }) => {
|
||||
const id = new Date().getTime().toString();
|
||||
z.mutate.budget.createCategory({
|
||||
id,
|
||||
budgetId: budget.id,
|
||||
order: index,
|
||||
});
|
||||
};
|
||||
|
||||
const deleteCategory = ({ selected }: { selected: { id: string }[] }) => {
|
||||
for (const { id } of selected) {
|
||||
z.mutate.budget.deleteCategory({ id });
|
||||
}
|
||||
};
|
||||
|
||||
const renameCategory = ({ selected }: { selected: Category[] }) => {
|
||||
for (const category of selected) {
|
||||
setRenaming(category);
|
||||
}
|
||||
};
|
||||
|
||||
const onEditCategoryYearly = ({
|
||||
selected,
|
||||
}: { selected: CategoryWithComputed[] }) => {
|
||||
for (const category of selected) {
|
||||
setEditCategoryAmount({ category, every: "year" });
|
||||
}
|
||||
};
|
||||
|
||||
const onEditCategoryMonthly = ({
|
||||
selected,
|
||||
}: { selected: CategoryWithComputed[] }) => {
|
||||
for (const category of selected) {
|
||||
setEditCategoryAmount({ category, every: "month" });
|
||||
}
|
||||
};
|
||||
|
||||
const onEditCategoryWeekly = ({
|
||||
selected,
|
||||
}: { selected: CategoryWithComputed[] }) => {
|
||||
for (const category of selected) {
|
||||
setEditCategoryAmount({ category, every: "week" });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<RenameCategoryDialog renaming={renaming} setRenaming={setRenaming} />
|
||||
<UpdateCategoryAmountDialog
|
||||
updating={editCategoryAmount}
|
||||
setUpdating={setEditCategoryAmount}
|
||||
/>
|
||||
|
||||
<View style={{ alignItems: "flex-start" }}>
|
||||
<Text style={{ fontFamily: "mono", textAlign: "left" }}>
|
||||
Selected Budget: {budget.label}
|
||||
</Text>
|
||||
</View>
|
||||
<Table.Provider
|
||||
data={data}
|
||||
columns={COLUMNS}
|
||||
shortcuts={[
|
||||
{ key: "i", handler: newCategory },
|
||||
{ key: "d", handler: deleteCategory },
|
||||
{ key: "r", handler: renameCategory },
|
||||
{ key: "y", handler: onEditCategoryYearly },
|
||||
{ key: "m", handler: onEditCategoryMonthly },
|
||||
{ key: "w", handler: onEditCategoryWeekly },
|
||||
]}
|
||||
>
|
||||
<View style={{ flex: 1 }}>
|
||||
<View style={{ flexShrink: 0 }}>
|
||||
<Table.Body />
|
||||
</View>
|
||||
</View>
|
||||
</Table.Provider>
|
||||
</>
|
||||
);
|
||||
}
|
||||
77
packages/ui/src/budget/RenameCategoryDialog.tsx
Normal file
77
packages/ui/src/budget/RenameCategoryDialog.tsx
Normal file
@@ -0,0 +1,77 @@
|
||||
import { useRef, useState } from "react";
|
||||
import * as Dialog from "../../components/Dialog";
|
||||
import { View, Text, TextInput } from "react-native";
|
||||
import { type Category, type Mutators, type Schema } from "@money/shared";
|
||||
import { useZero } from "@rocicorp/zero/react";
|
||||
|
||||
interface RenameCategoryDialogProps {
|
||||
renaming: Category | undefined;
|
||||
setRenaming: (v: Category | undefined) => void;
|
||||
}
|
||||
export function RenameCategoryDialog({
|
||||
renaming,
|
||||
setRenaming,
|
||||
}: RenameCategoryDialogProps) {
|
||||
const refText = useRef("");
|
||||
const [renamingText, setRenamingText] = useState("");
|
||||
|
||||
const z = useZero<Schema, Mutators>();
|
||||
|
||||
return (
|
||||
<Dialog.Provider
|
||||
visible={renaming != undefined}
|
||||
close={() => setRenaming(undefined)}
|
||||
>
|
||||
<Dialog.Content>
|
||||
<View style={{ width: 400 }}>
|
||||
<View
|
||||
style={{
|
||||
borderBottomWidth: 1,
|
||||
paddingTop: 12,
|
||||
paddingLeft: 12,
|
||||
paddingRight: 12,
|
||||
}}
|
||||
>
|
||||
<TextInput
|
||||
style={{
|
||||
fontFamily: "mono",
|
||||
// @ts-ignore
|
||||
outline: "none",
|
||||
}}
|
||||
autoFocus
|
||||
selectTextOnFocus
|
||||
defaultValue={renaming?.label}
|
||||
onChangeText={(t) => {
|
||||
refText.current = t;
|
||||
setRenamingText(t);
|
||||
}}
|
||||
onKeyPress={(e) => {
|
||||
if (!renaming) return;
|
||||
if (e.nativeEvent.key == "Enter") {
|
||||
if (refText.current.trim() == "")
|
||||
return setRenaming(undefined);
|
||||
z.mutate.budget.updateCategory({
|
||||
id: renaming.id,
|
||||
label: refText.current,
|
||||
});
|
||||
setRenaming(undefined);
|
||||
} else if (e.nativeEvent.key == "Escape") {
|
||||
setRenaming(undefined);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View
|
||||
style={{ paddingLeft: 12, paddingRight: 12, paddingBottom: 12 }}
|
||||
>
|
||||
<Text style={{ fontFamily: "mono" }}>
|
||||
→ Rename category to: {renamingText || renaming?.label}
|
||||
</Text>
|
||||
<Text style={{ fontFamily: "mono" }}>→ Cancel</Text>
|
||||
</View>
|
||||
</View>
|
||||
</Dialog.Content>
|
||||
</Dialog.Provider>
|
||||
);
|
||||
}
|
||||
107
packages/ui/src/budget/UpdateCategoryAmountDialog.tsx
Normal file
107
packages/ui/src/budget/UpdateCategoryAmountDialog.tsx
Normal file
@@ -0,0 +1,107 @@
|
||||
import { useRef, useState } from "react";
|
||||
import * as Dialog from "../../components/Dialog";
|
||||
import { View, Text, TextInput } from "react-native";
|
||||
import { type Category, type Mutators, type Schema } from "@money/shared";
|
||||
import { useZero } from "@rocicorp/zero/react";
|
||||
|
||||
export type Updating = {
|
||||
category: CategoryWithComputed;
|
||||
every: Category["every"];
|
||||
};
|
||||
|
||||
export type CategoryWithComputed = Category & {
|
||||
month: number;
|
||||
year: number;
|
||||
};
|
||||
|
||||
interface UpdateCategoryAmountDialogProps {
|
||||
updating: Updating | undefined;
|
||||
setUpdating: (v: Updating | undefined) => void;
|
||||
}
|
||||
export function UpdateCategoryAmountDialog({
|
||||
updating,
|
||||
setUpdating,
|
||||
}: UpdateCategoryAmountDialogProps) {
|
||||
const category = updating?.category;
|
||||
const every = updating?.every;
|
||||
|
||||
const refText = useRef("");
|
||||
const [amountText, setAmountText] = useState("");
|
||||
|
||||
const z = useZero<Schema, Mutators>();
|
||||
|
||||
return (
|
||||
<Dialog.Provider
|
||||
visible={category != undefined}
|
||||
close={() => setUpdating(undefined)}
|
||||
>
|
||||
<Dialog.Content>
|
||||
<View style={{ width: 400 }}>
|
||||
<View
|
||||
style={{
|
||||
borderBottomWidth: 1,
|
||||
paddingTop: 12,
|
||||
paddingLeft: 12,
|
||||
paddingRight: 12,
|
||||
}}
|
||||
>
|
||||
<TextInput
|
||||
style={{
|
||||
fontFamily: "mono",
|
||||
// @ts-ignore
|
||||
outline: "none",
|
||||
}}
|
||||
autoFocus
|
||||
selectTextOnFocus
|
||||
defaultValue={category?.month.toString()}
|
||||
onChangeText={(t) => {
|
||||
refText.current = t;
|
||||
setAmountText(t);
|
||||
}}
|
||||
onKeyPress={(e) => {
|
||||
if (!category) return;
|
||||
if (e.nativeEvent.key == "Enter") {
|
||||
if (refText.current.trim() == "")
|
||||
return setUpdating(undefined);
|
||||
|
||||
try {
|
||||
const parsed = parseFloat(refText.current);
|
||||
|
||||
const amount = (function () {
|
||||
switch (every) {
|
||||
case "year":
|
||||
return parsed / 12;
|
||||
case "month":
|
||||
return parsed;
|
||||
case "week":
|
||||
return parsed * 4;
|
||||
}
|
||||
})();
|
||||
|
||||
z.mutate.budget.updateCategory({
|
||||
id: category.id,
|
||||
amount,
|
||||
every,
|
||||
});
|
||||
setUpdating(undefined);
|
||||
} catch (e) {}
|
||||
} else if (e.nativeEvent.key == "Escape") {
|
||||
setUpdating(undefined);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
|
||||
<View
|
||||
style={{ paddingLeft: 12, paddingRight: 12, paddingBottom: 12 }}
|
||||
>
|
||||
<Text style={{ fontFamily: "mono" }}>
|
||||
→ Update monthly amount to: {amountText || category?.month}
|
||||
</Text>
|
||||
<Text style={{ fontFamily: "mono" }}>→ Cancel</Text>
|
||||
</View>
|
||||
</View>
|
||||
</Dialog.Content>
|
||||
</Dialog.Provider>
|
||||
);
|
||||
}
|
||||
@@ -1,24 +1,37 @@
|
||||
import { createContext, use } from "react";
|
||||
import { createContext, use, type ReactNode } from "react";
|
||||
import { Transactions } from "./transactions";
|
||||
import { View, Text } from "react-native";
|
||||
import { View } from "react-native";
|
||||
import { Settings } from "./settings";
|
||||
import { useKeyboard } from "./useKeyboard";
|
||||
import type { AuthData } from "@money/shared/auth";
|
||||
import { Budget } from "./budget";
|
||||
import {
|
||||
ShortcutProvider,
|
||||
ShortcutDebug,
|
||||
useShortcut,
|
||||
type KeyName,
|
||||
} from "../lib/shortcuts";
|
||||
|
||||
const PAGES = {
|
||||
"/": {
|
||||
screen: <Transactions />,
|
||||
key: "1",
|
||||
},
|
||||
"/budget": {
|
||||
screen: <Budget />,
|
||||
key: "2",
|
||||
},
|
||||
"/settings": {
|
||||
screen: <Settings />,
|
||||
key: "2",
|
||||
key: "3",
|
||||
children: {
|
||||
"/accounts": {},
|
||||
"/family": {},
|
||||
},
|
||||
},
|
||||
};
|
||||
} satisfies Record<
|
||||
string,
|
||||
{ screen: ReactNode; key: KeyName; children?: Record<string, unknown> }
|
||||
>;
|
||||
|
||||
type Join<A extends string, B extends string> = `${A}${B}` extends `${infer X}`
|
||||
? X
|
||||
@@ -59,7 +72,10 @@ type AppProps = {
|
||||
export function App({ auth, route, setRoute }: AppProps) {
|
||||
return (
|
||||
<RouterContext.Provider value={{ auth, route, setRoute }}>
|
||||
<Main />
|
||||
<ShortcutProvider>
|
||||
<ShortcutDebug />
|
||||
<Main />
|
||||
</ShortcutProvider>
|
||||
</RouterContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -67,17 +83,9 @@ export function App({ auth, route, setRoute }: AppProps) {
|
||||
function Main() {
|
||||
const { route, setRoute } = use(RouterContext);
|
||||
|
||||
useKeyboard((key) => {
|
||||
const screen = Object.entries(PAGES).find(
|
||||
([, screen]) => screen.key == key.name,
|
||||
);
|
||||
|
||||
if (!screen) return;
|
||||
|
||||
const [route] = screen as [Route, never];
|
||||
|
||||
setRoute(route);
|
||||
});
|
||||
for (const [route, page] of Object.entries(PAGES)) {
|
||||
useShortcut(page.key, () => setRoute(route as Route));
|
||||
}
|
||||
|
||||
const match =
|
||||
route in PAGES
|
||||
|
||||
@@ -4,8 +4,7 @@ import { RouterContext, type Route } from ".";
|
||||
import { General } from "./settings/general";
|
||||
import { Accounts } from "./settings/accounts";
|
||||
import { Family } from "./settings/family";
|
||||
import { useKeyboard } from "./useKeyboard";
|
||||
import { Modal } from "react-native-opentui";
|
||||
import { useShortcut } from "../lib/shortcuts";
|
||||
|
||||
type SettingsRoute = Extract<Route, `/settings${string}`>;
|
||||
|
||||
@@ -32,28 +31,24 @@ type Tab = keyof typeof TABS;
|
||||
export function Settings() {
|
||||
const { route, setRoute } = use(RouterContext);
|
||||
|
||||
useKeyboard(
|
||||
(key) => {
|
||||
if (key.name == "h") {
|
||||
const currentIdx = Object.entries(TABS).findIndex(
|
||||
([tabRoute, _]) => tabRoute == route,
|
||||
);
|
||||
const routes = Object.keys(TABS) as SettingsRoute[];
|
||||
const last = routes[currentIdx - 1];
|
||||
if (!last) return;
|
||||
setRoute(last);
|
||||
} else if (key.name == "l") {
|
||||
const currentIdx = Object.entries(TABS).findIndex(
|
||||
([tabRoute, _]) => tabRoute == route,
|
||||
);
|
||||
const routes = Object.keys(TABS) as SettingsRoute[];
|
||||
const next = routes[currentIdx + 1];
|
||||
if (!next) return;
|
||||
setRoute(next);
|
||||
}
|
||||
},
|
||||
[route],
|
||||
);
|
||||
useShortcut("h", () => {
|
||||
const currentIdx = Object.entries(TABS).findIndex(
|
||||
([tabRoute, _]) => tabRoute == route,
|
||||
);
|
||||
const routes = Object.keys(TABS) as SettingsRoute[];
|
||||
const last = routes[currentIdx - 1];
|
||||
if (!last) return;
|
||||
setRoute(last);
|
||||
});
|
||||
useShortcut("l", () => {
|
||||
const currentIdx = Object.entries(TABS).findIndex(
|
||||
([tabRoute, _]) => tabRoute == route,
|
||||
);
|
||||
const routes = Object.keys(TABS) as SettingsRoute[];
|
||||
const next = routes[currentIdx + 1];
|
||||
if (!next) return;
|
||||
setRoute(next);
|
||||
});
|
||||
|
||||
return (
|
||||
<View style={{ flexDirection: "row" }}>
|
||||
|
||||
@@ -3,7 +3,6 @@ import { queries, type Mutators, type Schema } from "@money/shared";
|
||||
import { use, useEffect, useState } from "react";
|
||||
import { RouterContext } from "..";
|
||||
import { View, Text, Linking } from "react-native";
|
||||
import { useKeyboard } from "../useKeyboard";
|
||||
import { Button } from "../../components/Button";
|
||||
import * as Table from "../../components/Table";
|
||||
import * as Dialog from "../../components/Dialog";
|
||||
|
||||
@@ -40,13 +40,9 @@ export function Transactions() {
|
||||
<Table.Provider
|
||||
data={items}
|
||||
columns={COLUMNS}
|
||||
onKey={(key) => {
|
||||
if (key.name == "r" && key.shift) {
|
||||
z.mutate.link.updateTransactions();
|
||||
}
|
||||
}}
|
||||
shortcuts={[{ key: "r", handler: () => z.mutate.link.sync() }]}
|
||||
>
|
||||
<View style={{ flex: 1 }}>
|
||||
<View style={{ padding: 10, flex: 1 }}>
|
||||
<View style={{ flexShrink: 0 }}>
|
||||
<Table.Body />
|
||||
</View>
|
||||
@@ -59,18 +55,16 @@ export function Transactions() {
|
||||
}
|
||||
|
||||
function Selected() {
|
||||
const { data, idx, selectedFrom } = use(Table.Context);
|
||||
const { data, selectedIdx } = use(Table.Context);
|
||||
|
||||
if (selectedFrom == undefined)
|
||||
if (selectedIdx.size == 0)
|
||||
return (
|
||||
<View style={{ backgroundColor: "#ddd" }}>
|
||||
<Text style={{ fontFamily: "mono" }}>No items selected</Text>
|
||||
</View>
|
||||
);
|
||||
|
||||
const from = Math.min(idx, selectedFrom);
|
||||
const to = Math.max(idx, selectedFrom);
|
||||
const selected = data.slice(from, to + 1) as Transaction[];
|
||||
const selected = data.filter((_, i) => selectedIdx.has(i)) as Transaction[];
|
||||
const count = selected.length;
|
||||
const sum = selected.reduce((prev, curr) => prev + curr.amount, 0);
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { useKeyboard as useOpentuiKeyboard } from "@opentui/react";
|
||||
|
||||
export function useKeyboard(
|
||||
handler: Parameters<typeof useOpentuiKeyboard>[0],
|
||||
_deps: any[] = [],
|
||||
) {
|
||||
return useOpentuiKeyboard(handler);
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
import { useEffect } from "react";
|
||||
import type { KeyboardEvent } from "react";
|
||||
import type { KeyEvent } from "@opentui/core";
|
||||
|
||||
function convertName(keyName: string): string {
|
||||
const result = keyName.toLowerCase();
|
||||
if (result == "arrowdown") return "down";
|
||||
if (result == "arrowup") return "up";
|
||||
return result;
|
||||
}
|
||||
|
||||
export function useKeyboard(
|
||||
handler: (key: KeyEvent) => void,
|
||||
deps: any[] = [],
|
||||
) {
|
||||
useEffect(() => {
|
||||
const handlerWeb = (event: KeyboardEvent) => {
|
||||
// @ts-ignore
|
||||
handler({
|
||||
name: convertName(event.key),
|
||||
ctrl: event.ctrlKey,
|
||||
meta: event.metaKey,
|
||||
shift: event.shiftKey,
|
||||
option: event.metaKey,
|
||||
sequence: "",
|
||||
number: false,
|
||||
raw: "",
|
||||
eventType: "press",
|
||||
source: "raw",
|
||||
code: event.code,
|
||||
super: false,
|
||||
hyper: false,
|
||||
capsLock: false,
|
||||
numLock: false,
|
||||
baseCode: event.keyCode,
|
||||
preventDefault: () => event.preventDefault(),
|
||||
});
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
window.addEventListener("keydown", handlerWeb);
|
||||
return () => {
|
||||
// @ts-ignore
|
||||
window.removeEventListener("keydown", handlerWeb);
|
||||
};
|
||||
}, deps);
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
},
|
||||
// Environment setup & latest features
|
||||
"lib": ["ESNext"],
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
|
||||
16152
pnpm-lock.yaml
generated
16152
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,4 +0,0 @@
|
||||
nodeLinker: hoisted
|
||||
packages:
|
||||
- 'apps/*'
|
||||
- 'packages/*'
|
||||
@@ -26,16 +26,16 @@ processes:
|
||||
period_seconds: 1
|
||||
|
||||
tailscale_machine_name:
|
||||
command: "pnpm tsx ./scripts/set-machine-name.ts"
|
||||
command: "bun tsx ./scripts/set-machine-name.ts"
|
||||
|
||||
expo:
|
||||
command: "pnpm --filter=@money/expo start"
|
||||
command: "bun --filter=@money/expo start"
|
||||
depends_on:
|
||||
tailscale_machine_name:
|
||||
condition: process_completed_successfully
|
||||
|
||||
api:
|
||||
command: "pnpm --filter=@money/api dev"
|
||||
command: "bun --filter=@money/api dev"
|
||||
|
||||
migrate:
|
||||
command: |
|
||||
@@ -51,13 +51,13 @@ processes:
|
||||
db:
|
||||
condition: process_healthy
|
||||
zero:
|
||||
command: npx zero-cache-dev -p packages/shared/src/schema.ts
|
||||
command: bunx zero-cache-dev -p packages/shared/src/schema.ts
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: process_completed_successfully
|
||||
|
||||
studio:
|
||||
command: npx drizzle-kit studio
|
||||
command: bunx drizzle-kit studio
|
||||
working_dir: ./packages/shared
|
||||
depends_on:
|
||||
db:
|
||||
|
||||
Reference in New Issue
Block a user