diff --git a/fe/README.md b/fe/README.md index 4af9cb7..7f222f9 100644 --- a/fe/README.md +++ b/fe/README.md @@ -79,6 +79,16 @@ The mock reads two localStorage keys. Set them in the browser and reload: Every other state on the board (each contract code, signing, pending, timed out) needs the stubbed RPC and is exercised in `tests/e2e/advance.spec.ts`. +While `NEXT_PUBLIC_API_URL` is empty, Repayment and History read fixtures instead of the #16 API. +One more key picks their state: + +| Key | Value | What happens | +| --- | --- | --- | +| `forepay.mock.ledger` | `active` (or unset) | Advance 1: 30.00 of 90.00 USDC repaid, with the 3a and 3b frames' transactions | +| `forepay.mock.ledger` | `none` | "No advance yet" on both pages | +| `forepay.mock.ledger` | `repaid` | the advance fully repaid, with a last 60.00 repayment | +| `forepay.mock.ledger` | `error` | "We cannot read your balance right now" | + ## A wallet to test with 1. Install the Freighter browser extension and create an account. diff --git a/fe/eslint.config.mjs b/fe/eslint.config.mjs index 7cc5b1e..b2e2e91 100644 --- a/fe/eslint.config.mjs +++ b/fe/eslint.config.mjs @@ -10,6 +10,8 @@ const eslintConfig = defineConfig([ // Default ignores of eslint-config-next: ".next/**", ".next-e2e/**", + // Any other build output a dev server was pointed at (NEXT_DIST_DIR), so a stray one cannot fail lint. + ".next-*/**", "out/**", "build/**", "next-env.d.ts", diff --git a/fe/src/app/globals.css b/fe/src/app/globals.css index 2a5fd69..4f1c9a9 100644 --- a/fe/src/app/globals.css +++ b/fe/src/app/globals.css @@ -9,3 +9,23 @@ body { /* The design's text layers use the font's own line height unless they set one. */ line-height: normal; } + +/* + * Tailwind 4's preflight gives diff --git a/fe/src/lib/api/client.test.ts b/fe/src/lib/api/client.test.ts new file mode 100644 index 0000000..97862c0 --- /dev/null +++ b/fe/src/lib/api/client.test.ts @@ -0,0 +1,43 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { getAdvances, getTransactions, usingMockApi } from "./client"; + +const BORROWER = "GBORROWER"; + +describe("the fixture client's forepay.mock.ledger switch", () => { + afterEach(() => window.localStorage.removeItem("forepay.mock.ledger")); + + it("answers from the fixtures while no API is set", () => { + expect(usingMockApi).toBe(true); + }); + + it("has the active advance by default: 30.00 of 90.00 repaid", async () => { + const [advance] = await getAdvances(BORROWER); + expect(advance.repaid).toBe(300_000_000n); + expect(await getTransactions(BORROWER)).toHaveLength(5); + }); + + it("none: no advance and no rows", async () => { + window.localStorage.setItem("forepay.mock.ledger", "none"); + expect(await getAdvances(BORROWER)).toEqual([]); + expect(await getTransactions(BORROWER)).toEqual([]); + }); + + it("repaid: all of it, with the last repayment first", async () => { + window.localStorage.setItem("forepay.mock.ledger", "repaid"); + const [advance] = await getAdvances(BORROWER); + expect(advance.repaid).toBe(advance.principal); + const [last] = await getTransactions(BORROWER); + expect(last).toMatchObject({ kind: "repayment", amount: 600_000_000n, status: "completed" }); + }); + + it("error: both reads fail", async () => { + window.localStorage.setItem("forepay.mock.ledger", "error"); + await expect(getAdvances(BORROWER)).rejects.toThrow(); + await expect(getTransactions(BORROWER)).rejects.toThrow(); + }); + + it("anything else reads as the default", async () => { + window.localStorage.setItem("forepay.mock.ledger", "paused"); + expect(await getAdvances(BORROWER)).toHaveLength(1); + }); +}); diff --git a/fe/src/lib/api/client.ts b/fe/src/lib/api/client.ts index 8a22788..14e9770 100644 --- a/fe/src/lib/api/client.ts +++ b/fe/src/lib/api/client.ts @@ -7,6 +7,10 @@ import type { AdvanceSummary, LedgerTransaction } from "./types"; * fixtures below, which use the brief's example figures (§4). When #16 lands, only `fetchJson` and the * two route paths change; screens keep calling these functions. * + * The localStorage key `forepay.mock.ledger` picks the fixtures' state, for trying each screen by hand: + * `none` (no advance yet), `active` (the default, 30.00 of 90.00 repaid), `repaid` (all of it), or + * `error` (the API does not answer). + * * PROVISIONAL route paths: agree them with @Lin1er. */ const API_URL = (process.env.NEXT_PUBLIC_API_URL ?? "").replace(/\/+$/, ""); @@ -46,27 +50,57 @@ export async function getTransactions(borrower: string): Promise => [ - { - nullifier: NULLIFIER, - borrower, - windowStart: "2026-07-01", - windowEnd: "2026-09-30", - revenueCents: 36_000n, - principal: 900_000_000n, - repaid: 300_000_000n, - openedAt: OPENED, - txHash: "a3f9c2".padEnd(64, "0"), - }, - ], + // Proven revenue $360.00 → 25% = 90.00 USDC, 30.00 repaid (brief §4), or all of it when `repaid`. + advances: async (borrower: string): Promise => { + const state = mockLedger(); + if (!guard(state)) return []; + return [ + { + nullifier: NULLIFIER, + borrower, + windowStart: "2026-07-01", + windowEnd: "2026-09-30", + revenueCents: 36_000n, + principal: 900_000_000n, + repaid: state === "repaid" ? 900_000_000n : 300_000_000n, + openedAt: OPENED, + txHash: "a3f9c2".padEnd(64, "0"), + }, + ]; + }, // The 3a and 3b frames' list: 20.00 repaid on 20 Oct, 10.00 that never went through, 10.00 on 24 Oct. - transactions: async (): Promise => [ - { hash: "e90b13".padEnd(64, "1"), kind: "repayment", amount: 100_000_000n, at: OPENED + 1_036_800n, status: "completed", nullifier: NULLIFIER }, - { hash: "f1c2d0".padEnd(64, "2"), kind: "repayment", amount: 100_000_000n, at: OPENED + 1_036_000n, status: "failed", nullifier: NULLIFIER }, - { hash: "7c21d0".padEnd(64, "3"), kind: "repayment", amount: 200_000_000n, at: OPENED + 691_200n, status: "completed", nullifier: NULLIFIER }, - { hash: "a3f9c2".padEnd(64, "0"), kind: "advance", amount: 900_000_000n, at: OPENED, status: "completed", nullifier: NULLIFIER }, - { hash: "5b8e44".padEnd(64, "4"), kind: "trustline", amount: 0n, at: OPENED - 600n, status: "completed", nullifier: "" }, - ], + // `repaid` adds the last 60.00 on 26 Oct. + transactions: async (): Promise => { + const state = mockLedger(); + if (!guard(state)) return []; + return [ + ...(state === "repaid" + ? [{ hash: "c4d7e1".padEnd(64, "5"), kind: "repayment" as const, amount: 600_000_000n, at: OPENED + 1_209_600n, status: "completed" as const, nullifier: NULLIFIER }] + : []), + { hash: "e90b13".padEnd(64, "1"), kind: "repayment", amount: 100_000_000n, at: OPENED + 1_036_800n, status: "completed", nullifier: NULLIFIER }, + { hash: "f1c2d0".padEnd(64, "2"), kind: "repayment", amount: 100_000_000n, at: OPENED + 1_036_000n, status: "failed", nullifier: NULLIFIER }, + { hash: "7c21d0".padEnd(64, "3"), kind: "repayment", amount: 200_000_000n, at: OPENED + 691_200n, status: "completed", nullifier: NULLIFIER }, + { hash: "a3f9c2".padEnd(64, "0"), kind: "advance", amount: 900_000_000n, at: OPENED, status: "completed", nullifier: NULLIFIER }, + { hash: "5b8e44".padEnd(64, "4"), kind: "trustline", amount: 0n, at: OPENED - 600n, status: "completed", nullifier: "" }, + ]; + }, }; diff --git a/fe/src/lib/api/types.ts b/fe/src/lib/api/types.ts index 0029056..65b534f 100644 --- a/fe/src/lib/api/types.ts +++ b/fe/src/lib/api/types.ts @@ -38,7 +38,10 @@ export type LedgerTransaction = { amount: bigint; /** Ledger timestamp in seconds. */ at: bigint; - /** A failed repayment is shown, struck through, with "not charged" (3a states). */ + /** + * `failed`: it reached the ledger and failed there, so the network fee was spent and no USDC moved + * (#16). A transaction that never reached a ledger is not listed. + */ status: "completed" | "failed"; nullifier: string; }; diff --git a/fe/src/lib/cn.test.ts b/fe/src/lib/cn.test.ts new file mode 100644 index 0000000..c6457c2 --- /dev/null +++ b/fe/src/lib/cn.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from "vitest"; +import { cn } from "./cn"; + +describe("cn", () => { + it("keeps a type role next to a text colour", () => { + expect(cn("text-card-title text-fg")).toBe("text-card-title text-fg"); + expect(cn("text-figure text-success")).toBe("text-figure text-success"); + expect(cn("text-input-amount text-fg")).toBe("text-input-amount text-fg"); + }); + + it("lets a later type role or pixel size win, as with any font size", () => { + expect(cn("text-body", "text-small")).toBe("text-small"); + expect(cn("text-page-title", "text-[20px]")).toBe("text-[20px]"); + }); +}); diff --git a/fe/src/lib/cn.ts b/fe/src/lib/cn.ts index a5ef193..6b33ada 100644 --- a/fe/src/lib/cn.ts +++ b/fe/src/lib/cn.ts @@ -1,5 +1,13 @@ import { clsx, type ClassValue } from "clsx"; -import { twMerge } from "tailwind-merge"; +import { extendTailwindMerge } from "tailwind-merge"; + +// The type roles in tokens.css are font sizes: without this, tailwind-merge reads `text-card-title` +// as a colour and drops it next to `text-fg`. +const twMerge = extendTailwindMerge({ + extend: { + classGroups: { "font-size": [{ text: ["hero", "amount", "page-title", "card-title", "input-amount", "figure", "body", "small"] }] }, + }, +}); export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); diff --git a/fe/src/lib/useDesktop.ts b/fe/src/lib/useDesktop.ts index f950e69..172ac02 100644 --- a/fe/src/lib/useDesktop.ts +++ b/fe/src/lib/useDesktop.ts @@ -1,11 +1,15 @@ import { useSyncExternalStore } from "react"; -/** The desktop layout's breakpoint, Tailwind's `lg` (1024px), where the design switches frames. */ -const DESKTOP = "(min-width: 1024px)"; +/** + * Where the phone frames end, Tailwind's `md` (768px): from there the tablet and desktop share the + * top nav and the centred dialogs (section 08 of the design). + */ +const DESKTOP = "(min-width: 768px)"; /** - * Whether the desktop frames apply, kept in step with the window. For a component whose desktop and - * phone forms differ in structure, not only in classes. The server renders the desktop form. + * Whether the tablet and desktop frames apply, kept in step with the window. For a component whose + * desktop and phone forms differ in structure, not only in classes. The server renders the desktop + * form. */ export function useDesktop(): boolean { return useSyncExternalStore( diff --git a/fe/src/modules/advance/Advance.tsx b/fe/src/modules/advance/Advance.tsx index c38fe2c..c2623ec 100644 --- a/fe/src/modules/advance/Advance.tsx +++ b/fe/src/modules/advance/Advance.tsx @@ -31,7 +31,7 @@ import { ReviewOffer } from "./components/ReviewOffer"; import { SignAdvance } from "./components/SignAdvance"; import { maxFee } from "./fee"; import { isCapped } from "./cap"; -import { clearPending, loadPending, savePending, type PendingAdvance } from "./pending"; +import { clearPending, loadPending, markRefused, onRefused, savePending, type PendingAdvance } from "./pending"; import { WalletSignError } from "@/lib/stellar/wallet-error"; import { acceptFailure, proofStillGood, proveFailure, type AdvanceFailure } from "./failures"; @@ -92,8 +92,8 @@ export function Advance() { useEffect(() => { if (!address) return; const pending = loadPending(address); - if (pending) void resume(pending); - // `resume` only reads refs and setters; it runs per mount and per wallet. + if (pending) void pickUp(pending); + // `pickUp` only reads refs and setters; it runs per mount and per wallet. // eslint-disable-next-line react-hooks/exhaustive-deps }, [address]); @@ -145,7 +145,7 @@ export function Advance() { leave(); // While an advance sent earlier is still being waited on, the start is that wait, not 2a. const pending = loadPending(me); - if (pending) return void resume(pending); + if (pending) return void pickUp(pending); setFlow({ at: "start" }); } @@ -225,13 +225,22 @@ export function Advance() { }; // Once the network has it, the transaction is remembered: leaving the page no longer loses it. - const remember = (hash: string) => savePending({ address: me, hash, req: r }); + let sentHash: string | undefined; + const remember = (hash: string) => { + sentHash = hash; + savePending({ address: me, hash, req: r }); + }; try { const { hash, value } = await requestAdvance(me, r, sign, remember); if (id !== attempt.current) return; landed(r, hash, value); } catch (e) { - if (id !== attempt.current) return; + if (id !== attempt.current) { + // The creator left meanwhile. A send turned down before any ledger will never land, so the + // screen they come back to says so at once rather than waiting two minutes for it. + if (sentHash && e instanceof TransactionFailedError && !e.charged && !e.unconfirmed) markRefused(me, sentHash); + return; + } stopped(e, r, signed); } } @@ -247,6 +256,12 @@ export function Advance() { void queryClient.invalidateQueries({ queryKey: ["transactions"] }); } + /** Back to a transaction sent before the creator left the page: its refusal, or the wait for it. */ + function pickUp(p: PendingAdvance) { + if (p.refused) refusedMeanwhile(p); + else void resume(p); + } + /** "Sending your USDC" again, for a transaction sent before the creator left the page. */ async function resume(p: PendingAdvance) { leave(); @@ -255,6 +270,10 @@ export function Advance() { slowTimer.current = setTimeout(() => { if (id === attempt.current) setFlow({ at: "signing", req: p.req, sent: true, slow: true }); }, SLOW_AFTER_MS); + // The send itself may still be answering, to the screen that was left: a refusal ends this wait. + const stopListening = onRefused(p.hash, () => { + if (id === attempt.current) refusedMeanwhile(p); + }); try { const { hash, value } = await awaitAdvance(p.hash); if (id !== attempt.current) return; @@ -262,9 +281,19 @@ export function Advance() { } catch (e) { if (id !== attempt.current) return; stopped(e, p.req, true); + } finally { + stopListening(); } } + /** The network turned down a send the creator left: nothing was charged, and Try again sends it. */ + function refusedMeanwhile(p: PendingAdvance) { + leave(); + clearPending(); + const refused = new TransactionFailedError("The network did not accept the transaction", false); + setFlow({ at: "failed", failure: acceptFailure(refused, true), req: p.req, during: "accept" }); + } + /** * Waiting on a sent advance ended without it landing. If the network gave no answer at all, the * transaction is still out: it stays remembered, and Try again waits on it again. Otherwise the ledger diff --git a/fe/src/modules/advance/components/AdvanceScreen.tsx b/fe/src/modules/advance/components/AdvanceScreen.tsx index 8783042..d92cbd0 100644 --- a/fe/src/modules/advance/components/AdvanceScreen.tsx +++ b/fe/src/modules/advance/components/AdvanceScreen.tsx @@ -36,10 +36,10 @@ export function AdvanceScreen({ }, [focusKey]); return ( -
+
-

