diff --git a/src/lib/statusTheme.test.ts b/src/lib/statusTheme.test.ts index 4c49832..d496cda 100644 --- a/src/lib/statusTheme.test.ts +++ b/src/lib/statusTheme.test.ts @@ -14,4 +14,12 @@ describe('status theme', () => { it('groups terminal statuses as settled', () => { expect(ALL.filter(isSettled)).toEqual(['FULFILLED', 'CANCELLED', 'REFUNDED']); }); + + it('falls back to a neutral theme for statuses this build does not know', () => { + expect(getStatusTheme('PARTIALLY_REFUNDED')).toEqual({ + label: 'Partially refunded', + color: '#5b605c', + background: '#ecebe6', + }); + }); }); diff --git a/src/lib/statusTheme.ts b/src/lib/statusTheme.ts index e41c281..cac7cd2 100644 --- a/src/lib/statusTheme.ts +++ b/src/lib/statusTheme.ts @@ -14,8 +14,21 @@ const STATUS_THEME: Record = { REFUNDED: { label: 'Refunded', color: '#b4412f', background: '#f8e0db' }, }; -export function getStatusTheme(status: OrderStatus): StatusTheme { - return STATUS_THEME[status]; +const FALLBACK_THEME: Omit = { color: '#5b605c', background: '#ecebe6' }; + +/** Turns an enum value such as PARTIALLY_REFUNDED into "Partially refunded". */ +function humanize(status: string): string { + const words = status.toLowerCase().split('_'); + return [words[0].charAt(0).toUpperCase() + words[0].slice(1), ...words.slice(1)].join(' '); +} + +/** + * Returns the display theme for an order status. Released app builds can + * receive statuses added to the API after they shipped, so unknown values get + * a neutral theme instead of crashing. + */ +export function getStatusTheme(status: OrderStatus | (string & {})): StatusTheme { + return STATUS_THEME[status as OrderStatus] ?? { ...FALLBACK_THEME, label: humanize(status) }; } /** Orders in these states no longer change and are grouped under "Past orders". */