list-columns.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Which optional columns a list shows.
  3. *
  4. * A work order table already carries eight columns on a laptop, so anything
  5. * not everyone needs is off until someone asks for it. The choice is per
  6. * browser and per list, stored the way the remembered sort is: a cookie, so
  7. * the server renders the right columns on the first paint instead of the page
  8. * flickering from the default set to the chosen one.
  9. */
  10. import type { ListKey } from './list-sort-preference'
  11. export const LIST_COLUMNS_COOKIE = 'listColumns'
  12. /** A year, like the sort cookie. */
  13. export const LIST_COLUMNS_COOKIE_MAX_AGE = 60 * 60 * 24 * 365
  14. /** Optional columns of the work order list, and whether they start on. */
  15. export const WORK_ORDER_COLUMNS = {
  16. invoice: true,
  17. customer: true,
  18. tech: true,
  19. /**
  20. * When the customer was told the vehicle would be ready. Off by default:
  21. * most shops promise nothing, and a full timestamp in every row crowds out
  22. * the columns that are read on every job.
  23. */
  24. promised: false,
  25. /**
  26. * When the job was last changed, for finding the one just worked on. Off by
  27. * default for the same reason: one more timestamp in every row.
  28. */
  29. updated: false,
  30. } as const
  31. export type WorkOrderColumn = keyof typeof WORK_ORDER_COLUMNS
  32. /** The columns of a list that are on when nobody has chosen. */
  33. export function defaultColumns(available: Record<string, boolean>): string[] {
  34. return Object.entries(available)
  35. .filter(([, on]) => on)
  36. .map(([column]) => column)
  37. }
  38. /**
  39. * Parses the cookie: `{"workOrders":["promised","tech"]}`. Anything malformed
  40. * reads as no choice at all rather than throwing, because a hand-edited or
  41. * truncated cookie must never break a page render.
  42. */
  43. export function parseListColumnsCookie(
  44. value: string | undefined
  45. ): Partial<Record<ListKey, string[]>> {
  46. if (!value) return {}
  47. try {
  48. const parsed = JSON.parse(decodeURIComponent(value))
  49. if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
  50. const out: Partial<Record<ListKey, string[]>> = {}
  51. for (const [list, columns] of Object.entries(parsed as Record<string, unknown>)) {
  52. if (Array.isArray(columns) && columns.every((c) => typeof c === 'string')) {
  53. out[list as ListKey] = columns as string[]
  54. }
  55. }
  56. return out
  57. } catch {
  58. return {}
  59. }
  60. }
  61. /**
  62. * The columns to render: the remembered choice, narrowed to the columns this
  63. * list actually has (a stored name that no longer exists is dropped), or the
  64. * defaults when there is no choice to read.
  65. */
  66. export function columnsFrom(
  67. stored: string[] | undefined,
  68. available: Record<string, boolean>
  69. ): string[] {
  70. if (!stored) return defaultColumns(available)
  71. return stored.filter((column) => column in available)
  72. }