table-utils.ts 1.2 KB

1234567891011121314151617181920212223242526
  1. export const typeColors: Record<string, string> = {
  2. maintenance: 'bg-blue-500/10 text-blue-500 border-blue-500/20',
  3. repair: 'bg-red-500/10 text-red-500 border-red-500/20',
  4. upgrade: 'bg-emerald-500/10 text-emerald-500 border-emerald-500/20',
  5. inspection: 'bg-amber-500/10 text-amber-500 border-amber-500/20',
  6. }
  7. /**
  8. * Width for a column in a fixed-layout table, sized from what it holds.
  9. *
  10. * Lists use `table-fixed` so their text columns can truncate, which means a
  11. * short column cannot size itself, and a fixed pixel width breaks as soon as
  12. * a translation is longer than the English (the header spills into the next
  13. * column). This takes the longer of the header label and the widest cell on
  14. * the page. `cellScale` is how wide a cell character is next to a header
  15. * character: about 1 for text-sm monospace, 0.85 for text-xs monospace. The
  16. * rem covers the cell padding and the sort icon.
  17. */
  18. export function fitColumnWidth(
  19. label: string,
  20. cells: readonly (string | number | null | undefined)[] = [],
  21. cellScale = 1
  22. ): string {
  23. const longestCell = Math.max(0, ...cells.map((c) => String(c ?? '-').length))
  24. return `calc(${Math.max(label.length, longestCell * cellScale)}ch + 2.5rem)`
  25. }