All files / src/i18n role-labels.ts

67.64% Statements 23/34
100% Branches 10/10
66.66% Functions 2/3
67.64% Lines 23/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                    1x 9x 9x 9x 9x 7x 9x 2x 9x 9x         1x                           1x 6x 6x 2x 6x 1x 6x 1x 6x 2x 6x 6x      
import type { UserRole } from "@/types"
 
/**
 * Central role-label registry. Views must call ``t(roleLabelKey(role))``
 * instead of inlining their own ``case 'admin': return …`` blocks —
 * this guarantees a single canonical translation per role and survives
 * locale switches.
 *
 * The keys live in ``i18n/locales/{de,en}.ts`` under ``roleLabels.*``.
 */
export function roleLabelKey(role: string | undefined | null): string {
  switch (role) {
    case "admin":
    case "teacher":
    case "student":
      return `roleLabels.${role}`
    default:
      return "roleLabels.unknown"
  }
}
 
/** Tailwind badge class per role, for places where the colour is co-located
 *  with the label (avatars / chips). Keeps the inline ``switch`` blocks
 *  out of view files. */
export function roleBadgeClass(role: string | undefined | null): string {
  switch (role) {
    case "admin":
      return "bg-red-50 text-red-700"
    case "teacher":
      return "bg-purple-50 text-purple-700"
    case "student":
      return "bg-blue-50 text-blue-700"
    default:
      return "bg-gray-50 text-gray-700"
  }
}
 
/** Variant name for the ``<Badge>`` UI component. */
export function roleBadgeVariant(role: string | undefined | null): 'yellow' | 'green' | 'red' | 'purple' | 'blue' | 'gray' {
  switch (role) {
    case "admin":
      return "purple"
    case "teacher":
      return "blue"
    case "student":
      return "green"
    default:
      return "gray"
  }
}
 
export type { UserRole }