Files
invoices/web/useInvoicesSection.ts
T
officer 6c70eb8eb3 invoices, extracted from the platform as a plugin
Ninth plugin out, third resurrection: switched off since 2026-08-13 with the
permission reduced to api: [], so the server booted clean and every panel
request 404'd. invoiceshelf/schema was also commented out of the drizzle
barrel.

Named `invoices`, not `invoiceshelf`. The appName drives the API prefix, the
frontend route, the table prefix and the install key all at once, and before
extraction those had drifted: the API was /api/invoiceshelf while the screen
was /invoices. The URL is what a person sees and should name what the plugin
shows rather than the daemon behind it — the permission key and label were
already `invoices`, so the API prefix was the odd one out.

Cost: three call sites, and renaming invoiceshelf_accounts to invoices_accounts
to satisfy the table-prefix rule. That rename was free ONLY because the table
existed on no server, which was checked before deciding. With rows in it,
keeping the vendor name would have been the better trade: db:push sees a rename
as a drop plus a create and this project has no migration mechanism.

The sidecar keeps its Invoiceshelf-named functions deliberately — they name the
upstream API it actually speaks.

91 palette classes converted, 52 of them in STATUS_TONE, which was already the
single source of status colour but held raw palette strings. SENT and VIEWED
now share `info`: the closed six-tone set has no second shade of "in flight,
not yet actioned". Recorded in PLUGIN.md as a real loss.
2026-08-19 03:47:44 +00:00

95 lines
3.3 KiB
TypeScript

import { useCallback, useMemo } from 'react';
import { useParams, useSearchParams } from 'react-router';
import { DEFAULT_INVOICES_SECTION, isInvoicesSection, type InvoicesSectionId } from './shared';
// The URL names the open section and the selected record — not a panel channel. See docs/navigation-audit.md.
// InvoicesScreen redirects anything unrecognised, so the fallback here only covers the instant before that
// lands.
export function useInvoicesSection(): InvoicesSectionId {
const { section } = useParams();
return isInvoicesSection(section) ? section : DEFAULT_INVOICES_SECTION;
}
export type ListFilters = {
/** The record whose detail pane is open. `?selected=` because the list stays on screen beside it. */
selected: number | null;
search: string;
status: string | null;
customerId: number | null;
fromDate: string | null;
toDate: string | null;
page: number;
isFiltered: boolean;
};
/**
* Read/write the list query string.
*
* Every setter drops `page` back to 1 — changing a filter while on page 4 of the old result set otherwise
* lands you on a page that may not exist, and upstream answers that with an empty array rather than an
* error, which reads as "no results" for a filter that has plenty.
*
* Selection is `replace: true` so clicking through a list does not bury the previous screen under a dozen
* history entries; filter changes push normally, because backing out of a filter is a thing people want.
*/
export function useListFilters() {
const [searchParams, setSearchParams] = useSearchParams();
const filters = useMemo<ListFilters>(() => {
const num = (key: string) => {
const raw = searchParams.get(key);
const n = raw == null ? NaN : Number(raw);
return Number.isInteger(n) && n > 0 ? n : null;
};
const status = searchParams.get('status');
const search = searchParams.get('q') ?? '';
const customerId = num('customer');
const fromDate = searchParams.get('from');
const toDate = searchParams.get('to');
return {
selected: num('selected'),
search,
status,
customerId,
fromDate,
toDate,
page: num('page') ?? 1,
isFiltered: Boolean(search || status || customerId || fromDate || toDate),
};
}, [searchParams]);
const patch = useCallback(
(changes: Record<string, string | number | null>, opts?: { replace?: boolean; keepPage?: boolean }) => {
setSearchParams(
(prev) => {
const next = new URLSearchParams(prev);
for (const [key, value] of Object.entries(changes)) {
if (value === null || value === '') next.delete(key);
else next.set(key, String(value));
}
if (!opts?.keepPage && !('page' in changes)) next.delete('page');
return next;
},
{ replace: opts?.replace ?? false },
);
},
[setSearchParams],
);
const select = useCallback(
(id: number | null) => patch({ selected: id }, { replace: true, keepPage: true }),
[patch],
);
const setPage = useCallback((page: number) => patch({ page: page <= 1 ? null : page }, { keepPage: true }), [patch]);
const clearFilters = useCallback(
() => patch({ q: null, status: null, customer: null, from: null, to: null }),
[patch],
);
return { filters, patch, select, setPage, clearFilters };
}