feat(invoice-inbox): show receipt fields on receipts, invoice fields on invoices (#1426)
* feat(invoice-inbox): show receipt fields on receipts, invoice fields on invoices A kassakvitto has no due date, no OCR reference, no invoice number and no bankgiro: it is already paid. The rail rendered all four as empty boxes anyway, so a perfectly extracted receipt looked like a failed extraction. The date was mislabelled too: on a receipt the document date is the purchase date, not a "Fakturadatum". When extracted_data.documentKind is 'receipt' the rail now labels the date "Inköpsdatum" and folds the five invoice-only fields behind a quiet "Visa fakturafält (N)" link. Two rules keep it safe when the classification is wrong: a field holding a value is never hidden (a hybrid restaurangnota with an invoice number still shows it), and the fold is one click from being undone. Logic lives in lib/documents/inbox-field-visibility.ts rather than the component: this repo has no jsdom or testing-library, so display rules are only testable as a React-free module. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(invoice-inbox): reset the invoice-field fold when switching documents Review catch (PR Agent on #1426): showAllFields was component state with no per-item reset, and EditableFieldsList stays mounted across selections in the rail. Expanding "Visa fakturafält" on one receipt therefore left the invoice-only fields open on the next document, which reads as if that one carried them too. Reset alongside drafts and edit provenance in the existing itemId effect, and moved the declaration up next to the other state so it is defined above its first use rather than relying on hoisting. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- 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:
co-authored by
Claude Fable 5
Jakob Wennberg
parent
576ed4d290
commit
d7320a6c87
@@ -47,6 +47,7 @@ import { CAPABILITY } from '@/lib/entitlements/keys'
|
||||
import type { WorkspaceComponentProps } from '@/lib/extensions/workspace-registry'
|
||||
import type { InboxChannelContext, InvoiceExtractionResult } from '@/types'
|
||||
import { renderChannelParticipant } from '@/lib/documents/channel-context-notes'
|
||||
import { selectInboxFields } from '@/lib/documents/inbox-field-visibility'
|
||||
import BookDirectlyDialog from '@/components/extensions/general/BookDirectlyDialog'
|
||||
import NewSupplierInvoiceDialog from '@/components/supplier-invoices/NewSupplierInvoiceDialog'
|
||||
import BulkBookInboxDialog from '@/components/extensions/general/BulkBookInboxDialog'
|
||||
@@ -2334,6 +2335,8 @@ export function EditableFieldsList({
|
||||
// from the extraction) and flips to user-verified once the user edits it:
|
||||
// mirrors the create form's AiFilledIndicator. Reset when switching items.
|
||||
const [edited, setEdited] = useState<Partial<Record<FieldKey, boolean>>>({})
|
||||
// Per-document fold for the invoice-only fields on a receipt.
|
||||
const [showAllFields, setShowAllFields] = useState(false)
|
||||
const timersRef = useRef<Partial<Record<FieldKey, ReturnType<typeof setTimeout>>>>({})
|
||||
// Last-known server values per field. Used to detect when the server
|
||||
// normalises a value (currency upper-cased, whitespace trimmed) so we can
|
||||
@@ -2351,6 +2354,10 @@ export function EditableFieldsList({
|
||||
setDrafts(seeded)
|
||||
lastServerRef.current = seeded
|
||||
setEdited({})
|
||||
// The "Visa fakturafält" fold is per document: without this, expanding
|
||||
// it on one receipt leaves the invoice fields open on the next one,
|
||||
// which reads as if that document had them too.
|
||||
setShowAllFields(false)
|
||||
return () => {
|
||||
for (const t of Object.values(timersRef.current)) {
|
||||
if (t) clearTimeout(t)
|
||||
@@ -2465,9 +2472,20 @@ export function EditableFieldsList({
|
||||
|
||||
const vatRows = useMemo(() => data.vatBreakdown ?? [], [data.vatBreakdown])
|
||||
|
||||
const { shown: shownFields, hiddenCount } = useMemo(
|
||||
() =>
|
||||
selectInboxFields({
|
||||
documentKind: data.documentKind ?? null,
|
||||
fields: FIELD_DEFS,
|
||||
hasValue: (key) => (drafts[key as FieldKey] ?? '').trim() !== '',
|
||||
showAll: showAllFields,
|
||||
}),
|
||||
[data, drafts, showAllFields]
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{FIELD_DEFS.map((f) => (
|
||||
{shownFields.map((f) => (
|
||||
<div key={f.key} className="flex flex-col gap-0.5">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<label
|
||||
@@ -2497,6 +2515,15 @@ export function EditableFieldsList({
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
{hiddenCount > 0 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAllFields(true)}
|
||||
className="text-[11px] text-muted-foreground hover:text-foreground hover:underline pt-1"
|
||||
>
|
||||
Visa fakturafält ({hiddenCount})
|
||||
</button>
|
||||
)}
|
||||
{vatRows.length > 0 && (
|
||||
<div className="pt-2 border-t mt-3">
|
||||
<p className="text-[10px] uppercase tracking-wide text-muted-foreground/80 mb-1.5">
|
||||
|
||||
Reference in New Issue
Block a user