Advance

-

{title}

+

Advance

+

{title}

diff --git a/fe/src/modules/advance/components/DetailRows.tsx b/fe/src/modules/advance/components/DetailRows.tsx index d8bfb17..19b1b01 100644 --- a/fe/src/modules/advance/components/DetailRows.tsx +++ b/fe/src/modules/advance/components/DetailRows.tsx @@ -7,8 +7,8 @@ export function DetailRows({ rows }: { rows: { label: string; value: ReactNode;
{rows.map((r) => (
-
{r.label}
-
{r.value}
+
{r.label}
+
{r.value}
))}
diff --git a/fe/src/modules/advance/components/FailurePanel.tsx b/fe/src/modules/advance/components/FailurePanel.tsx index fd55a81..1bd14cf 100644 --- a/fe/src/modules/advance/components/FailurePanel.tsx +++ b/fe/src/modules/advance/components/FailurePanel.tsx @@ -61,6 +61,8 @@ export function FailurePanel({ let illustration: ReactNode; let actions: ReactNode; + // Side by side on a phone only for "Try again" or "Prove again" with "Back": the pairs that fit at 320. + let row = false; let copy; if ("code" in failure) { @@ -71,11 +73,13 @@ export function FailurePanel({ case 30: illustration = ; actions = failure.code === 30 ? back : <>{proveAgain}{back}; + row = true; break; case 12: case 13: illustration = ; actions = <>{proveAgain}{back}; + row = true; break; case 20: illustration = ; @@ -104,6 +108,7 @@ export function FailurePanel({ // A refusal a creator cannot cause (admin and upgrade codes): the transaction did not happen. illustration = ; actions = <>{}{back}; + row = true; } } else if (failure.kind === "tooSmall") { copy = tooSmallCopy( @@ -134,6 +139,7 @@ export function FailurePanel({ {back} ); + row = true; break; case "charged": illustration = ; @@ -143,6 +149,7 @@ export function FailurePanel({ {back} ); + row = true; break; case "unconfirmed": illustration = ; @@ -166,6 +173,7 @@ export function FailurePanel({ {back} ); + row = true; break; case "relayNotReady": case "notConfigured": @@ -190,5 +198,5 @@ export function FailurePanel({ } } - return ; + return ; } diff --git a/fe/src/modules/advance/components/FundsReceived.tsx b/fe/src/modules/advance/components/FundsReceived.tsx index 046eaba..79ba23d 100644 --- a/fe/src/modules/advance/components/FundsReceived.tsx +++ b/fe/src/modules/advance/components/FundsReceived.tsx @@ -34,19 +34,19 @@ export function FundsReceived({ advance ? render(advance) : advanceFailed ? missing : pending; return ( -
+

-

The USDC is in your wallet

-

+

The USDC is in your wallet

+

You can repay any amount, any time, from the Repayment tab.

-

{title}

-

{body}

+

{title}

+

{body}

{action &&
{action}
}
); diff --git a/fe/src/modules/advance/components/ProveRevenue.tsx b/fe/src/modules/advance/components/ProveRevenue.tsx index 5def997..014a1ad 100644 --- a/fe/src/modules/advance/components/ProveRevenue.tsx +++ b/fe/src/modules/advance/components/ProveRevenue.tsx @@ -9,8 +9,12 @@ import type { UsdcStatus } from "@/lib/stellar/trustline"; /** * 2a Prove revenue. What will be read (AdSense revenue for the last three months, nothing else), what - * will not, and the button that opens the Reclaim session. Desktop adds the two lists and the wallet - * card; a phone keeps the card, the button and the rule. + * will not, and the button that opens the Reclaim session. From a tablet up it adds the two lists and + * the wallet card; a phone keeps the card, the button and the rule. + * + * Section 08: on a tablet the side cards sit in a row under the Prove card; from 1024 they make the side + * column, 320 wide and 360 from 1200. The two lists sit side by side only where the text column has + * room for both. */ export function ProveRevenue({ walletName, @@ -30,22 +34,22 @@ export function ProveRevenue({ }) { return (
-
-
+
+
-
-

- See how much you can get -

-

+

+

See how much you can get

+

Log in to AdSense in a secure window. We read your revenue for the last three months and nothing else.

-
+ {/* Side by side only where each list keeps its longest item ("Revenue from the last three + complete months", about 360 in Body) on one line: two of them and the 24 gap. */} +
} /> @@ -55,38 +59,44 @@ export function ProveRevenue({ icon={} />
-
-
-
-

Your wallet

+
+
+

Your wallet

{walletName}} /> Stellar testnet} /> } />
-
-

How your advance is set

- {terms === "loading" ? ( - // As tall as the line it stands for: one 20px line on a phone, two 21px lines on desktop, - // where "The same proof always gives the same number." wraps under it. -
- - -
- ) : ( -

- {terms - ? `${shareWords(terms)} of your proven revenue, up to ${usdcWords(terms.capStroops)}.` - : "A share of your proven revenue, up to a limit per advance."} - The same proof always gives the same number. -

- )} +
+

How your advance is set

+ {/* While the rate loads, bars in the shape of the sentence: invisible words of the same length + and no figure (every figure is the contract's), so the bars wrap as the sentence will at any + width and the card keeps its height. */} +

+ {terms === "loading" ? ( + + A part of the revenue you have proven, up to a cap + The same proof always gives the same number. + + ) : ( + <> + {terms + ? `${shareWords(terms)} of your proven revenue, up to ${usdcWords(terms.capStroops)}.` + : "A share of your proven revenue, up to a limit per advance."} + The same proof always gives the same number. + + )} +

@@ -95,11 +105,12 @@ export function ProveRevenue({ function List({ head, items, icon }: { head: string; items: string[]; icon: ReactNode }) { return ( -
-

{head}

+
+

{head}