diff --git a/src/viewer/app/derive.test.ts b/src/viewer/app/derive.test.ts index 297d5ae..021338a 100644 --- a/src/viewer/app/derive.test.ts +++ b/src/viewer/app/derive.test.ts @@ -6,7 +6,7 @@ import { ROWS, SIDEBAR } from '../../shared/fixtures/grid'; import type { Account, Position } from '../../shared/model'; import { buildDataset } from '../../shared/parse'; import { sortPositions } from './position-columns'; -import { median, incomeMedian, dripMin, asKind, ALL_FOLD, toggleFold, moveAccount, moveGroup, stepMove, orderAccounts, sortAccounts, scopeOf, toggleAccount, accountsIn, allocation, groupsOf, movers, near52, positionsIn, rangeRows, payoutParts, payDatesIn, securitiesBySymbol, incomeFacts, kindOf, ofKind, toggleKind, kindOn, addMonths, localToday, incomeIn, periodBounds, rangePos, totalsOf, upcomingDividends, payoutTotal, weightedRate, adjustedDrip, rowDripAdj, rowDripAdjUsd, yieldNote, sortDividends, toggleDivSort, asDivSorts, divChips, divSizeOf, toggleDivChip, setDivSize, asDivSize, validScope, dripGap, perPayment, windowEnd, asDivWindow, symbolDrip, dripToBuy, scopeTitle, dripMinAdj, dripMinAdjUsd, framedChips } from './derive'; +import { median, incomeMedian, dripMin, asKind, ALL_FOLD, toggleFold, moveAccount, moveGroup, stepMove, orderAccounts, sortAccounts, scopeOf, toggleAccount, accountsIn, allocation, groupsOf, movers, near52, positionsIn, rangeRows, payoutParts, payDatesIn, securitiesBySymbol, incomeFacts, kindOf, ofKind, toggleKind, kindOn, addMonths, localToday, incomeIn, periodBounds, rangePos, totalsOf, upcomingDividends, payoutTotal, weightedRate, adjustedDrip, rowDripAdj, rowDripAdjUsd, yieldNote, sortDividends, toggleDivSort, asDivSorts, divChips, divSizeOf, toggleDivChip, setDivSize, asDivSize, validScope, dripGap, perPayment, windowEnd, asDivWindow, symbolDrip, dripToBuy, scopeTitle, dripMinAdj, dripMinAdjUsd, framedChips, toggleGroup } from './derive'; const ds = buildDataset({ rows: ROWS, sidebar: SIDEBAR, asOf: null, builtAt: 't' }); @@ -114,6 +114,26 @@ test('the frame goes on the highest chip chosen whole: All accounts, a group, el expect(framed('X')).toEqual([]); }); +test('the group checkbox keeps only its group when every account is chosen, else adds or takes the group', () => { + const acc = (number: string, group: string) => ({ number, name: number, group, asOf: 't' }); + const d = { ...ds, accounts: [acc('A', 'Investment'), acc('B', 'Investment'), acc('R1', 'Retirement'), acc('R2', 'Retirement'), acc('K', 'Other')] }; + const pick = (group: string, ...n: string[]) => toggleGroup(d, new Set(n), group); + // all chosen: only Investment, and the frame then goes on the group + const only = pick('Investment', 'A', 'B', 'R1', 'R2', 'K'); + expect(accountsIn(d, only).map((a) => a.number)).toEqual(['A', 'B']); + expect(Array.from(framedChips(d, new Set(['A', 'B'])))).toEqual(['group:Investment']); + // some chosen, the group whole: taken away + expect(accountsIn(d, pick('Investment', 'A', 'B', 'K')).map((a) => a.number)).toEqual(['K']); + // some chosen, the group partly or not at all: added + expect(accountsIn(d, pick('Retirement', 'A', 'R1')).map((a) => a.number)).toEqual(['A', 'R1', 'R2']); + expect(accountsIn(d, pick('Other', 'A')).map((a) => a.number)).toEqual(['A', 'K']); + // nothing chosen: the group; the only group taken away: nothing + expect(accountsIn(d, pick('Other')).map((a) => a.number)).toEqual(['K']); + expect(pick('Other', 'K')).toBe('none'); + // a group that is not there changes nothing + expect(accountsIn(d, pick('Nope', 'A')).map((a) => a.number)).toEqual(['A']); +}); + test('my order of accounts: moved within the bar, groups stay put', () => { const acc = (number: string, group: string) => ({ number, name: number, group, asOf: 't' }); const d = { ...ds, accounts: [acc('A', 'Investment'), acc('B', 'Investment'), acc('C', 'Investment'), acc('R1', 'Retirement'), acc('R2', 'Retirement')] }; diff --git a/src/viewer/app/derive.ts b/src/viewer/app/derive.ts index a6145bf..6d243fe 100644 --- a/src/viewer/app/derive.ts +++ b/src/viewer/app/derive.ts @@ -129,6 +129,19 @@ export function framedChips(ds: Dataset, shown: ReadonlySet): Set, group: string): Scope { + const mine = groupsOf(ds).find((g) => g.name === group)?.accounts.map((a) => a.number) ?? []; + if (!mine.length) return scopeOf(ds, Array.from(shown)); + if (ds.accounts.every((a) => shown.has(a.number))) return scopeOf(ds, mine); + const others = Array.from(shown).filter((n) => mine.indexOf(n) < 0); + return scopeOf(ds, mine.every((n) => shown.has(n)) ? others : others.concat(mine)); +} + /** the account checkbox (owner 2026-11-23: "only Elena and Elena ROTH IRA"): adds or removes one account */ export function toggleAccount(ds: Dataset, scope: Scope, number: string): Scope { const now = accountsIn(ds, scope).map((a) => a.number); diff --git a/src/viewer/ui/AccountTabs.tsx b/src/viewer/ui/AccountTabs.tsx index 6584d0c..9586d25 100644 --- a/src/viewer/ui/AccountTabs.tsx +++ b/src/viewer/ui/AccountTabs.tsx @@ -1,6 +1,6 @@ import { useRef, useState } from 'react'; import type { Account, Dataset } from '../../shared/model'; -import { ALL_FOLD, accountsIn, framedChips, groupsOf, scopeOf, stepMove, toggleAccount } from '../app/derive'; +import { ALL_FOLD, accountsIn, framedChips, groupsOf, stepMove, toggleAccount, toggleGroup } from '../app/derive'; import { chipActivates, chipStep } from '../app/focus'; import { t } from '../app/i18n'; import type { Scope } from '../app/prefs'; @@ -87,10 +87,6 @@ export function AccountTabs({ ds, scope, collapsed, className, onScope, onFold, {groups.map((g) => { const open = !collapsed.includes(g.name); const inGroup = g.accounts.filter((a) => shown.has(a.number)).length; - const toggleGroup = () => { - const others = Array.from(shown).filter((n) => !g.accounts.some((a) => a.number === n)); - onScope(scopeOf(ds, inGroup === g.accounts.length ? others : others.concat(g.accounts.map((a) => a.number)))); - }; return (
{ if (draggedGroup && draggedGroup !== g.name) { e.preventDefault(); setOverGroup(g.name); } }} @@ -103,7 +99,7 @@ export function AccountTabs({ ds, scope, collapsed, className, onScope, onFold, onBlur={() => keep(null)} aria-keyshortcuts={MOVE_KEYS} onKeyDown={chipKey(() => onFold(g.name), stepper(groups.map((x) => x.name), g.name, `group:${g.name}`, onMoveGroup))} data-scope={`group:${g.name}`}> {chev(open)} - 0} label={t('Show {name}', { name: g.name })} onChange={toggleGroup} /> + 0} label={t('Show {name}', { name: g.name })} onChange={() => onScope(toggleGroup(ds, shown, g.name))} /> {g.name}
{g.accounts.map((a) => accountChip(a, g.accounts))}