diff --git a/browser/data-browser/src/chunks/TablePage/QuickFilterField.test.tsx b/browser/data-browser/src/chunks/TablePage/QuickFilterField.test.tsx new file mode 100644 index 0000000000..b226affeaf --- /dev/null +++ b/browser/data-browser/src/chunks/TablePage/QuickFilterField.test.tsx @@ -0,0 +1,344 @@ +// @vitest-environment jsdom +// @wc-ignore-file +import { readFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import { useState } from 'react'; +import { afterEach, beforeAll, describe, expect, it } from 'vitest'; +import { + act, + cleanup, + fireEvent, + render, + screen, + waitFor, +} from '@testing-library/react'; +import { ThemeProvider } from 'styled-components'; +import { + core, + Datatype, + LoroLoader, + Store, + StoreContext, + useResource, + useString, + type AggregateOutcome, + type Aggregation, + type Collection, + type Property, +} from '@tomic/react'; +import { buildTheme } from '../../styling'; +import { QuickFilterField } from './QuickFilterField'; +import { + subjectListCollection, + useQuickFilter, + useQuickFilterAggregates, +} from './useQuickFilter'; +import { useAllMembers } from './helpers/useAllMembers'; + +beforeAll(async () => { + // As in TableCell.test: hand loro-crdt its WASM from disk, not over HTTP. + const wasm = await readFile( + createRequire(import.meta.url).resolve('loro-crdt/web/loro_wasm_bg.wasm'), + ); + const web = (await import('loro-crdt/web')) as unknown as { + default: (opts: { module_or_path: Uint8Array }) => Promise; + }; + await web.default({ module_or_path: wasm }); + await LoroLoader.initializeLoro(); +}); + +afterEach(cleanup); + +// `_new:` subjects stay local, so nothing here reaches for a server — the +// properties included. +const TITLE = '_new:prop-title'; +const OWNER = '_new:prop-owner'; +const DONE = '_new:prop-done'; +/** Not a searched column: only the totals read it. */ +const AMOUNT = '_new:prop-amount'; + +const properties: Property[] = [ + { subject: TITLE, datatype: Datatype.STRING, shortname: 'title' }, + { subject: OWNER, datatype: Datatype.ATOMIC_URL, shortname: 'owner' }, + { subject: DONE, datatype: Datatype.BOOLEAN, shortname: 'done' }, +].map(p => ({ ...p, description: '' })); + +const columns = properties.map(property => ({ property })); + +interface Fixture { + store: Store; + rows: string[]; + /** The rows a "Done is true" column filter lets through, in order. */ + doneRows: string[]; +} + +async function fixture(): Promise { + const store = new Store({ serverUrl: 'https://example.com' }); + + const make = async (subject: string, values: Record) => { + const resource = await store.newResource({ subject, noParent: true }); + + for (const [property, value] of Object.entries(values)) { + await resource.set(property, value as never, false); + } + + return resource.subject; + }; + + for (const [subject, name] of [ + [TITLE, 'Title'], + [OWNER, 'Owner'], + [DONE, 'Done'], + ]) { + await make(subject, { [core.properties.name]: name }); + } + + const ada = await make('_new:ada', { + [core.properties.name]: 'Ada Lovelace', + }); + + const rows = [ + await make('_new:row-1', { + [TITLE]: 'Buy oat milk', + [DONE]: true, + [AMOUNT]: 3, + }), + await make('_new:row-2', { + [TITLE]: 'Buy cow milk', + [OWNER]: ada, + [AMOUNT]: 5, + }), + await make('_new:row-3', { + [TITLE]: 'Fix the roof', + [DONE]: true, + [AMOUNT]: 10, + }), + ]; + + return { store, rows, doneRows: [rows[0], rows[2]] }; +} + +/** A sum and an average of Amount under the table, as a view configures them. */ +const TOTALS: Aggregation = { + aggregates: [ + { id: 'sum', property: AMOUNT, function: 'sum' }, + { id: 'avg', property: AMOUNT, function: 'avg' }, + ], +}; + +const storeTotals = (sum: number, count: number): AggregateOutcome[] => [ + { id: 'sum', property: AMOUNT, function: 'sum', value: sum, count }, + { id: 'avg', property: AMOUNT, function: 'avg', value: sum / count, count }, +]; + +/** Amounts are 3, 5 and 10; the Done rows hold 3 and 10. */ +const ALL_TOTALS = storeTotals(18, 3); +const DONE_TOTALS = storeTotals(13, 2); + +function RowTitle({ subject }: { subject: string }) { + const [title] = useString(useResource(subject), TITLE); + + return
  • {title}
  • ; +} + +/** + * The toolbar field over a view's rows, wired the way the table page wires it: + * the column filter decides which collection the view has (the store answers + * it), and the quick filter narrows that collection further. + */ +function Harness({ rows, doneRows }: Omit) { + const [text, setText] = useState(''); + const [onlyDone, setOnlyDone] = useState(false); + const [all] = useState(() => subjectListCollection(rows)); + const [done] = useState(() => subjectListCollection(doneRows)); + const collection: Collection = onlyDone ? done : all; + + const quickFilter = useQuickFilter(collection, columns, text); + const shown = useAllMembers(quickFilter.collection); + // What the store answers for the view's own query (column filter included): + // the totals over every row it matches. + const storeOutcomes = onlyDone ? DONE_TOTALS : ALL_TOTALS; + const totals = useQuickFilterAggregates(quickFilter, TOTALS, storeOutcomes); + const total = (id: string) => + `${totals.find(outcome => outcome.id === id)?.value ?? '—'}`; + + return ( + <> + + +
      + {shown.map(subject => ( + + ))} +
    + + {quickFilter.collection.totalMembers} + + {total('sum')} + {total('avg')} + + ); +} + +async function renderHarness() { + const { store, ...rest } = await fixture(); + + render( + + + + + , + ); + + await expectRows(['Buy oat milk', 'Buy cow milk', 'Fix the roof']); +} + +async function expectRows(titles: string[]) { + await waitFor(() => + expect( + screen + .getByRole('list', { name: 'Rows' }) + .querySelectorAll('li') + .values() + .map(li => li.textContent) + .toArray(), + ).toEqual(titles), + ); +} + +const field = () => + screen.getByRole('textbox', { name: 'Find rows containing text' }); + +async function type(text: string) { + await act(async () => { + fireEvent.change(field(), { target: { value: text } }); + }); +} + +describe('the quick filter field', () => { + it('keeps the rows that show the text, in any column', async () => { + await renderHarness(); + + await type('MILK'); + await expectRows(['Buy oat milk', 'Buy cow milk']); + + // A reference, by its title. + await type('lovelace'); + await expectRows(['Buy cow milk']); + + // A ticked checkbox, by its column's label. + await type('done'); + await expectRows(['Buy oat milk', 'Fix the roof']); + }); + + it('says so when nothing matches, and restores every row when cleared', async () => { + await renderHarness(); + + await type('garden'); + await expectRows([]); + + await act(async () => { + fireEvent.click(screen.getByTitle('Clear')); + }); + expect((field() as HTMLInputElement).value).toBe(''); + await expectRows(['Buy oat milk', 'Buy cow milk', 'Fix the roof']); + }); + + it('clears with Escape', async () => { + await renderHarness(); + + await type('roof'); + await expectRows(['Fix the roof']); + + await act(async () => { + fireEvent.keyDown(field(), { key: 'Escape' }); + }); + expect((field() as HTMLInputElement).value).toBe(''); + await expectRows(['Buy oat milk', 'Buy cow milk', 'Fix the roof']); + }); + + it('only offers the clear button while there is text', async () => { + await renderHarness(); + + expect(screen.queryByTitle('Clear')).toBeNull(); + await type('milk'); + expect(screen.getByTitle('Clear')).toBeTruthy(); + }); + + it('narrows what a column filter lets through, and not more', async () => { + await renderHarness(); + + await act(async () => { + fireEvent.click(screen.getByLabelText('Column filter: Done')); + }); + await expectRows(['Buy oat milk', 'Fix the roof']); + + await type('milk'); + // "Buy cow milk" matches the text, but the column filter keeps it out. + await expectRows(['Buy oat milk']); + + // Dropping the column filter keeps the text, and lets the other match in. + await act(async () => { + fireEvent.click(screen.getByLabelText('Column filter: Done')); + }); + await expectRows(['Buy oat milk', 'Buy cow milk']); + }); + + it('makes the totals cover the matching rows, and restores them when cleared', async () => { + await renderHarness(); + await expectTotals({ rows: '3', sum: '18', average: '6' }); + + await type('milk'); + await expectRows(['Buy oat milk', 'Buy cow milk']); + // 3 + 5, over the two rows the footer counts. + await expectTotals({ rows: '2', sum: '8', average: '4' }); + + await act(async () => { + fireEvent.click(screen.getByTitle('Clear')); + }); + await expectTotals({ rows: '3', sum: '18', average: '6' }); + }); + + it('totals only what both the column filter and the quick filter let through', async () => { + await renderHarness(); + + await act(async () => { + fireEvent.click(screen.getByLabelText('Column filter: Done')); + }); + await expectTotals({ rows: '2', sum: '13', average: '6.5' }); + + await type('milk'); + await expectRows(['Buy oat milk']); + await expectTotals({ rows: '1', sum: '3', average: '3' }); + + await type('nothing shows this'); + await expectRows([]); + // No row to add up is not a sum of zero. + await expectTotals({ rows: '0', sum: '—', average: '—' }); + }); +}); + +async function expectTotals(expected: { + rows: string; + sum: string; + average: string; +}) { + const read = (name: string) => + screen.getByRole('status', { name }).textContent; + + await waitFor(() => + expect({ + rows: read('Rows counted'), + sum: read('Sum'), + average: read('Average'), + }).toEqual(expected), + ); +} diff --git a/browser/data-browser/src/chunks/TablePage/QuickFilterField.tsx b/browser/data-browser/src/chunks/TablePage/QuickFilterField.tsx new file mode 100644 index 0000000000..e1af8736ee --- /dev/null +++ b/browser/data-browser/src/chunks/TablePage/QuickFilterField.tsx @@ -0,0 +1,168 @@ +import { useRef, useState, type JSX } from 'react'; +import { styled } from 'styled-components'; +import { FaMagnifyingGlass, FaXmark } from 'react-icons/fa6'; +import { IconButton } from '@components/IconButton/IconButton'; + +interface QuickFilterFieldProps { + value: string; + onChange: (value: string) => void; +} + +/** Below this width the field folds into its icon until you open it. */ +const COLLAPSE_BELOW = '40rem'; + +/** + * The table toolbar's quick filter: type, and only the rows that show that text + * in some column stay. Not a search of the drive — that is the header's Search. + * + * ✕ or Escape clears it. On a phone it takes the room of one icon until opened, + * and folds back when it is left empty. + */ +export function QuickFilterField({ + value, + onChange, +}: QuickFilterFieldProps): JSX.Element { + const inputRef = useRef(null); + const [opened, setOpened] = useState(false); + // Something typed keeps it open: hiding a filter that is narrowing the rows + // would leave you wondering where they went. + const expanded = opened || value !== ''; + + const open = () => { + setOpened(true); + // The input is only laid out once it is expanded. + requestAnimationFrame(() => inputRef.current?.focus()); + }; + + const clear = () => { + onChange(''); + inputRef.current?.focus(); + }; + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key !== 'Escape') { + return; + } + + // Escape belongs to the field here, not to a dialog or the grid behind it. + e.stopPropagation(); + + if (value !== '') { + onChange(''); + } else { + setOpened(false); + inputRef.current?.blur(); + } + }; + + return ( + + + + + + + onChange(e.target.value)} + onKeyDown={handleKeyDown} + onFocus={() => setOpened(true)} + onBlur={() => setOpened(false)} + /> + {value !== '' && ( + e.preventDefault()} + > + + + )} + + + ); +} + +const Wrapper = styled.div` + display: flex; + align-items: center; + min-width: 0; +`; + +const OpenButton = styled.button` + display: none; + align-items: center; + justify-content: center; + height: 1.85rem; + width: 1.85rem; + border: none; + border-radius: ${p => p.theme.radius}; + background-color: transparent; + color: ${p => p.theme.colors.textLight}; + cursor: pointer; + + &:hover, + &:focus-visible { + background-color: ${p => p.theme.colors.bg1}; + color: ${p => p.theme.colors.text}; + } + + @media (max-width: ${COLLAPSE_BELOW}) { + ${Wrapper}[data-expanded='false'] & { + display: inline-flex; + } + } +`; + +const Field = styled.div` + display: flex; + align-items: center; + gap: 0.35rem; + height: 1.85rem; + width: 13rem; + padding-inline: 0.5rem 0.25rem; + border: 1px solid ${p => p.theme.colors.bg2}; + border-radius: ${p => p.theme.radius}; + background-color: ${p => p.theme.colors.bg}; + + & > svg { + flex-shrink: 0; + color: ${p => p.theme.colors.textLight}; + font-size: 0.85em; + } + + &:focus-within { + border-color: ${p => p.theme.colors.main}; + } + + @media (max-width: ${COLLAPSE_BELOW}) { + width: 10rem; + + ${Wrapper}[data-expanded='false'] & { + display: none; + } + } +`; + +const Input = styled.input` + flex: 1; + min-width: 0; + height: 100%; + border: none; + outline: none; + padding: 0; + font-size: 0.9em; + color: ${p => p.theme.colors.text}; + background: transparent; +`; diff --git a/browser/data-browser/src/chunks/TablePage/TableResource.tsx b/browser/data-browser/src/chunks/TablePage/TableResource.tsx index f9f90439f9..3bfbbfa720 100644 --- a/browser/data-browser/src/chunks/TablePage/TableResource.tsx +++ b/browser/data-browser/src/chunks/TablePage/TableResource.tsx @@ -3,6 +3,7 @@ import { dataBrowser, unknownSubject, type AggregateFunction, + type Aggregation, useCanWrite, useStore, type DataBrowser, @@ -82,6 +83,7 @@ import type { AggregateTarget } from './tablePageContext'; import type { DerivedColumnSpec } from './derivedColumns'; import { TablePresenceContext, useTablePresence } from './TablePresence'; import { withRowDefaults } from './rowDefaults'; +import { useQuickFilter, useQuickFilterAggregates } from './useQuickFilter'; interface TableResourceProps { resource: Resource; @@ -105,6 +107,8 @@ const columnToKey = (column: TableColumn) => column.key; /** Draft rows minted ahead, so adding a row never waits on a signature. */ const DRAFT_POOL_SIZE = 2; +const NO_ROWS: string[] = []; + /** * Which rows a query asks for, as one comparable string — so the collection in * hand can be told apart from the one that was asked for. Built from the same @@ -193,6 +197,46 @@ export const TableResource: React.FC = ({ ); const calendarRow = useTableCalendarRow(tableClass, allColumns); + // The toolbar's quick filter. Session state, not View configuration: it is + // tagged with the view it was typed in, so switching views (or leaving the + // table) starts that view unfiltered. See `quickFilter.ts`. + const [quickFilterState, setQuickFilterState] = useState<{ + view: string | undefined; + text: string; + }>({ view: undefined, text: '' }); + const quickFilterText = + quickFilterState.view === activeView ? quickFilterState.text : ''; + const setQuickFilterText = useCallback( + (text: string) => setQuickFilterState({ view: activeView, text }), + [activeView], + ); + + // A dashboard or an app shows no rows of its own, so there's nothing to narrow. + const showsRows = appView === undefined && viewKind !== 'dashboard'; + + // The visible stored columns, each language of a split column on its own: + // what the quick filter reads a row's text from. + const quickFilterSources = useMemo( + () => + columns.flatMap(c => + c.property + ? [{ property: c.property, languageTag: c.languageTag }] + : [], + ), + [columns], + ); + + // Narrows the view's own (column-filtered, sorted) rows. Every row view below + // reads `quickFilter.collection`, which is the view's collection while the + // field is empty. + const quickFilter = useQuickFilter( + collection, + quickFilterSources, + showsRows ? quickFilterText : '', + ); + const rowCollection = quickFilter.collection; + const quickFilterQuery = quickFilterText.trim(); + // The rendered column's property, per grid index (split columns repeat // theirs) — for consumers that need index alignment (presence). Virtual // columns have no property and are appended after the real ones, so dropping @@ -491,25 +535,42 @@ export const TableResource: React.FC = ({ [gridColumns, setViewColumnOrder, setViewColumns], ); + // Rebuilt every render (it carries a quantized `now`), so keyed on content. + const aggregationKey = JSON.stringify( + toAggregation( + viewAggregates, + viewGroupByColumn, + viewGroupGranularity, + derivedSpecs, + ) ?? null, + ); + const aggregation = useMemo( + () => (JSON.parse(aggregationKey) as Aggregation | null) ?? undefined, + [aggregationKey], + ); + // Totals ride their own query so they can be re-read on every edit without // clearing the grid's pages. See `useTableAggregates`. - const aggregateOutcomes = useTableAggregates({ + const viewAggregateOutcomes = useTableAggregates({ property: core.properties.parent, value: resource.subject, filters: queryFilters, expressionFilters: queryExpressionFilters, - aggregation: toAggregation( - viewAggregates, - viewGroupByColumn, - viewGroupGranularity, - derivedSpecs, - ), + aggregation, drive: store.getDrive(), server: resource.subject.startsWith('http') ? new URL(resource.subject).origin : undefined, }); + // Under the quick filter, the totals cover the rows that match — the rows on + // screen, and the ones the footer counts. + const aggregateOutcomes = useQuickFilterAggregates( + quickFilter, + aggregation, + viewAggregateOutcomes, + ); + const [columnSizes, handleColumnResize] = useHandleColumnResize(resource); // Widths follow the *rendered* order, since that is how `tableColumnWidths` @@ -576,7 +637,7 @@ export const TableResource: React.FC = ({ const handlePaste = useHandlePaste( resource, - collection, + rowCollection, tableClass, invalidateCollection, addItemsToHistoryStack, @@ -870,7 +931,10 @@ export const TableResource: React.FC = ({ */ const handleInsertRowBelow = useCallback( (index: number): boolean => { + // Rows hidden by the quick filter sit between the visible ones, so + // "directly below" has no position to stand for. if ( + quickFilter.active || sorting.prop !== dataBrowser.properties.sortOrder || sorting.sortDesc ) { @@ -949,6 +1013,7 @@ export const TableResource: React.FC = ({ }, [ sorting, + quickFilter.active, memberCount, newRowSubjects, sessionSortOrders, @@ -968,15 +1033,29 @@ export const TableResource: React.FC = ({ ); }, [withSessionRow]); + // What the grid draws. Under the quick filter: the matching rows and nothing + // else — no session rows, since a row being typed would vanish the moment it + // stopped matching. The session rows are kept, and come back on clearing. + const gridMemberCount = quickFilter.active + ? quickFilter.matches.length + : memberCount; + const gridNewRowSubjects = quickFilter.active ? NO_ROWS : newRowSubjects; + const itemKey = useCallback( (index: number) => { + if (quickFilter.active) { + // By subject: the same row keeps its cells while the matches around it + // come and go. + return `match-${quickFilter.matches[index] ?? index}`; + } + if (index < memberCount) { return `member-${index}`; } return newRowSubjects[index - memberCount] ?? `new-row-fallback-${index}`; }, - [memberCount, newRowSubjects], + [quickFilter.active, quickFilter.matches, memberCount, newRowSubjects], ); // See `TablePageContextType.rowSource`. The SAME index→row mapping the grid @@ -984,18 +1063,19 @@ export const TableResource: React.FC = ({ // come from the collection, session rows from `newRowSubjects`. A session row // keeps its `_new:` key for its whole life here — materializing does not turn // it into a member — so resolving everything through the collection would - // address the wrong row. + // address the wrong row. Under the quick filter the grid draws the matches, + // so this follows `rowCollection` and the grid's counts too. const rowSource = useCallback( (index: number): RowSource | undefined => { - if (index < memberCount) { - return { kind: 'member', collection, index }; + if (index < gridMemberCount) { + return { kind: 'member', collection: rowCollection, index }; } - const key = newRowSubjects[index - memberCount]; + const key = gridNewRowSubjects[index - gridMemberCount]; return key ? { kind: 'session', key } : undefined; }, - [collection, memberCount, newRowSubjects], + [rowCollection, gridMemberCount, gridNewRowSubjects], ); const [showExpandedRowDialog, setShowExpandedRowDialog] = useState(false); @@ -1003,11 +1083,11 @@ export const TableResource: React.FC = ({ const handleRowExpand = useCallback( async (index: number) => { - const row = await collection.getMemberWithIndex(index); + const row = await rowCollection.getMemberWithIndex(index); setExpandedRowSubject(row); setShowExpandedRowDialog(true); }, - [collection], + [rowCollection], ); const tablePageContext: TablePageContextType = useMemo( @@ -1032,7 +1112,8 @@ export const TableResource: React.FC = ({ removeRowAction, aggregates: viewAggregates, aggregateOutcomes, - rowCount: collection.totalMembers, + // The rows on screen: under the quick filter, the ones that match. + rowCount: rowCollection.totalMembers, setColumnAggregate, removeAggregateRow, canWriteTable: canWrite, @@ -1062,7 +1143,7 @@ export const TableResource: React.FC = ({ allColumns, viewAggregates, aggregateOutcomes, - collection.totalMembers, + rowCollection.totalMembers, setColumnAggregate, removeAggregateRow, canWrite, @@ -1084,10 +1165,10 @@ export const TableResource: React.FC = ({ // Using `collection.getMemberWithIndex` for everything would mis-resolve // session rows (they are drafts, not addressed by collection index // here). - const isMember = index < memberCount; + const isMember = index < gridMemberCount; const subject = isMember - ? await collection.getMemberWithIndex(index) - : newRowSubjects[index - memberCount]; + ? await rowCollection.getMemberWithIndex(index) + : gridNewRowSubjects[index - gridMemberCount]; if (!subject) { return; @@ -1123,11 +1204,11 @@ export const TableResource: React.FC = ({ // update back to the pre-delete state. }, [ - collection, + rowCollection, store, addItemsToHistoryStack, - memberCount, - newRowSubjects, + gridMemberCount, + gridNewRowSubjects, decrementMemberCount, ], ); @@ -1137,15 +1218,20 @@ export const TableResource: React.FC = ({ // cards via TablePresenceContext). const { presenceValue, handleSelectedCellChange } = useTablePresence( resource.subject, - { collection, columns: columnProperties, memberCount, newRowSubjects }, + { + collection: rowCollection, + columns: columnProperties, + memberCount: gridMemberCount, + newRowSubjects: gridNewRowSubjects, + }, ); const handleClearCells = useHandleClearCells( - collection, + rowCollection, addItemsToHistoryStack, ); - const handleCopyCommandByProperty = useHandleCopyCommand(collection); + const handleCopyCommandByProperty = useHandleCopyCommand(rowCollection); // The grid works in rendered (TableColumn) cells; the copy helper works // per property, so unwrap at the boundary. @@ -1163,12 +1249,15 @@ export const TableResource: React.FC = ({ const Row = useCallback( ({ index }: { index: number }) => { - if (index < memberCount) { + if (index < gridMemberCount) { return ( ); } @@ -1194,8 +1283,11 @@ export const TableResource: React.FC = ({ // Resource can update a lot but its internals are stable so removing it from the array saves a lot of rerenders and shouldn't cause issues. // eslint-disable-next-line react-hooks/exhaustive-deps [ - collection, + rowCollection, + quickFilter.active, + quickFilter.matches, gridColumns, + gridMemberCount, memberCount, newRowSubjects, resource.subject, @@ -1229,6 +1321,11 @@ export const TableResource: React.FC = ({ canWrite={canWrite} quickAdd={viewQuickAdd} setQuickAdd={setViewQuickAdd} + quickFilter={ + showsRows + ? { value: quickFilterText, onChange: setQuickFilterText } + : undefined + } /> )} {/* Above the view switch, not inside the table branch: the filter @@ -1241,6 +1338,13 @@ export const TableResource: React.FC = ({ derivedColumns={derivedSpecs} /> )} + {quickFilter.active && + !quickFilter.loading && + quickFilter.matches.length === 0 && ( + + {`No rows show “${quickFilterQuery}”.`} + + )} {/* Above the view switch on purpose: a grocery board wants its "Add * item" as much as the list does. Writers only — a create button that * will be rejected is worse than none. */} @@ -1279,7 +1383,7 @@ export const TableResource: React.FC = ({ tableClass={tableClass} allColumns={allColumns} columns={uniqueColumnProperties} - collection={collection} + collection={rowCollection} ready={ready} viewGroupBy={viewGroupBy} setViewGroupBy={setViewGroupBy} @@ -1290,7 +1394,7 @@ export const TableResource: React.FC = ({ tableSubject={resource.subject} tableClass={tableClass} allColumns={allColumns} - collection={collection} + collection={rowCollection} ready={ready} viewGroupBy={viewGroupBy} setViewGroupBy={setViewGroupBy} @@ -1301,7 +1405,7 @@ export const TableResource: React.FC = ({ tableSubject={resource.subject} tableClass={tableClass} allColumns={allColumns} - collection={collection} + collection={rowCollection} ready={ready} viewGroupBy={viewGroupBy} setViewGroupBy={setViewGroupBy} @@ -1334,7 +1438,7 @@ export const TableResource: React.FC = ({ // seconds with no row to type into. Members that arrive during // load shift the row's index, not its key (`itemKey` offsets by // `memberCount`), so nothing remounts. - itemCount={memberCount + newRowSubjects.length} + itemCount={gridMemberCount + gridNewRowSubjects.length} itemKey={itemKey} columnToKey={columnToKey} labelledBy={titleId} @@ -1356,7 +1460,8 @@ export const TableResource: React.FC = ({ {Row} {/* Under the grid, where a spreadsheet's totals live. The numbers - * come from the store, over every row the view matches. Not + * come from the store, over every row the view matches (under + * a quick filter, over the rows that match it). Not * mounted at all without totals: it resolves a title per column, * and a table with no totals should pay nothing for that. */} {viewAggregates.length > 0 && viewGroupByColumn && ( @@ -1389,6 +1494,12 @@ export const TableResource: React.FC = ({ * stays reachable. An iframe cannot report how tall its document is, so the * box has to be decided out here. */ +const NoMatches = styled.p` + margin: 0; + padding-block: 0.5rem; + color: ${p => p.theme.colors.textLight}; +`; + const AppViewWrapper = styled.div` display: flex; flex-direction: column; diff --git a/browser/data-browser/src/chunks/TablePage/TableRow.tsx b/browser/data-browser/src/chunks/TablePage/TableRow.tsx index d8bfb0fae4..4d511285d7 100644 --- a/browser/data-browser/src/chunks/TablePage/TableRow.tsx +++ b/browser/data-browser/src/chunks/TablePage/TableRow.tsx @@ -27,6 +27,11 @@ interface TableRowProps { collection: Collection; index: number; columns: TableColumn[]; + /** + * The row's subject when the caller already knows it (the quick filter's + * matches), so the row draws at once instead of resolving its index first. + */ + subject?: string; } const WarningIcon = styled(FaTriangleExclamation)` @@ -76,6 +81,7 @@ export function TableRow({ collection, index, columns, + subject, }: TableRowProps): JSX.Element { const resource = useMemberFromCollection(collection, index); @@ -102,9 +108,10 @@ export function TableRow({ } const displaySubject = - lastKnownRef.current?.index === index + subject ?? + (lastKnownRef.current?.index === index ? lastKnownRef.current.subject - : undefined; + : undefined); if (!displaySubject) { return ( diff --git a/browser/data-browser/src/chunks/TablePage/TableViewTabs.tsx b/browser/data-browser/src/chunks/TablePage/TableViewTabs.tsx index d3af9e4cbb..73f4705e1c 100644 --- a/browser/data-browser/src/chunks/TablePage/TableViewTabs.tsx +++ b/browser/data-browser/src/chunks/TablePage/TableViewTabs.tsx @@ -51,6 +51,7 @@ import { } from './viewTypeChoice'; import { QuickAddDialog } from './QuickAddDialog'; import type { QuickAddSpec } from './quickAdd'; +import { QuickFilterField } from './QuickFilterField'; interface TableViewTabsProps { /** The class of this table's rows, which decides what apps can show it. */ @@ -87,6 +88,11 @@ interface TableViewTabsProps { quickAdd: QuickAddSpec | undefined; /** Persist the active view's create button (undefined removes it). */ setQuickAdd: (spec: QuickAddSpec | undefined) => void; + /** + * The quick filter over the rows the view shows. Absent for views that show + * no rows (a dashboard, an app). + */ + quickFilter?: { value: string; onChange: (value: string) => void }; } /** @@ -115,6 +121,7 @@ export function TableViewTabs({ canWrite, quickAdd, setQuickAdd, + quickFilter, }: TableViewTabsProps): JSX.Element { // A table with no saved views yet still shows one implicit "Default View" tab. const tabs = views.length > 0 ? views : [undefined]; @@ -158,6 +165,12 @@ export function TableViewTabs({ )} + {quickFilter && ( + + )} `https://example.com/properties/${name}`; + +const column = ( + name: string, + datatype: Datatype, + extra: Partial = {}, +): QuickFilterColumn => ({ + property: P(name), + datatype, + label: name[0].toUpperCase() + name.slice(1), + ...extra, +}); + +const TITLES: Record = { + 'https://example.com/people/ada': 'Ada Lovelace', + 'https://example.com/tags/urgent': 'urgent', + 'https://example.com/tags/later': 'later', +}; + +const context: QuickFilterContext = { + titleOf: subject => TITLES[subject], + contentLanguage: 'en', +}; + +const row = (values: Record): QuickFilterRow => ({ + get: property => values[property], +}); + +/** Does a single cell of this column, holding this value, match the query? */ +const matches = (col: QuickFilterColumn, value: JSONValue, query: string) => + rowMatchesQuickFilter( + row({ [col.property]: value }), + [col], + normalizeQuickFilter(query), + context, + ); + +describe('quick filter matching', () => { + it('finds text anywhere in a string, ignoring case and outer spaces', () => { + const title = column('title', Datatype.STRING); + + expect(matches(title, 'Buy Oat Milk', 'oat')).toBe(true); + expect(matches(title, 'Buy Oat Milk', ' MILK ')).toBe(true); + expect(matches(title, 'Buy Oat Milk', 'cheese')).toBe(false); + }); + + it('matches every row when the query is empty', () => { + expect(matches(column('title', Datatype.STRING), undefined, ' ')).toBe( + true, + ); + }); + + it('reads markdown as plain text', () => { + const notes = column('notes', Datatype.MARKDOWN); + const value = 'Call **the plumber** about [the leak](https://x.test/1)'; + + expect(matches(notes, value, 'call the plumber')).toBe(true); + expect(matches(notes, value, 'about the leak')).toBe(true); + expect(matches(notes, value, 'dentist')).toBe(false); + }); + + it('matches a number as shown and as typed', () => { + const count = column('count', Datatype.INTEGER); + const shown = new Intl.NumberFormat('default').format(1234567); + + expect(matches(count, 1234567, shown)).toBe(true); + expect(matches(count, 1234567, '1234567')).toBe(true); + expect(matches(count, 1234567, '999')).toBe(false); + }); + + it('matches a float with its formatting, e.g. as a percentage', () => { + const progress = column('progress', Datatype.FLOAT, { + numberFormatting: urls.instances.numberFormats.percentage, + }); + const shown = cellTexts(42, progress, context)[0]; + + expect(shown).toContain('%'); + expect(matches(progress, 42, shown)).toBe(true); + }); + + it('matches a date as it is displayed, not as it is stored', () => { + const due = column('due', Datatype.DATE, { + dateFormat: urls.instances.dateFormats.localLong, + }); + // A civil date is local midnight of that day (#1812), whatever the zone. + const shown = formatDate( + urls.instances.dateFormats.localLong, + new Date(2026, 9, 2), + false, + ); + + expect(cellTexts('2026-10-02', due, context)).toEqual([shown]); + expect(matches(due, '2026-10-02', shown)).toBe(true); + // The month name is in the long format, so part of it finds the row too. + expect(matches(due, '2026-10-02', shown.split(' ')[0])).toBe(true); + expect(matches(due, '2026-10-02', '2026-10-02')).toBe(false); + }); + + it('uses the numeric local format for a date column without one', () => { + const due = column('due', Datatype.DATE); + const shown = formatDate( + urls.instances.dateFormats.localNumeric, + new Date(2026, 9, 2), + false, + ); + + expect(cellTexts('2026-10-02', due, context)).toEqual([shown]); + }); + + it('matches a timestamp as displayed, with its time', () => { + const at = column('at', Datatype.TIMESTAMP); + const instant = new Date(2026, 0, 15, 14, 30).getTime(); + const shown = formatDate( + urls.instances.dateFormats.localNumeric, + new Date(instant), + true, + ); + + expect(matches(at, instant, shown)).toBe(true); + }); + + it('reads a ticked checkbox as its column label, an unticked one as nothing', () => { + const done = column('done', Datatype.BOOLEAN); + + expect(matches(done, true, 'done')).toBe(true); + expect(matches(done, false, 'done')).toBe(false); + expect(matches(done, false, 'false')).toBe(false); + }); + + it('matches a reference by its title', () => { + const owner = column('owner', Datatype.ATOMIC_URL); + + expect(matches(owner, 'https://example.com/people/ada', 'lovelace')).toBe( + true, + ); + // The URL is not what the cell shows once the title is known. + expect(matches(owner, 'https://example.com/people/ada', 'people')).toBe( + false, + ); + }); + + it('falls back to the subject for a reference with no title yet', () => { + const owner = column('owner', Datatype.ATOMIC_URL); + + expect(matches(owner, 'https://example.com/people/bob', 'bob')).toBe(true); + }); + + it('matches a multi-value cell by any of its items', () => { + const tags = column('tags', Datatype.RESOURCEARRAY); + const value = [ + 'https://example.com/tags/urgent', + 'https://example.com/tags/later', + ]; + + expect(matches(tags, value, 'urgent')).toBe(true); + expect(matches(tags, value, 'LATER')).toBe(true); + expect(matches(tags, value, 'someday')).toBe(false); + }); + + it('reads localized text in the content language or the split language', () => { + const name = column('name', Datatype.LOCALIZEDTEXT); + const value = { en: 'Apple', nl: 'Appel' }; + + expect(matches(name, value, 'apple')).toBe(true); + expect(matches(name, value, 'appel')).toBe(false); + expect(matches({ ...name, languageTag: 'nl' }, value, 'appel')).toBe(true); + }); + + it('matches a row when any one of its columns matches', () => { + const columns = [ + column('title', Datatype.STRING), + column('owner', Datatype.ATOMIC_URL), + ]; + const r = row({ + [P('title')]: 'Fix the roof', + [P('owner')]: 'https://example.com/people/ada', + }); + + expect(rowMatchesQuickFilter(r, columns, 'roof', context)).toBe(true); + expect(rowMatchesQuickFilter(r, columns, 'ada', context)).toBe(true); + expect(rowMatchesQuickFilter(r, columns, 'garden', context)).toBe(false); + }); +}); + +describe('filtering a list of rows', () => { + const title = column('title', Datatype.STRING); + const rows: Record = { + a: row({ [P('title')]: 'Apples' }), + b: row({ [P('title')]: 'Bananas' }), + c: row({ [P('title')]: 'Pineapple' }), + }; + + it('keeps the matching rows in the order it was given', () => { + expect( + filterSubjectsByQuickFilter( + ['c', 'b', 'a'], + s => rows[s], + [title], + 'APPLE', + context, + ), + ).toEqual(['c', 'a']); + }); + + it('returns the list unchanged for an empty query', () => { + const subjects = ['a', 'b', 'c']; + + expect( + filterSubjectsByQuickFilter( + subjects, + s => rows[s], + [title], + ' ', + context, + ), + ).toBe(subjects); + }); + + it('leaves out a row that has not loaded', () => { + expect( + filterSubjectsByQuickFilter( + ['a', 'missing'], + s => rows[s], + [title], + 'a', + context, + ), + ).toEqual(['a']); + }); +}); + +describe('referenced subjects', () => { + it('collects every reference the reference columns show, once', () => { + const columns = [ + column('title', Datatype.STRING), + column('owner', Datatype.ATOMIC_URL), + column('tags', Datatype.RESOURCEARRAY), + ]; + + expect( + referencedSubjects( + [ + row({ + [P('title')]: 'https://not-a-reference.test', + [P('owner')]: 'https://example.com/people/ada', + [P('tags')]: ['https://example.com/tags/urgent'], + }), + row({ [P('owner')]: 'https://example.com/people/ada' }), + ], + columns, + ).sort(), + ).toEqual([ + 'https://example.com/people/ada', + 'https://example.com/tags/urgent', + ]); + }); +}); diff --git a/browser/data-browser/src/chunks/TablePage/quickFilter.ts b/browser/data-browser/src/chunks/TablePage/quickFilter.ts new file mode 100644 index 0000000000..37723b9f3d --- /dev/null +++ b/browser/data-browser/src/chunks/TablePage/quickFilter.ts @@ -0,0 +1,277 @@ +import { + Datatype, + localizeText, + urls, + type JSONValue, + type LocalizedText, +} from '@tomic/react'; +import { formatDate } from '@helpers/dates/formatDate'; +import { calendarDateToLocalDate } from '@helpers/dates/calendarDate'; +import { markdownToPlainText } from '@helpers/markdown'; +import { formatNumber } from './helpers/formatNumber'; + +/** + * The quick filter: one text field that keeps the rows where any visible + * column *shows* that text. It matches what a person reads in the cell, not + * what is stored — a date as it is formatted, a reference by its title — so it + * answers "where is the row that says X", which the per-column filters (exact + * stored values, one column at a time) do not. + * + * It narrows the rows the view already has (after its column filters, in its + * sort order) on the client, and is not stored on the View: it resets when you + * switch views or leave the table. + */ + +/** What a column needs to be rendered as text, read once per column. */ +export interface QuickFilterColumn { + property: string; + datatype: Datatype; + /** The column heading. A ticked checkbox reads as this. */ + label: string; + /** A split LocalizedText column shows exactly this language. */ + languageTag?: string; + /** `constraints.dateFormat`; the numeric local format when unset. */ + dateFormat?: string; + /** `constraints.numberFormatting` (percentage, currency). */ + numberFormatting?: string; + decimalPlaces?: number; + currency?: string; +} + +export interface QuickFilterContext { + /** + * The title a reference shows (name, shortname, filename, else the subject), + * or undefined while it has not loaded. + */ + titleOf: (subject: string) => string | undefined; + /** The app's content language, for an unsplit LocalizedText column. */ + contentLanguage: string; +} + +/** The query as it is compared: trimmed and lower-cased. Empty = no filter. */ +export function normalizeQuickFilter(query: string): string { + return query.trim().toLocaleLowerCase(); +} + +const isNumber = (value: unknown): value is number => + typeof value === 'number' && Number.isFinite(value); + +/** + * The texts one cell shows, per datatype. More than one when a cell has several + * items (a multi-value reference) or reads two ways (a number as formatted and + * as typed). + */ +export function cellTexts( + value: JSONValue, + column: QuickFilterColumn, + context: QuickFilterContext, +): string[] { + if (value === undefined || value === null || value === '') { + return []; + } + + switch (column.datatype) { + case Datatype.MARKDOWN: + // The cell shows the source; plain text also lets "a b" find "a **b**". + return typeof value === 'string' + ? [value, markdownToPlainText(value)] + : []; + + case Datatype.INTEGER: + + case Datatype.FLOAT: { + if (!isNumber(value)) { + return [String(value)]; + } + + const shown = formatNumber( + value, + column.datatype === Datatype.INTEGER ? 0 : column.decimalPlaces, + column.numberFormatting, + column.currency, + ); + + // As shown ("1,234.5", "€ 12.00", "40%"), and as typed ("1234.5"), since + // nobody types the grouping separator when looking for a number. + return [shown, String(value)]; + } + + case Datatype.DATE: { + const date = calendarDateToLocalDate(value); + + return date + ? [formatDate(dateFormatOf(column), date, false)] + : [String(value)]; + } + + case Datatype.TIMESTAMP: + return isNumber(value) + ? [formatDate(dateFormatOf(column), new Date(value), true)] + : []; + + case Datatype.BOOLEAN: + // The cell is a checkbox with no text of its own. A ticked one reads as + // its column — "done" finds the done rows; an unticked one says nothing. + return value === true ? [column.label] : []; + + case Datatype.ATOMIC_URL: + return typeof value === 'string' ? titles([value], context) : []; + + case Datatype.RESOURCEARRAY: + return Array.isArray(value) + ? titles( + value.filter((v): v is string => typeof v === 'string'), + context, + ) + : []; + + case Datatype.LOCALIZEDTEXT: { + if (typeof value !== 'object' || Array.isArray(value)) { + return []; + } + + const localized = value as LocalizedText; + const text = + column.languageTag !== undefined + ? localized[column.languageTag] + : localizeText(localized, context.contentLanguage); + + return text ? [text] : []; + } + + case Datatype.JSON: + return [JSON.stringify(value)]; + + default: + // String, slug, URI and anything unknown: the text itself. + return typeof value === 'object' + ? [JSON.stringify(value)] + : [String(value)]; + } +} + +function dateFormatOf(column: QuickFilterColumn): string { + return column.dateFormat ?? urls.instances.dateFormats.localNumeric; +} + +function titles(subjects: string[], context: QuickFilterContext): string[] { + return subjects.map(subject => context.titleOf(subject) ?? subject); +} + +/** Anything that hands out a row's stored values, like a `Resource`. */ +export interface QuickFilterRow { + get(property: string): JSONValue; +} + +/** + * True when any of the columns shows `query` in this row. `query` must already + * be normalized; an empty one matches every row. + */ +export function rowMatchesQuickFilter( + row: QuickFilterRow, + columns: QuickFilterColumn[], + query: string, + context: QuickFilterContext, +): boolean { + if (query === '') { + return true; + } + + return columns.some(column => + cellTexts(row.get(column.property), column, context).some(text => + text.toLocaleLowerCase().includes(query), + ), + ); +} + +/** Joins a row's cells: a character nobody types, so no match spans two cells. */ +const CELL_SEPARATOR = '\u0000'; + +/** + * Everything a row shows in these columns, lower-cased, as one string to + * search. Built once per row and reused for every keystroke: formatting dates + * and numbers is the expensive part, a substring search is not. + */ +export function rowSearchText( + row: QuickFilterRow, + columns: QuickFilterColumn[], + context: QuickFilterContext, +): string { + return columns + .flatMap(column => cellTexts(row.get(column.property), column, context)) + .join(CELL_SEPARATOR) + .toLocaleLowerCase(); +} + +/** + * The subjects whose search text contains the query, in the order given — the + * view's own order, so sorting keeps working under the quick filter. A row with + * no text (not loaded yet) is left out. + */ +export function filterSubjectsBySearchText( + subjects: string[], + textOf: (subject: string) => string | undefined, + query: string, +): string[] { + const normalized = normalizeQuickFilter(query); + + if (normalized === '') { + return subjects; + } + + return subjects.filter(subject => textOf(subject)?.includes(normalized)); +} + +/** {@link filterSubjectsBySearchText}, straight from the rows. */ +export function filterSubjectsByQuickFilter( + subjects: string[], + rowOf: (subject: string) => QuickFilterRow | undefined, + columns: QuickFilterColumn[], + query: string, + context: QuickFilterContext, +): string[] { + return filterSubjectsBySearchText( + subjects, + subject => { + const row = rowOf(subject); + + return row && rowSearchText(row, columns, context); + }, + query, + ); +} + +/** The references a row's columns show, whose titles have to be loaded. */ +export function referencedSubjects( + rows: Iterable, + columns: QuickFilterColumn[], +): string[] { + const referenceColumns = columns.filter( + c => + c.datatype === Datatype.ATOMIC_URL || + c.datatype === Datatype.RESOURCEARRAY, + ); + const found = new Set(); + + if (referenceColumns.length === 0) { + return []; + } + + for (const row of rows) { + for (const column of referenceColumns) { + const value = row.get(column.property); + + if (typeof value === 'string') { + found.add(value); + } else if (Array.isArray(value)) { + for (const item of value) { + if (typeof item === 'string') { + found.add(item); + } + } + } + } + } + + return [...found]; +} diff --git a/browser/data-browser/src/chunks/TablePage/tableAggregates.test.ts b/browser/data-browser/src/chunks/TablePage/tableAggregates.test.ts index 3b18a16f28..689e7464bc 100644 --- a/browser/data-browser/src/chunks/TablePage/tableAggregates.test.ts +++ b/browser/data-browser/src/chunks/TablePage/tableAggregates.test.ts @@ -1,6 +1,7 @@ import { describe, it, expect } from 'vitest'; import { Datatype, type JSONValue, type Property } from '@tomic/react'; import { + aggregateRows, defaultGranularity, formatAggregateValue, formatGroupKey, @@ -231,3 +232,80 @@ describe('formatting', () => { expect(formatGroupKey('true', 'exact')).toBe('true'); }); }); + +describe('aggregateRows', () => { + const Q = 'https://example.com/property/quantity'; + const P = 'https://example.com/property/price'; + const TAG = 'https://example.com/property/tag'; + const DUE = 'https://example.com/property/due'; + + const rows = [ + { [Q]: 2, [P]: 5, [TAG]: ['a'], [DUE]: '2026-07-01' }, + { [Q]: 3, [TAG]: ['b', 'a'], [DUE]: '2026-07-20' }, + { [P]: '4', [TAG]: ['a'], [DUE]: '2026-08-02' }, + ].map(row => (prop: string) => (row as Record)[prop]); + + const byId = (outcomes: ReturnType) => + Object.fromEntries(outcomes.map(o => [o.id, o])); + + it('follows the store: counts rows with a value, skips rows without a number', () => { + const out = byId( + aggregateRows(rows, { + aggregates: [ + { id: 'rows', function: 'count' }, + { id: 'sumQ', property: Q, function: 'sum' }, + { id: 'avgQ', property: Q, function: 'avg' }, + { id: 'countQ', property: Q, function: 'count' }, + // A numeric string still sums, as it does in the store. + { id: 'sumP', property: P, function: 'sum' }, + { id: 'latest', property: DUE, function: 'max' }, + { + id: 'amount', + expression: { kind: 'product', a: Q, b: P }, + function: 'sum', + }, + ], + }), + ); + + expect(out.rows.value).toBe(3); + expect(out.sumQ).toMatchObject({ value: 5, count: 2 }); + expect(out.avgQ.value).toBe(2.5); + expect(out.countQ.value).toBe(2); + expect(out.sumP.value).toBe(9); + expect(out.latest.value).toBe(Date.UTC(2026, 7, 2)); + // Only the first row has both a quantity and a price. + expect(out.amount).toMatchObject({ value: 10, count: 1 }); + }); + + it('has no value, rather than zero, when nothing contributed', () => { + const [sum, count] = aggregateRows([], { + aggregates: [{ property: Q, function: 'sum' }, { function: 'count' }], + }); + + expect(sum.value).toBeNull(); + expect(count.value).toBe(0); + }); + + it('breaks down by the first tag, and by month', () => { + const [byTag] = aggregateRows(rows, { + aggregates: [{ property: Q, function: 'sum' }], + group_by: { property: TAG }, + }); + + expect(byTag.groups).toEqual([ + { key: 'b', value: 3, count: 1 }, + { key: 'a', value: 2, count: 1 }, + ]); + + const [byMonth] = aggregateRows(rows, { + aggregates: [{ function: 'count' }], + group_by: { property: DUE, granularity: 'month' }, + }); + + expect(byMonth.groups).toEqual([ + { key: '2026-07', value: 2, count: 2 }, + { key: '2026-08', value: 1, count: 1 }, + ]); + }); +}); diff --git a/browser/data-browser/src/chunks/TablePage/tableAggregates.ts b/browser/data-browser/src/chunks/TablePage/tableAggregates.ts index 764430d814..4df43340fd 100644 --- a/browser/data-browser/src/chunks/TablePage/tableAggregates.ts +++ b/browser/data-browser/src/chunks/TablePage/tableAggregates.ts @@ -2,6 +2,8 @@ import { Datatype, type AggregateFunction, type AggregateGrouping, + type AggregateGroup, + type AggregateOutcome, type Aggregate, type Aggregation, type JSONValue, @@ -14,6 +16,9 @@ import { import { DERIVED_COLUMN_GENERATORS, toExpression, + type ArgValues, + type DerivedColumnArg, + type DerivedColumnKind, type DerivedColumnSpec, } from './derivedColumns'; @@ -22,6 +27,7 @@ import { * (`view-aggregates`), computed by the store over **every** row the view * matches — filters included, paging excluded. So a "Sum of Amount" is the * answer for the whole table, not for the rows that happen to be loaded. + * A quick filter narrows it to the rows that match (`aggregateRows`). */ export interface TableAggregate { /** Stable identity within the view. */ @@ -358,3 +364,215 @@ export function formatGroupKey( return key; } + +/** Reads one property's value off a row. */ +export type RowValueReader = (property: string) => JSONValue | undefined; + +/** The store's default number of buckets per breakdown (`DEFAULT_GROUP_LIMIT`). */ +const DEFAULT_GROUP_LIMIT = 100; + +const ISO_DAY = /^(\d{4})-(\d{2})-(\d{2})/; + +/** A stored value as a number, the way the store reads one (`value_as_number`). */ +function valueAsNumber(value: JSONValue | undefined): number | undefined { + if (typeof value === 'number') { + return Number.isFinite(value) ? value : undefined; + } + + if (typeof value === 'boolean') { + return value ? 1 : 0; + } + + if (typeof value !== 'string') { + return undefined; + } + + const trimmed = value.trim(); + + // `Number('')` is 0; the store's parse fails on it, and so must this. + if (trimmed !== '' && !Number.isNaN(Number(trimmed))) { + return Number(trimmed); + } + + // A DATE is UTC midnight of its day. + const day = ISO_DAY.exec(trimmed); + + return day + ? Date.UTC(Number(day[1]), Number(day[2]) - 1, Number(day[3])) + : undefined; +} + +/** The bucket a row falls into, the way the store buckets it (`group_key`). */ +function groupKeyOf( + value: JSONValue | undefined, + grouping: AggregateGrouping, +): string { + if (value === undefined || value === null) { + return ''; + } + + // A select column's first tag, so the groups still add up to the total. + if (Array.isArray(value)) { + const first = value[0]; + + return typeof first === 'string' ? first : ''; + } + + const granularity = grouping.granularity ?? 'exact'; + + if (typeof value === 'number') { + if (granularity === 'exact') { + return String(value); + } + + const shifted = new Date( + value + (grouping.tz_offset_minutes ?? 0) * 60_000, + ).toISOString(); + + return granularity === 'month' ? shifted.slice(0, 7) : shifted.slice(0, 10); + } + + if (typeof value === 'string' && ISO_DAY.test(value)) { + return granularity === 'month' ? value.slice(0, 7) : value; + } + + return typeof value === 'object' ? JSON.stringify(value) : String(value); +} + +/** Running state of one statistic: the store's `Accumulator`. */ +class Accumulator { + sum = 0; + count = 0; + min: number | undefined; + max: number | undefined; + + add(number: number) { + this.sum += number; + this.count += 1; + this.min = this.min === undefined ? number : Math.min(this.min, number); + this.max = this.max === undefined ? number : Math.max(this.max, number); + } + + finish(fn: AggregateFunction): number | null { + if (fn === 'count') return this.count; + if (this.count === 0) return null; + if (fn === 'sum') return this.sum; + if (fn === 'avg') return this.sum / this.count; + + return (fn === 'min' ? this.min : this.max) ?? null; + } +} + +const compareKeys = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0); + +/** + * Computes an aggregation over rows the client already holds, by the rules the + * store applies to a query (`aggregate.rs`, `Db::aggregate`): `count` counts the + * rows that have a value (every row when it names nothing), the other functions + * skip rows without a number, and a computed column is evaluated per row. + * + * For the rows the quick filter leaves, which the store cannot be asked about: + * under a quick filter the totals describe the rows on screen. + */ +export function aggregateRows( + rows: Iterable, + aggregation: Aggregation, +): AggregateOutcome[] { + const { aggregates, group_by: grouping } = aggregation; + const now = aggregation.now_ms ?? Date.now(); + const totals = aggregates.map(() => new Accumulator()); + const perGroup = aggregates.map(() => new Map()); + + for (const read of rows) { + const group = grouping ? groupKeyOf(read(grouping.property), grouping) : ''; + + aggregates.forEach((aggregate, index) => { + let present = true; + let number: number | undefined; + + if (aggregate.expression) { + // An expression is a computed column's kind and arguments, flattened + // (`toExpression`), so the column's own generator evaluates it. + const { kind, ...args } = aggregate.expression as { + kind: DerivedColumnKind; + } & Record; + const values: ArgValues = {}; + + for (const arg of Object.values(args)) { + if (typeof arg === 'string' && arg !== '') { + values[arg] = read(arg); + } + } + + number = DERIVED_COLUMN_GENERATORS[kind]?.compute(values, args, now); + present = number !== undefined; + } else if (aggregate.property) { + const value = read(aggregate.property); + present = value !== undefined && value !== null; + number = valueAsNumber(value); + } + + const accumulate = (acc: Accumulator) => { + if (aggregate.function === 'count') { + if (present) acc.count += 1; + + return; + } + + if (number !== undefined) acc.add(number); + }; + + accumulate(totals[index]); + + if (grouping) { + let acc = perGroup[index].get(group); + + if (!acc) { + acc = new Accumulator(); + perGroup[index].set(group, acc); + } + + accumulate(acc); + } + }); + } + + return aggregates.map((aggregate, index): AggregateOutcome => { + const outcome: AggregateOutcome = { + id: aggregate.id, + property: aggregate.property, + function: aggregate.function, + value: totals[index].finish(aggregate.function), + count: totals[index].count, + }; + + if (!grouping) { + return outcome; + } + + const groups: AggregateGroup[] = [...perGroup[index]].map(([key, acc]) => ({ + key, + value: acc.finish(aggregate.function), + count: acc.count, + })); + + // Chronological for day and month buckets, biggest-first otherwise. + if ((grouping.granularity ?? 'exact') === 'exact') { + groups.sort( + (a, b) => + (b.value ?? -Infinity) - (a.value ?? -Infinity) || + compareKeys(a.key, b.key), + ); + } else { + groups.sort((a, b) => compareKeys(a.key, b.key)); + } + + const limit = grouping.limit ?? DEFAULT_GROUP_LIMIT; + + return { + ...outcome, + groups: groups.slice(0, limit), + groups_truncated: groups.length > limit, + }; + }); +} diff --git a/browser/data-browser/src/chunks/TablePage/useQuickFilter.ts b/browser/data-browser/src/chunks/TablePage/useQuickFilter.ts new file mode 100644 index 0000000000..70651e6505 --- /dev/null +++ b/browser/data-browser/src/chunks/TablePage/useQuickFilter.ts @@ -0,0 +1,273 @@ +import { + core, + server, + urls, + dataBrowser, + useResources, + type AggregateOutcome, + type Aggregation, + type Collection, + type Datatype, + type JSONValue, + type Property, +} from '@tomic/react'; +import { useEffect, useMemo, useState } from 'react'; +import { useSettings } from '../../helpers/AppSettings'; +import { usePropertyTitles } from './helpers/usePropertyTitles'; +import { aggregateRows } from './tableAggregates'; +import { + filterSubjectsBySearchText, + normalizeQuickFilter, + referencedSubjects, + rowSearchText, + type QuickFilterColumn, + type QuickFilterContext, +} from './quickFilter'; + +/** A column the quick filter searches: a property, split by language or not. */ +export interface QuickFilterSource { + property: Property; + languageTag?: string; +} + +export interface QuickFilterResult { + /** True while there is text to filter by. */ + active: boolean; + /** + * What the view should render: the view's own collection when inactive, the + * matching rows (in the collection's order) when active. + */ + collection: Collection; + /** The matching subjects, in order. Empty while inactive. */ + matches: string[]; + /** True while the rows to search through are still being fetched. */ + loading: boolean; +} + +const EMPTY: string[] = []; + +/** + * Narrows a view's rows to the ones where any of `columns` shows `query` — see + * `quickFilter.ts` for what "shows" means per datatype. + * + * The collection is the view's own query, column filters and sort included, so + * the quick filter combines with both for free: it only ever removes rows from + * what the view would otherwise show, and keeps their order. It walks every page + * of that collection (the grid itself fetches 30 rows at a time), so it finds a + * row you have not scrolled to yet; nothing is fetched while the field is empty. + */ +export function useQuickFilter( + collection: Collection, + columns: QuickFilterSource[], + query: string, +): QuickFilterResult { + const normalized = normalizeQuickFilter(query); + const active = normalized !== ''; + const { contentLanguage } = useSettings(); + + // Every member, fetched only while filtering. Tagged with the collection it + // came from, so a stale list is never searched as if it answered a new query. + const [members, setMembers] = useState<{ + from: Collection; + subjects: string[]; + }>(); + const totalMembers = collection.totalMembers; + + useEffect(() => { + if (!active) { + return; + } + + let cancelled = false; + + collection + .getAllMembers() + .then(subjects => { + if (!cancelled) { + setMembers({ from: collection, subjects }); + } + }) + .catch(() => undefined); + + return () => { + cancelled = true; + }; + }, [active, collection, totalMembers]); + + const memberSubjects = + active && members?.from === collection ? members.subjects : EMPTY; + const rows = useResources(memberSubjects); + + // Formatting constraints live on the Property resources. + const properties = useMemo(() => { + const seen = new Map(); + + for (const { property } of columns) { + seen.set(property.subject, property); + } + + return [...seen.values()]; + }, [columns]); + const propertySubjects = useMemo( + () => (active ? properties.map(p => p.subject) : EMPTY), + [active, properties], + ); + const propertyResources = useResources(propertySubjects); + const labels = usePropertyTitles(properties); + + const quickColumns = useMemo( + (): QuickFilterColumn[] => + columns.map(({ property, languageTag }) => { + const resource = propertyResources.get(property.subject); + const read = (prop: string) => resource?.get(prop) as T | undefined; + + return { + property: property.subject, + datatype: property.datatype as Datatype, + label: labels.get(property.subject) ?? property.shortname, + languageTag, + dateFormat: read(urls.properties.constraints.dateFormat), + numberFormatting: read( + urls.properties.constraints.numberFormatting, + ), + decimalPlaces: read( + urls.properties.constraints.decimalPlaces, + ), + currency: read(dataBrowser.properties.currency), + }; + }), + [columns, propertyResources, labels], + ); + + // A reference is searched by its title, so the resources it points at have + // to be loaded too. Keyed on content: `rows` changes on every edit, and a new + // array would resubscribe every reference. + const referenceKey = useMemo( + () => referencedSubjects(rows.values(), quickColumns).join('\n'), + [rows, quickColumns], + ); + const references = useMemo( + () => (referenceKey === '' ? EMPTY : referenceKey.split('\n')), + [referenceKey], + ); + const referenceResources = useResources(references); + + const context = useMemo( + (): QuickFilterContext => ({ + contentLanguage, + titleOf: subject => { + const resource = referenceResources.get(subject); + + if (!resource || resource.loading || resource.error) { + return undefined; + } + + return (resource.get(core.properties.name) ?? + resource.get(core.properties.shortname) ?? + resource.get(server.properties.filename)) as string | undefined; + }, + }), + [contentLanguage, referenceResources], + ); + + // What each row shows, rebuilt when a row, a column or a title changes — not + // on every keystroke. + const searchTexts = useMemo(() => { + const texts = new Map(); + + for (const [subject, row] of rows) { + if (!row.loading && !row.error) { + texts.set(subject, rowSearchText(row, quickColumns, context)); + } + } + + return texts; + }, [rows, quickColumns, context]); + + const matchKey = useMemo( + () => + active + ? filterSubjectsBySearchText( + memberSubjects, + subject => searchTexts.get(subject), + normalized, + ).join('\n') + : '', + [active, memberSubjects, searchTexts, normalized], + ); + + // Stable while the same rows match, so an edit that doesn't change the result + // doesn't make the views re-resolve their rows. + const matches = useMemo( + () => (matchKey === '' ? EMPTY : matchKey.split('\n')), + [matchKey], + ); + + const filtered = useMemo(() => subjectListCollection(matches), [matches]); + + return { + active, + collection: active ? filtered : collection, + matches: active ? matches : EMPTY, + loading: active && members?.from !== collection, + }; +} + +/** + * The totals to show under a quick-filtered view: over the rows that match, + * computed here since the store only knows the view's own query. While the + * field is empty, the store's `outcomes` (over every row the view matches) + * pass through unchanged — so the footer's row count and its totals always + * describe the same rows. + */ +export function useQuickFilterAggregates( + quickFilter: Pick, + aggregation: Aggregation | undefined, + outcomes: AggregateOutcome[], +): AggregateOutcome[] { + const rows = useResources( + quickFilter.active && aggregation ? quickFilter.matches : EMPTY, + ); + + return useMemo(() => { + if (!quickFilter.active || !aggregation) { + return outcomes; + } + + const readers = quickFilter.matches.flatMap(subject => { + const row = rows.get(subject); + + return row && !row.error + ? [(property: string) => row.get(property) as JSONValue | undefined] + : []; + }); + + return aggregateRows(readers, aggregation); + }, [quickFilter.active, quickFilter.matches, aggregation, outcomes, rows]); +} + +/** + * A fixed list of subjects, in the shape the views read a `Collection` in: by + * index (the grid, its copy/paste/delete and presence helpers), and whole (the + * board, the issue list, the calendar). Those are the only parts of a + * `Collection` a row view reads, which is what lets the quick filter narrow + * every view kind without each of them knowing about it. + */ +export function subjectListCollection(subjects: string[]): Collection { + const list = { + totalMembers: subjects.length, + getMemberWithIndex: async (index: number) => { + if (index < 0 || index >= subjects.length) { + throw new Error('Index out of bounds'); + } + + return subjects[index]; + }, + getAllMembers: async () => [...subjects], + async *[Symbol.asyncIterator]() { + yield* subjects; + }, + }; + + return list as unknown as Collection; +} diff --git a/browser/data-browser/src/locales/de.po b/browser/data-browser/src/locales/de.po index 800b9b11fa..4e08883164 100644 --- a/browser/data-browser/src/locales/de.po +++ b/browser/data-browser/src/locales/de.po @@ -1867,6 +1867,7 @@ msgid "Copied error to clipboard" msgstr "" #: src/chunks/TablePage/EditorCells/AtomicURLCell.tsx +#: src/chunks/TablePage/QuickFilterField.tsx #: src/components/Toaster.tsx #: src/components/forms/InputResourceArray.tsx msgid "Clear" @@ -10695,3 +10696,20 @@ msgstr "" #: src/chunks/AI/RealAIChat.tsx msgid "Included AI isn't reachable right now. Check your connection and sign-in, then reload to try again." msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows…" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows containing text" +msgstr "" + +#. 0: quickFilterQuery +#: src/chunks/TablePage/TableResource.tsx +msgid "No rows show “{0}”." +msgstr "" diff --git a/browser/data-browser/src/locales/en.po b/browser/data-browser/src/locales/en.po index 723c35bceb..23f0ed7f42 100644 --- a/browser/data-browser/src/locales/en.po +++ b/browser/data-browser/src/locales/en.po @@ -1867,6 +1867,7 @@ msgid "Copied error to clipboard" msgstr "Copied error to clipboard" #: src/chunks/TablePage/EditorCells/AtomicURLCell.tsx +#: src/chunks/TablePage/QuickFilterField.tsx #: src/components/Toaster.tsx #: src/components/forms/InputResourceArray.tsx msgid "Clear" @@ -10713,3 +10714,20 @@ msgstr "Discard upload" #: src/chunks/AI/RealAIChat.tsx msgid "Included AI isn't reachable right now. Check your connection and sign-in, then reload to try again." msgstr "Included AI isn't reachable right now. Check your connection and sign-in, then reload to try again." + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows" +msgstr "Find rows" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows…" +msgstr "Find rows…" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows containing text" +msgstr "Find rows containing text" + +#. 0: quickFilterQuery +#: src/chunks/TablePage/TableResource.tsx +msgid "No rows show “{0}”." +msgstr "No rows show “{0}”." diff --git a/browser/data-browser/src/locales/es.po b/browser/data-browser/src/locales/es.po index 6222ccfcb8..dd8c372d00 100644 --- a/browser/data-browser/src/locales/es.po +++ b/browser/data-browser/src/locales/es.po @@ -1867,6 +1867,7 @@ msgid "Copied error to clipboard" msgstr "Error copiado al portapapeles" #: src/chunks/TablePage/EditorCells/AtomicURLCell.tsx +#: src/chunks/TablePage/QuickFilterField.tsx #: src/components/Toaster.tsx #: src/components/forms/InputResourceArray.tsx msgid "Clear" @@ -10695,3 +10696,20 @@ msgstr "" #: src/chunks/AI/RealAIChat.tsx msgid "Included AI isn't reachable right now. Check your connection and sign-in, then reload to try again." msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows…" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows containing text" +msgstr "" + +#. 0: quickFilterQuery +#: src/chunks/TablePage/TableResource.tsx +msgid "No rows show “{0}”." +msgstr "" diff --git a/browser/data-browser/src/locales/fr.po b/browser/data-browser/src/locales/fr.po index 795654f91f..a310b1896d 100644 --- a/browser/data-browser/src/locales/fr.po +++ b/browser/data-browser/src/locales/fr.po @@ -1867,6 +1867,7 @@ msgid "Copied error to clipboard" msgstr "Erreur copiée dans le presse-papier" #: src/chunks/TablePage/EditorCells/AtomicURLCell.tsx +#: src/chunks/TablePage/QuickFilterField.tsx #: src/components/Toaster.tsx #: src/components/forms/InputResourceArray.tsx msgid "Clear" @@ -10695,3 +10696,20 @@ msgstr "" #: src/chunks/AI/RealAIChat.tsx msgid "Included AI isn't reachable right now. Check your connection and sign-in, then reload to try again." msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows…" +msgstr "" + +#: src/chunks/TablePage/QuickFilterField.tsx +msgid "Find rows containing text" +msgstr "" + +#. 0: quickFilterQuery +#: src/chunks/TablePage/TableResource.tsx +msgid "No rows show “{0}”." +msgstr ""