format.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Display formatters for the Telegram bots feature. Kept as
  3. * pure functions / components so they're easy to test
  4. * independently of React.
  5. */
  6. import { Badge } from '@/components/ui/badge';
  7. import type { TelegramBotStatus } from './types';
  8. const STATUS_LABEL: Record<TelegramBotStatus, string> = {
  9. active: 'Active',
  10. paused: 'Paused',
  11. };
  12. const STATUS_VARIANT: Record<TelegramBotStatus, 'success' | 'warning'> = {
  13. active: 'success',
  14. paused: 'warning',
  15. };
  16. export function statusLabel(s: TelegramBotStatus): string {
  17. return STATUS_LABEL[s] ?? s;
  18. }
  19. export function statusVariant(s: TelegramBotStatus): 'success' | 'warning' {
  20. return STATUS_VARIANT[s] ?? 'warning';
  21. }
  22. export function StatusBadge({ status }: { status: TelegramBotStatus }) {
  23. return <Badge variant={statusVariant(status)}>{statusLabel(status)}</Badge>;
  24. }
  25. export function tokenSetLabel(set: boolean): string {
  26. return set ? 'Configured' : 'Not set';
  27. }
  28. export function tokenSetVariant(
  29. set: boolean,
  30. ): 'success' | 'warning' {
  31. return set ? 'success' : 'warning';
  32. }
  33. export function TokenSetBadge({ set }: { set: boolean }) {
  34. return <Badge variant={tokenSetVariant(set)}>{tokenSetLabel(set)}</Badge>;
  35. }
  36. export function formatDate(iso: string | null | undefined): string {
  37. if (!iso) return '\u2014';
  38. const d = new Date(iso);
  39. if (Number.isNaN(d.getTime())) return iso;
  40. return d.toLocaleDateString(undefined, {
  41. year: 'numeric',
  42. month: 'short',
  43. day: 'numeric',
  44. });
  45. }
  46. export function formatDateTime(iso: string | null | undefined): string {
  47. if (!iso) return '\u2014';
  48. const d = new Date(iso);
  49. if (Number.isNaN(d.getTime())) return iso;
  50. return d.toLocaleString(undefined, {
  51. year: 'numeric',
  52. month: 'short',
  53. day: 'numeric',
  54. hour: '2-digit',
  55. minute: '2-digit',
  56. });
  57. }
  58. /**
  59. * Truncate a welcome message / description for table display.
  60. * Keeps the first N chars and adds an ellipsis.
  61. */
  62. export function truncate(s: string | undefined | null, n = 60): string {
  63. if (!s) return '\u2014';
  64. if (s.length <= n) return s;
  65. return s.slice(0, n - 1) + '\u2026';
  66. }