diff --git a/src/viewer/app/i18n/ru.ts b/src/viewer/app/i18n/ru.ts index 3a5887e..913825d 100644 --- a/src/viewer/app/i18n/ru.ts +++ b/src/viewer/app/i18n/ru.ts @@ -6,6 +6,12 @@ export const RU: Record = { 'Previous pages': 'Предыдущие страницы', 'Next pages': 'Следующие страницы', 'Fidelity Summary': 'Fidelity Сводка', + // the pages of the scope tree, no source in the name (app/pages.ts) + 'Summary': 'Сводка', + 'Positions': 'Позиции', + 'Activity': 'Операции', + 'Reports': 'Отчёты', + 'Salary': 'Зарплата', 'Saved login': 'Сохранённый логин', 'The script panel on the sites opens. Click to keep it minimized': 'Панель скрипта на сайтах открывается. Нажмите, чтобы держать её свёрнутой', 'The script panel on the sites stays minimized. Click to open it': 'Панель скрипта на сайтах свёрнута. Нажмите, чтобы открывать её', diff --git a/src/viewer/app/pages.test.ts b/src/viewer/app/pages.test.ts new file mode 100644 index 0000000..2ce6e74 --- /dev/null +++ b/src/viewer/app/pages.test.ts @@ -0,0 +1,65 @@ +/// +/// +/// +// The pages table: which pages a scope shows, which one opens, today's seven tabs. Account numbers here are made up +import { expect, test } from 'bun:test'; +import { LEAF_401K, LEAF_PAY, PAGES, REFERENCE_PAGES, openPage, pageFacts, pageName, referenceName, referenceSource, referenceTabs, visiblePages } from './pages'; +import { MAIN_TABS } from './prefs'; + +const taxable = { id: 'fidelity:X1', shelter: null }; +const roth = { id: 'fidelity:R1', shelter: 'free' as const }; +const ira = { id: 'fidelity:T1', shelter: 'deferred' as const }; +const k401 = { id: LEAF_401K }; +const pay = { id: LEAF_PAY }; +const pages = (...leaves: Array<{ id: string; shelter?: 'free' | 'deferred' | null }>) => visiblePages(pageFacts(leaves)); + +test('the facts of the shown leaves', () => { + expect(pageFacts([taxable, roth, k401])).toEqual({ fidelity: true, taxable: true, roth: true, k401: true, pay: false }); + expect(pageFacts([ira])).toEqual({ fidelity: true, taxable: false, roth: false, k401: false, pay: false }); + expect(pageFacts([pay])).toEqual({ fidelity: false, taxable: false, roth: false, k401: false, pay: true }); + expect(pageFacts([])).toEqual({ fidelity: false, taxable: false, roth: false, k401: false, pay: false }); +}); + +test('a page shows only where the scope has data for it (the owner\'s tree of 2026-12-03)', () => { + // All: every page + expect(pages(taxable, roth, k401, pay)).toEqual(PAGES.slice()); + // Fidelity alone: today's Fidelity pages + expect(pages(taxable, roth)).toEqual(['summary', 'positions', 'activity', 'reports', 'taxes']); + // only the Roth IRAs: nothing is taxed, so no Taxes + expect(pages(roth)).toEqual(['summary', 'positions', 'activity', 'reports']); + // a traditional IRA: no Taxes either (taxed when taken out) + expect(pages(ira)).toEqual(['summary', 'positions', 'activity', 'reports']); + // the 401k alone: Summary, Activity and Taxes (its pre-tax contributions) + expect(pages(k401)).toEqual(['summary', 'activity', 'taxes']); + // only ADP Workforce: only the salary and the taxes + expect(pages(pay)).toEqual(['taxes', 'salary']); + // the ADP group: the 401k pages and the salary and taxes pages + expect(pages(k401, pay)).toEqual(['summary', 'activity', 'taxes', 'salary']); + expect(pages()).toEqual([]); +}); + +test('the wanted page opens while it has data, else the first one that has, else none', () => { + expect(openPage('positions', ['summary', 'positions'])).toBe('positions'); + expect(openPage('positions', ['summary', 'activity'])).toBe('summary'); + expect(openPage('salary', ['taxes', 'salary'])).toBe('salary'); + expect(openPage('summary', ['taxes', 'salary'])).toBe('taxes'); + expect(openPage('summary', [])).toBeNull(); +}); + +test('every page has a name without a source', () => { + expect(PAGES.map(pageName)).toEqual(['Summary', 'Positions', 'Activity', 'Reports', 'Taxes', 'Salary']); +}); + +test('today\'s seven pages, in today\'s order, each with its source and its new page', () => { + expect(REFERENCE_PAGES.map((r) => r.tab)).toEqual(MAIN_TABS.slice()); + expect(MAIN_TABS.map(referenceName)).toEqual(['Fidelity Summary', 'Fidelity Positions', 'Fidelity Activity', 'Fidelity Reports', 'Fidelity Taxes', 'ADP Summary', 'ADP Activity']); + expect(MAIN_TABS.map(referenceSource)).toEqual(['fidelity', 'fidelity', 'fidelity', 'fidelity', 'fidelity', 'adp', 'adp']); + expect(REFERENCE_PAGES.map((r) => r.page)).toEqual(['summary', 'positions', 'activity', 'reports', 'taxes', 'summary', 'activity']); +}); + +test('today\'s tabs: the ADP pages once the 401k is read, Fidelity Taxes once a tax year is read', () => { + expect(referenceTabs({ adp: false, taxYears: false })).toEqual(['summary', 'positions', 'activity', 'reports']); + expect(referenceTabs({ adp: true, taxYears: false })).toEqual(['summary', 'positions', 'activity', 'reports', 'adpSummary', 'adpActivity']); + expect(referenceTabs({ adp: false, taxYears: true })).toEqual(['summary', 'positions', 'activity', 'reports', 'taxes']); + expect(referenceTabs({ adp: true, taxYears: true })).toEqual(MAIN_TABS.slice()); +}); diff --git a/src/viewer/app/pages.ts b/src/viewer/app/pages.ts new file mode 100644 index 0000000..919fc1a --- /dev/null +++ b/src/viewer/app/pages.ts @@ -0,0 +1,86 @@ +/// +// The pages of the viewer as one table (owner 2026-12-03: "we will show only available pages for certain acc, if there are +// no data then we have nothing to show and we should not"). Two lists: +// - PAGES: the pages of the scope tree, without a source prefix (owner 2026-12-03: "with these All group and or Fidelity +// and ADP subgroup we would not need [the page prefixes]"); a page is shown only where the scope has data for it +// - REFERENCE_PAGES: the seven pages of today (Fidelity Summary ... ADP Activity), kept as they are for comparing the new +// numbers with them (owner 2026-12-03: "requirement for site map and having previous pages versions backup in this +// sitemap should remain"); until the scope tree takes over they are the tabs the app draws +import type { Shelter } from '../../shared/tax'; +import { t } from './i18n'; +import type { MainTab } from './prefs'; + +export const PAGES = ['summary', 'positions', 'activity', 'reports', 'taxes', 'salary'] as const; +export type Page = (typeof PAGES)[number]; + +/** the name of a page in the tabs (no source in it: the strip says which accounts are shown) */ +export const pageName = (page: Page): string => ({ summary: t('Summary'), positions: t('Positions'), activity: t('Activity'), reports: t('Reports'), taxes: t('Taxes'), salary: t('Salary') })[page]; + +// leaf ids of the scope tree: a Fidelity account by its number, the ADP 401k, the ADP Workforce pay (later) +export const LEAF_401K = 'adp:401k'; +export const LEAF_PAY = 'adp:pay'; +export const FIDELITY_LEAF = 'fidelity:'; +export const isFidelityLeaf = (id: string): boolean => id.startsWith(FIDELITY_LEAF); + +/** what the shown accounts hold: the facts the pages are chosen by */ +export interface PageFacts { fidelity: boolean; taxable: boolean; roth: boolean; k401: boolean; pay: boolean } + +/** + * The facts of the shown leaves. A Fidelity account is taxable when it shelters nothing (shared/tax.ts shelterOf), a + * Roth when its earnings are tax-free; another retirement account (tax-deferred) is neither + */ +export function pageFacts(leaves: ReadonlyArray<{ id: string; shelter?: Shelter | null }>): PageFacts { + const fid = leaves.filter((l) => isFidelityLeaf(l.id)); + return { + fidelity: fid.length > 0, + taxable: fid.some((l) => !l.shelter), + roth: fid.some((l) => l.shelter === 'free'), + k401: leaves.some((l) => l.id === LEAF_401K), + pay: leaves.some((l) => l.id === LEAF_PAY), + }; +} + +/** + * The pages with data under the scope, in the order of PAGES: Summary for any account, Positions and Reports for + * Fidelity, Activity for Fidelity or the 401k, Taxes for a taxable account, the 401k or the pay, Salary for the pay only + */ +export function visiblePages(f: PageFacts): Page[] { + const on: Record = { summary: f.fidelity || f.k401, positions: f.fidelity, activity: f.fidelity || f.k401, reports: f.fidelity, taxes: f.taxable || f.k401 || f.pay, salary: f.pay }; + return PAGES.filter((p) => on[p]); +} + +/** + * The saved page is the wanted one; the page drawn is the wanted one while it has data, else the first one that has + * (Summary whenever there is one), so ticking an account back brings the wanted page back. null: nothing has data + */ +export function openPage(wanted: T, visible: readonly T[]): T | null { + return visible.indexOf(wanted) >= 0 ? wanted : visible[0] ?? null; +} + +/** a page of today, kept for comparing: its tab, the source it shows and the new page it becomes */ +export interface ReferencePage { tab: MainTab; source: 'fidelity' | 'adp'; page: Page } + +// the order of today's tabs (owner 2026-11-29: the pages of one source carry its name, the ADP 401k ones after Fidelity's) +export const REFERENCE_PAGES: readonly ReferencePage[] = [ + { tab: 'summary', source: 'fidelity', page: 'summary' }, + { tab: 'positions', source: 'fidelity', page: 'positions' }, + { tab: 'activity', source: 'fidelity', page: 'activity' }, + { tab: 'reports', source: 'fidelity', page: 'reports' }, + { tab: 'taxes', source: 'fidelity', page: 'taxes' }, + { tab: 'adpSummary', source: 'adp', page: 'summary' }, + { tab: 'adpActivity', source: 'adp', page: 'activity' }, +]; + +/** the name of a page of today, its source first (owner 2026-11-29: "Fidelity Summary, Fidelity Positions and Fidelity Activity") */ +export const referenceName = (tab: MainTab): string => ({ summary: t('Fidelity Summary'), positions: t('Fidelity Positions'), activity: t('Fidelity Activity'), reports: t('Fidelity Reports'), taxes: t('Fidelity Taxes'), adpSummary: t('ADP Summary'), adpActivity: t('ADP Activity') })[tab]; + +/** the source of a page of today: the ADP pages show the 401k's money sources in the strip, the rest the Fidelity accounts */ +export const referenceSource = (tab: MainTab): 'fidelity' | 'adp' => REFERENCE_PAGES.find((r) => r.tab === tab)?.source ?? 'fidelity'; + +/** + * Today's tabs with data: the ADP pages once the 401k history is there (owner 2026-11-30), Fidelity Taxes once Fidelity's + * tax summary has a year (owner 2026-12-03); the rest always + */ +export function referenceTabs(have: { adp: boolean; taxYears: boolean }): MainTab[] { + return REFERENCE_PAGES.filter((r) => (r.tab === 'taxes' ? have.taxYears : r.source === 'adp' ? have.adp : true)).map((r) => r.tab); +} diff --git a/src/viewer/ui/App.tsx b/src/viewer/ui/App.tsx index b21ce96..84e0b0e 100644 --- a/src/viewer/ui/App.tsx +++ b/src/viewer/ui/App.tsx @@ -5,7 +5,8 @@ import { accountsIn, groupsOf, localToday, moveAccount, moveGroup, orderAccounts import { adpHistory, autoWatch, dataset, loginSaved, sheetPin, transactions, watchChosen, watching } from '../app/data'; import { connectGoogle, sheets } from '../app/google'; import { lang, t } from '../app/i18n'; -import { MAIN_TABS, prefs, setPrefs, type MainTab } from '../app/prefs'; +import { prefs, setPrefs, type MainTab } from '../app/prefs'; +import { referenceName, referenceSource, referenceTabs } from '../app/pages'; import { USERSCRIPT_ATTR, userscriptVersion } from '../app/status'; import { rovingIndex } from '../app/focus'; import { squashed } from '../app/squash'; @@ -39,9 +40,6 @@ import { useStore } from './useStore'; import { useTheme } from './useTheme'; import { PHONE_QUERY, useMedia } from './useMedia'; -// the pages of one source carry its name (owner 2026-11-29: "Fidelity Summary, Fidelity Positions and Fidelity -// Activity": the pages of the ADP 401k come next to them) -const tabName = (tab: MainTab): string => ({ summary: t('Fidelity Summary'), positions: t('Fidelity Positions'), activity: t('Fidelity Activity'), reports: t('Fidelity Reports'), taxes: t('Fidelity Taxes'), adpSummary: t('ADP Summary'), adpActivity: t('ADP Activity') })[tab]; const TAB_PANEL = 'main-tabpanel'; // open or closed panels are remembered per browser (twitter: `${PREFIX}-panel-setup`) @@ -61,9 +59,10 @@ export function App() { // year (owner 2026-12-03: "if there are no data then we have nothing to show and we should not"); a page saved but not // shown falls back to Summary const taxYearsRead = taxYears(rawDs?.taxSummary).length > 0; - const tabs = useMemo(() => MAIN_TABS.filter((x) => (x === 'taxes' ? taxYearsRead : (x !== 'adpActivity' && x !== 'adpSummary') || !!adp)), [adp, taxYearsRead]); + const tabs = useMemo(() => referenceTabs({ adp: !!adp, taxYears: taxYearsRead }), [adp, taxYearsRead]); const openTab: MainTab = tabs.indexOf(pr.tab) >= 0 ? pr.tab : 'summary'; - const adpPage = openTab === 'adpSummary' || openTab === 'adpActivity'; + // the pages of the ADP 401k show its money sources in the strip, the rest the Fidelity accounts (app/pages.ts) + const adpPage = referenceSource(openTab) === 'adp'; // the owner's own order of accounts applies everywhere: the bar, Positions, the Accounts card const ds = useMemo(() => (rawDs ? orderAccounts(rawDs, pr.accountOrder, pr.groupOrder) : null), [rawDs, pr.accountOrder, pr.groupOrder]); const g = useStore(sheets); @@ -188,7 +187,7 @@ export function App() { {flow && (
- (tab === pr.tab ? page.current?.scrollTo({ top: 0, behavior: 'smooth' }) : setPrefs({ tab }))} /> + (tab === pr.tab ? page.current?.scrollTo({ top: 0, behavior: 'smooth' }) : setPrefs({ tab }))} />
)} @@ -268,7 +267,7 @@ export function App() { {ds && (
{/* squashed above the scrolled Positions rows: the line of the pages in place of the panel */} - {flat && (tab === pr.tab ? setHeld(true) : setPrefs({ tab }))} />} + {flat && (tab === pr.tab ? setHeld(true) : setPrefs({ tab }))} />}
{/* the merge switch stands before the word Positions (owner 2026-11-27; it followed the tab since 2026-11-25), outside the tablist in the code: a tablist holds only tabs and its arrow keys move between them (the tablist @@ -280,9 +279,10 @@ export function App() {
{/* WAI-ARIA tabs: only the chosen tab is in the Tab order, Left and Right (Home, End) choose the next one */}
- {tabs.map((tab) => ( + {/* the tabs keep the table's order (app/pages.ts); the merge switch takes place 2, right after Summary */} + {tabs.map((tab, i) => ( + className={`main-tab flex h-9 items-center ${openTab === tab ? 'on' : ''}`} style={{ order: i === 0 ? 1 : i + 2 }} onClick={() => setPrefs({ tab })} data-main-tab={tab}>{referenceName(tab)} ))}
{/* drawn between Summary and Positions (owner 2026-11-27): the gap from Summary to the icon is the page