perf(hem): stream the home page in three sections and trim its query plan (#1945)

Hem was one ~33-query render behind a single fallback: the greeting waited
for the slowest worklist scan, and the request also paid a sequential
bank_file_imports read after the batch, a second scan of the suggested
matches (getWorklistCounts counted the same 200 rows the pane listed) and
an awaited stale-dismissal delete on the read path.

- page.tsx awaits only what the greeting shell and the redirects need
  (settings, profile, agent profile, the Skatteverket flag); the notice
  line, the setup checklist and the Att göra + Fortsätt panes are async
  server components behind their own Suspense (hem-sections.tsx). RSC
  streaming applies to client navigations too, so the greeting paints
  first on every visit and each block fills in as its queries land.
- DashboardContent becomes the shell with three slots; HemNotices keeps
  the one client-side action (the wrong-account sign-out); HemSkeletons
  are the two fallbacks.
- getWorklistCounts accepts the suggested matches the caller is already
  fetching (a promise, so it stays parallel); listSuggestedMatches runs
  once at the scan cap and the pane shows the first five.
- countInboxDocuments runs its id chunks in one wave instead of N
  sequential round trips.
- getCompanyNotices takes deferReap; Hem passes Next's after() so the
  stale-dismissal delete runs after the response.
- bank_file_imports joins the checklist section's batch.

Tests: worklist aggregate (precomputed matches skip the rescan), notices
aggregate (deferReap receives the reap; the delete does not run inline
and runs when the task does).

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-26 14:02:09 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5
parent a3326a0296
commit a8980a3a41
10 changed files with 429 additions and 248 deletions
+19
View File
@@ -141,6 +141,25 @@ describe('stale-dismissal reaping (contract in lib/notices/types.ts)', () => {
expect(queued.findCall('notice_dismissals', 'in')).toEqual(['notice_id', [BACKUP_ID]])
})
it('hands the reap to deferReap instead of awaiting it on the read path (Hem streams)', async () => {
queued.enqueue({ data: [{ notice_id: BACKUP_ID }] }) // dismissal read
queued.enqueue({ data: null }) // delete result, consumed only when the task runs
const deferred: Array<() => Promise<void>> = []
const notices = await getCompanyNotices(qSupabase, 'company-1', {
userId: 'user-1',
deferReap: (task) => deferred.push(task),
})
expect(notices).toEqual([])
expect(deferred).toHaveLength(1)
expect(queued.findCalls('notice_dismissals', 'delete')).toEqual([])
await deferred[0]()
expect(queued.findCalls('notice_dismissals', 'delete').length).toBe(1)
expect(queued.findCall('notice_dismissals', 'in')).toEqual(['notice_id', [BACKUP_ID]])
})
it('resurfaces a NEW failure after the healthy spell reaped the dismissal', async () => {
// error -> dismiss: hidden while the incident persists.
detectMocks.backup.mockResolvedValue(backupNotice)
+9 -2
View File
@@ -13,6 +13,12 @@ import {
const log = createLogger('notices')
export interface GetCompanyNoticesOptions {
/**
* Run the stale-dismissal reap after the response instead of on the read
* path (Hem passes Next's `after`). The reap is hygiene: awaiting it made
* every Hem render wait for a delete nobody sees.
*/
deferReap?: (task: () => Promise<void>) => void
/** Caller's user id: Skatteverket connections and dismissals are per user. */
userId: string
now?: Date
@@ -37,7 +43,7 @@ export interface GetCompanyNoticesOptions {
export async function getCompanyNotices(
supabase: SupabaseClient,
companyId: string,
{ userId, now = new Date() }: GetCompanyNoticesOptions,
{ userId, now = new Date(), deferReap }: GetCompanyNoticesOptions,
): Promise<Notice[]> {
const [candidates, dismissedIds] = await Promise.all([
Promise.all([
@@ -62,7 +68,8 @@ export async function getCompanyNotices(
const category = NOTICE_PRIORITY.find((c) => id.startsWith(`${c}:`))
return category !== undefined && !byCategory.has(category)
})
await reapStaleDismissals(supabase, companyId, userId, staleIds)
if (deferReap) deferReap(() => reapStaleDismissals(supabase, companyId, userId, staleIds))
else await reapStaleDismissals(supabase, companyId, userId, staleIds)
return NOTICE_PRIORITY.map((category) => byCategory.get(category)).filter(
(n): n is Notice => n !== undefined && !dismissedIds.has(n.id),
+10
View File
@@ -37,6 +37,16 @@ describe('getWorklistCounts', () => {
})
})
it('takes the suggested-match count from a caller-supplied list instead of rescanning', async () => {
const { countSuggestedMatches } = await import('../categories')
const matches = [{ transactionId: 't1' }, { transactionId: 't2' }, { transactionId: 't3' }] as never[]
const { counts } = await getWorklistCounts(supabase, 'company-1', {
suggestedMatches: Promise.resolve(matches),
})
expect(counts.suggested_match).toBe(3)
expect(countSuggestedMatches).not.toHaveBeenCalled()
})
it('excludes suggested_match from the total (subset of book_transaction)', async () => {
const { total } = await getWorklistCounts(supabase, 'company-1')
// 4 + 6 + 1 + 3 + 5 + 1 + 2 + 1, without the 2 suggested matches.
+15 -1
View File
@@ -1,4 +1,5 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import type { SuggestedMatch } from './types'
import type { WorklistCounts } from './types'
import {
countDeadlinesNeedingAction,
@@ -22,9 +23,20 @@ import {
* fast path over transactions already counted in book_transaction, so it is
* excluded to avoid double-counting (see lib/worklist/types.ts).
*/
export interface GetWorklistCountsOptions {
/**
* Suggested matches the caller is already fetching (Hem renders them in
* the Att göra pane): the count is taken from this list instead of a
* second scan of the same rows. A promise is accepted so it can run in
* parallel with the other counts.
*/
suggestedMatches?: SuggestedMatch[] | Promise<SuggestedMatch[]>
}
export async function getWorklistCounts(
supabase: SupabaseClient,
companyId: string,
options: GetWorklistCountsOptions = {},
): Promise<WorklistCounts> {
const [
bookTransaction,
@@ -39,7 +51,9 @@ export async function getWorklistCounts(
] = await Promise.all([
countUnbookedTransactions(supabase, companyId),
countInboxDocuments(supabase, companyId),
countSuggestedMatches(supabase, companyId),
options.suggestedMatches
? Promise.resolve(options.suggestedMatches).then((m) => m.length)
: countSuggestedMatches(supabase, companyId),
countSupplierInvoicesAwaitingApproval(supabase, companyId),
countVerifikatMissingDocument(supabase, companyId),
countOverdueInvoices(supabase, companyId),
+16 -9
View File
@@ -100,15 +100,22 @@ export async function countInboxDocuments(
// PostgREST serialises .in() into the GET query string: chunk the id list
// so a large inbox can't push the URL past proxy limits (HTTP 414, which
// would silently zero the badge via the error branch).
// The chunks are independent: one wave instead of N sequential round trips.
const chunks: string[][] = []
for (let i = 0; i < docIds.length; i += IN_CLAUSE_CHUNK) chunks.push(docIds.slice(i, i + IN_CLAUSE_CHUNK))
const results = await Promise.all(
chunks.map((ids) =>
supabase
.from('document_attachments')
.select('id', { count: 'exact', head: true })
.eq('company_id', companyId)
.in('id', ids)
.is('journal_entry_id', null)
.eq('is_current_version', true),
),
)
let total = 0
for (let i = 0; i < docIds.length; i += IN_CLAUSE_CHUNK) {
const { count, error: docError } = await supabase
.from('document_attachments')
.select('id', { count: 'exact', head: true })
.eq('company_id', companyId)
.in('id', docIds.slice(i, i + IN_CLAUSE_CHUNK))
.is('journal_entry_id', null)
.eq('is_current_version', true)
for (const { count, error: docError } of results) {
if (docError) return logAndZero('inbox_document', companyId, docError)
total += count ?? 0
}
@@ -125,7 +132,7 @@ const SUGGESTED_MATCH_OR =
* feeds are chunked (fetchCandidatesChunked), so the URL length stays bounded
* regardless of this number.
*/
const SUGGESTED_MATCH_SCAN_CAP = 200
export const SUGGESTED_MATCH_SCAN_CAP = 200
/**
* Unbooked transactions with a still-actionable invoice/supplier-invoice match