diff --git a/apps/mobile/src/features/chats/widgets/KinMoneyInCheckCard.tsx b/apps/mobile/src/features/chats/widgets/KinMoneyInCheckCard.tsx new file mode 100644 index 000000000000..a7c468c15ff4 --- /dev/null +++ b/apps/mobile/src/features/chats/widgets/KinMoneyInCheckCard.tsx @@ -0,0 +1,54 @@ +import { memo, useState } from "react"; +import { View } from "react-native"; + +import { AppText as Text } from "../../../components/ui/AppText"; +import { Button } from "../../../components/ui/Button"; +import { + KIN_MONEY_IN_COPY, + kinMoneyInShown, + type KinMoneyInCheckView, +} from "@t3tools/client-runtime/kin/kinMoneyInCheck"; + +/** A check of money in against the bank: read-only, so a newer check only quiets it. */ +export const KinMoneyInCheckCard = memo(function KinMoneyInCheckCard(props: { + readonly check: KinMoneyInCheckView; +}) { + const { check } = props; + const [open, setOpen] = useState(false); + const shown = kinMoneyInShown(check, open); + const tone = check.superseded ? "text-foreground-muted" : "text-foreground"; + return ( + + {check.superseded ? ( + + {KIN_MONEY_IN_COPY.superseded} + + ) : null} + + {check.title} + + {check.changes === null ? null : ( + + {check.changes} + + )} + {shown.rows.map((row) => ( + + {row.heading === null ? null : ( + + {row.heading} + + )} + + {row.text} + + + ))} + {shown.toggle === null ? null : ( + + + + )} + + ); +} + function KinChoiceCard(props: { readonly choice: KinChoiceCardView; readonly environmentId: EnvironmentId; diff --git a/packages/client-runtime/src/kin/kinCardPresentation.ts b/packages/client-runtime/src/kin/kinCardPresentation.ts index 31242fddad48..3cae356d8a61 100644 --- a/packages/client-runtime/src/kin/kinCardPresentation.ts +++ b/packages/client-runtime/src/kin/kinCardPresentation.ts @@ -5,6 +5,7 @@ import { KIN_HOST_TROUBLE_CARD, KIN_MAIL_RULE_CARD_TYPE, KIN_MEMORY_CHANGES_CARD, + KIN_MONEY_IN_CHECK_CARD, KIN_SOCIAL_PACK_CARD, KIN_SOCIAL_WEEK_CARD, KIN_STATEMENT_PASSWORD_CARD, @@ -33,6 +34,7 @@ import { kinDocCardView, type KinDocCardView } from "./kinDocs.ts"; import { kinHostTroubleCardView } from "./kinHostCare.ts"; import { kinLinkLabel, kinSafeWebUrl } from "./kinLinks.ts"; import { kinMailRuleCardView } from "./kinMailRules.ts"; +import { kinMoneyInCheckView, type KinMoneyInCheckView } from "./kinMoneyInCheck.ts"; import { kinSentences } from "./kinScreenState.ts"; import { kinZoneLabel } from "./kinTimeZone.ts"; import { @@ -100,6 +102,7 @@ export type KinCardView = | { readonly kind: "statement_review"; readonly review: KinStatementReviewView } | { readonly kind: "statement_password"; readonly password: KinStatementPasswordView } | { readonly kind: "bill_batch"; readonly bills: KinBillBatchCardView } + | { readonly kind: "money_in_check"; readonly check: KinMoneyInCheckView } | { readonly kind: "social_week"; readonly week: KinSocialWeekView } | { readonly kind: "social_pack"; readonly pack: KinSocialPackView } /** Drawn with the reader's clock (`kinHostTroubleCardView`), so it carries the widget. */ @@ -646,6 +649,10 @@ function kinLiveCardView( const bills = kinBillBatchCardView(widget); return bills === null ? unknown(widget) : { kind: "bill_batch", bills }; } + case KIN_MONEY_IN_CHECK_CARD: { + const check = kinMoneyInCheckView(widget); + return check === null ? unknown(widget) : { kind: "money_in_check", check }; + } case KIN_MEMORY_CHANGES_CARD: { const changes = kinMemoryChangesView(widget); return changes === null ? unknown(widget) : { kind: "memory_changes", changes }; diff --git a/packages/client-runtime/src/kin/kinMoneyInCheck.test.ts b/packages/client-runtime/src/kin/kinMoneyInCheck.test.ts new file mode 100644 index 000000000000..d049eb6cbbde --- /dev/null +++ b/packages/client-runtime/src/kin/kinMoneyInCheck.test.ts @@ -0,0 +1,131 @@ +import { + KIN_MONEY_IN_CHECK_CARD, + type EnvironmentId, + type KinMoneyInCheckCard, + type KinWidget, +} from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { kinCardView } from "./kinCardPresentation.ts"; +import { kinMoneyInShown, type KinMoneyInCheckView } from "./kinMoneyInCheck.ts"; + +const TITLE = "Money in, 1 Nov to 28 Nov, against the bank"; +const MATCHED = "BNK-2, ₹47,200.00 received 10 Nov; INV-1: ₹47,200.00 recorded as received 10 Nov."; +const BANK_ONLY = "BNK-3, ₹18,500.00 received 17 Nov: Kin can't tell what it is."; + +const line = ( + id: string, + group: KinMoneyInCheckCard["lines"][number]["group"], + text: string, +): KinMoneyInCheckCard["lines"][number] => ({ + id, + group, + day: "2026-11-10", + payments: [], + text, + printed: "NEFT CR TIDEWATER FREIGHT HDFC0001234", +}); + +const card = (overrides: Partial = {}): KinMoneyInCheckCard => ({ + from: "2026-11-01", + through: "2026-11-28", + checkedOn: "2026-11-28", + title: TITLE, + counts: { matched: 1, bankOnly: 1, kinOnly: 0, notSeenYet: 0, differs: 0 }, + explained: 0, + covered: [], + // The host puts bank-only lines after matches; the card shows matches first either way. + lines: [line("bank:BNK-3", "bankOnly", BANK_ONLY), line("bank:BNK-2", "matched", MATCHED)], + text: `${TITLE}\n1 matches.\n${MATCHED}\n1 is at the bank and not in Kin.\n${BANK_ONLY}`, + ...overrides, +}); + +const HOST = "env-home" as EnvironmentId; +const view = (state: unknown): KinMoneyInCheckView => { + const widget = { + orgId: "org", + chatId: "chat", + widgetId: "w1", + messageId: "m1", + widgetClass: "native", + type: KIN_MONEY_IN_CHECK_CARD, + revision: 1, + state, + awaiting: [], + } as unknown as KinWidget; + const drawn = kinCardView(widget, HOST, () => true); + if (drawn.kind !== "money_in_check") throw new Error(`expected money in, got ${drawn.kind}`); + return drawn.check; +}; + +describe("kinMoneyInCheckView", () => { + it("draws a first check: its title and each line under its group, never the bank's words", () => { + const check = view(card()); + expect(check).toMatchObject({ title: TITLE, changes: null, superseded: false }); + expect(check.rows).toEqual([ + { key: "bank:BNK-2", heading: "Matches", text: MATCHED }, + { key: "bank:BNK-3", heading: "At the bank, not in Kin", text: BANK_ONLY }, + ]); + expect(check.accessibilityLabel).toContain(BANK_ONLY); + expect(JSON.stringify(check)).not.toContain("NEFT"); + expect(kinMoneyInShown(check, false)).toEqual({ rows: check.rows, toggle: null }); + }); + + it("says what changed since the last check, and quiets a check a newer one replaced", () => { + const again = view(card({ changes: "Nothing changed since the check on 28 Nov." })); + expect(again.changes).toBe("Nothing changed since the check on 28 Nov."); + expect(again.superseded).toBe(false); + + const old = view(card({ superseded: true, text: `A newer check is below.\n${TITLE}` })); + expect(old.superseded).toBe(true); + expect(old.rows).toHaveLength(2); + expect(old.accessibilityLabel.startsWith("A newer check is below. ")).toBe(true); + }); + + it("folds a long check to its first ten lines under Show all, and opens it whole", () => { + const lines = Array.from({ length: 100 }, (_, index) => + line(`bank:BNK-${index + 1}`, "bankOnly", `BNK-${index + 1}, ₹100.00 received 3 Nov.`), + ); + const check = view(card({ lines })); + const folded = kinMoneyInShown(check, false); + expect(folded.rows).toHaveLength(10); + expect(folded.rows[0]?.heading).toBe("At the bank, not in Kin"); + expect(folded.toggle).toBe("Show all 100"); + const open = kinMoneyInShown(check, true); + expect(open.rows).toHaveLength(100); + expect(open.rows[99]?.text).toBe("BNK-100, ₹100.00 received 3 Nov."); + expect(open.toggle).toBe("Show less"); + }); + + it("shows what a card missing fields has, never a blank card", () => { + // No lines: the host's own text, without the lines the card already shows. + const { lines: _lines, ...noLines } = card({ changes: "Since the check on 20 Nov: 1 fixed." }); + expect(view(noLines)).toMatchObject({ + title: TITLE, + changes: "Since the check on 20 Nov: 1 fixed.", + rows: [ + { heading: null, text: "1 matches." }, + { heading: null, text: MATCHED }, + { heading: null, text: "1 is at the bank and not in Kin." }, + { heading: null, text: BANK_ONLY }, + ], + }); + // No title, a line with no group, and a line with no words. + const { title: _title, ...noTitle } = card({ + lines: [ + { ...line("", "matched", MATCHED), group: undefined } as never, + line("x", "differs", " "), + ], + }); + expect(view(noTitle)).toMatchObject({ + title: "Money in, against the bank", + rows: [{ key: "line:0", heading: null, text: MATCHED }], + }); + // Nothing at all to show falls back to the generic card. + const empty = { orgId: "org", widgetClass: "native", type: KIN_MONEY_IN_CHECK_CARD, state: {} }; + expect(kinCardView(empty as unknown as KinWidget, HOST, () => true)).toEqual({ + kind: "unknown", + text: null, + }); + }); +}); diff --git a/packages/client-runtime/src/kin/kinMoneyInCheck.ts b/packages/client-runtime/src/kin/kinMoneyInCheck.ts new file mode 100644 index 000000000000..2a9ae6090f9a --- /dev/null +++ b/packages/client-runtime/src/kin/kinMoneyInCheck.ts @@ -0,0 +1,118 @@ +import type { KinMoneyInCheckCard, KinWidget } from "@t3tools/contracts"; + +import { kinSentences } from "./kinScreenState.ts"; + +type Group = KinMoneyInCheckCard["lines"][number]["group"]; + +/** A long check opens to this many lines under Show all. */ +const KIN_MONEY_IN_FOLD = 10; + +const HEADINGS: Record = { + matched: "Matches", + bankOnly: "At the bank, not in Kin", + kinOnly: "In Kin, not at the bank", + notSeenYet: "In Kin, not seen at the bank yet", + differs: "Differs", +}; +const GROUPS = Object.keys(HEADINGS) as ReadonlyArray; +const isGroup = (value: unknown): value is Group => + typeof value === "string" && Object.hasOwn(HEADINGS, value); + +export const KIN_MONEY_IN_COPY = { + title: "Money in, against the bank", + superseded: "A newer check is below.", + showAll: (count: number) => `Show all ${count}`, + showLess: "Show less", +} as const; + +export interface KinMoneyInRow { + readonly key: string; + /** The group this row starts; null while the group continues or the card has no groups. */ + readonly heading: string | null; + readonly text: string; +} + +export interface KinMoneyInCheckView { + readonly title: string; + /** "Since the check on …" or "Nothing changed since …"; null on a first check. */ + readonly changes: string | null; + /** A newer check replaced this one: the card reads quiet. */ + readonly superseded: boolean; + readonly rows: ReadonlyArray; + readonly accessibilityLabel: string; +} + +const words = (value: unknown) => (typeof value === "string" ? value.trim() : ""); + +// The bank's own words (`printed`) are never drawn: each line's text starts with its Kin key. +function lineRows(lines: ReadonlyArray): ReadonlyArray { + const read = lines.flatMap((line, index) => { + if (typeof line !== "object" || line === null) return []; + const text = words(Reflect.get(line, "text")); + if (text === "") return []; + const group: unknown = Reflect.get(line, "group"); + const id = words(Reflect.get(line, "id")); + return [{ key: id === "" ? `line:${index}` : id, group: isGroup(group) ? group : null, text }]; + }); + const order = (group: Group | null) => (group === null ? GROUPS.length : GROUPS.indexOf(group)); + return [...read] + .sort((a, b) => order(a.group) - order(b.group)) + .map((row, index, all) => ({ + key: row.key, + heading: + row.group === null || all[index - 1]?.group === row.group ? null : HEADINGS[row.group], + text: row.text, + })); +} + +// A card without readable lines still has the host's plain text: each of its lines but the head. +function textRows(text: string, skip: ReadonlySet): ReadonlyArray { + return text + .split("\n") + .map((line) => line.trim()) + .filter((line) => line !== "" && !skip.has(line)) + .map((line, index) => ({ key: `text:${index}`, heading: null, text: line })); +} + +/** Null only when the state carries nothing to show, so the card falls back to plain text. */ +export function kinMoneyInCheckView(widget: KinWidget): KinMoneyInCheckView | null { + const state: unknown = widget.state; + if (typeof state !== "object" || state === null) return null; + const title = words(Reflect.get(state, "title")); + const changes = words(Reflect.get(state, "changes")); + const superseded = Reflect.get(state, "superseded") === true; + const lines: unknown = Reflect.get(state, "lines"); + const rows = Array.isArray(lines) + ? lineRows(lines) + : textRows( + words(Reflect.get(state, "text")), + new Set([title, changes, KIN_MONEY_IN_COPY.superseded]), + ); + if (title === "" && changes === "" && rows.length === 0) return null; + const shownTitle = title === "" ? KIN_MONEY_IN_COPY.title : title; + return { + title: shownTitle, + changes: changes === "" ? null : changes, + superseded, + rows, + accessibilityLabel: kinSentences([ + superseded ? KIN_MONEY_IN_COPY.superseded : null, + shownTitle, + changes, + ...rows.flatMap((row) => [row.heading, row.text]), + ]), + }; +} + +/** The rows a card draws: a long check folds to its first lines until Show all. */ +export function kinMoneyInShown(view: KinMoneyInCheckView, open: boolean) { + const folds = view.rows.length > KIN_MONEY_IN_FOLD; + return { + rows: folds && !open ? view.rows.slice(0, KIN_MONEY_IN_FOLD) : view.rows, + toggle: !folds + ? null + : open + ? KIN_MONEY_IN_COPY.showLess + : KIN_MONEY_IN_COPY.showAll(view.rows.length), + }; +}