|
| 1 | +/** |
| 2 | + * StateHistoryTimeline - Reusable timeline component for displaying state history |
| 3 | + */ |
| 4 | + |
| 5 | +import { Userpic, cn, Typography } from "@humansignal/ui"; |
| 6 | +import type { StateHistoryItem } from "../../hooks/useStateHistory"; |
| 7 | +import { formatStateName, formatTimestamp, formatUserName } from "./utils"; |
| 8 | +import { getStateVisuals } from "./state-visuals"; |
| 9 | + |
| 10 | +export interface StateHistoryTimelineProps { |
| 11 | + history: StateHistoryItem[]; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Get user initials from triggered_by object |
| 16 | + */ |
| 17 | +function getUserInitials( |
| 18 | + triggeredBy: { |
| 19 | + first_name?: string; |
| 20 | + last_name?: string; |
| 21 | + email?: string; |
| 22 | + } | null, |
| 23 | +): string { |
| 24 | + if (!triggeredBy) return "SY"; |
| 25 | + |
| 26 | + const { first_name, last_name, email } = triggeredBy; |
| 27 | + |
| 28 | + if (first_name && last_name) { |
| 29 | + return `${first_name.charAt(0)}${last_name.charAt(0)}`.toUpperCase(); |
| 30 | + } |
| 31 | + if (first_name) return first_name.slice(0, 2).toUpperCase(); |
| 32 | + if (last_name) return last_name.slice(0, 2).toUpperCase(); |
| 33 | + if (email) return email.slice(0, 2).toUpperCase(); |
| 34 | + |
| 35 | + return "SY"; |
| 36 | +} |
| 37 | + |
| 38 | +export interface TimelineItemProps { |
| 39 | + item: StateHistoryItem; |
| 40 | + index: number; |
| 41 | + isLast: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Timeline item component for a single state history entry |
| 46 | + */ |
| 47 | +export function TimelineItem({ item, index, isLast }: TimelineItemProps) { |
| 48 | + const isCurrent = index === 0; |
| 49 | + const stateLabel = formatStateName(item.state); |
| 50 | + const visuals = getStateVisuals(stateLabel); |
| 51 | + const StateIcon = visuals.icon; |
| 52 | + |
| 53 | + // Current state (index 0) gets bold/base colors, past states get subtle colors |
| 54 | + const bgColor = isCurrent ? visuals.baseBg : visuals.subtleBg; |
| 55 | + const iconColor = isCurrent ? visuals.baseIconColor : visuals.subtleIconColor; |
| 56 | + |
| 57 | + // Text color: current state is dark, all past states are subtle |
| 58 | + const labelClass = isCurrent ? "text-neutral-content" : "text-neutral-content-subtle"; |
| 59 | + |
| 60 | + const userName = formatUserName(item.triggered_by); |
| 61 | + const isSystem = userName === "System"; |
| 62 | + const reason = item.reason; |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className="flex gap-2 items-start relative"> |
| 66 | + {/* Timeline connector line - positioned behind icon, extends to next icon */} |
| 67 | + {!isLast && ( |
| 68 | + <div className="absolute w-px bg-neutral-border" style={{ left: "16px", top: "38px", bottom: "4px" }} /> |
| 69 | + )} |
| 70 | + {/* Icon column */} |
| 71 | + <div className="flex flex-col items-center shrink-0 pt-0.5"> |
| 72 | + {/* State icon with circular background - 32px circle with 24px icon */} |
| 73 | + <div className="rounded-full size-8 p-1 flex items-center justify-center" style={{ backgroundColor: bgColor }}> |
| 74 | + <StateIcon className="w-6 h-6 shrink-0" style={{ color: iconColor }} /> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + |
| 78 | + {/* Content */} |
| 79 | + <div className={cn("flex flex-col gap-0.5 flex-1 min-h-10 justify-center min-w-0", !isLast && "pb-6")}> |
| 80 | + {/* State name and optional reason */} |
| 81 | + <div className="flex flex-col gap-1"> |
| 82 | + <Typography variant="label" size="small" className={`${labelClass} truncate`}> |
| 83 | + {stateLabel} |
| 84 | + </Typography> |
| 85 | + {reason && ( |
| 86 | + <Typography variant="body" size="smaller" className="text-neutral-content-subtler mt-0.5"> |
| 87 | + {reason} |
| 88 | + </Typography> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + |
| 92 | + {/* Metadata row */} |
| 93 | + <div className="flex items-center gap-2 text-neutral-content-subtler"> |
| 94 | + {/* Author section */} |
| 95 | + {!isSystem && ( |
| 96 | + <> |
| 97 | + <div className="flex items-center gap-1 shrink-0"> |
| 98 | + <Userpic size={20} user={item.triggered_by} username={getUserInitials(item.triggered_by)} /> |
| 99 | + <Typography variant="body" size="smaller"> |
| 100 | + {userName} |
| 101 | + </Typography> |
| 102 | + </div> |
| 103 | + {/* Dot separator */} |
| 104 | + <div className="size-[3px] rounded-full bg-neutral-content-subtler shrink-0" /> |
| 105 | + </> |
| 106 | + )} |
| 107 | + {isSystem && ( |
| 108 | + <> |
| 109 | + <Typography variant="body" size="smaller"> |
| 110 | + System |
| 111 | + </Typography> |
| 112 | + {/* Dot separator */} |
| 113 | + <div className="size-[3px] rounded-full bg-neutral-content-subtler shrink-0" /> |
| 114 | + </> |
| 115 | + )} |
| 116 | + {/* Timestamp */} |
| 117 | + <Typography variant="body" size="smaller"> |
| 118 | + {formatTimestamp(item.created_at)} |
| 119 | + </Typography> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Timeline component that renders a list of state history items |
| 128 | + */ |
| 129 | +export function StateHistoryTimeline({ history }: StateHistoryTimelineProps) { |
| 130 | + return ( |
| 131 | + <div className="flex flex-col"> |
| 132 | + {history.map((item: StateHistoryItem, index: number) => ( |
| 133 | + <TimelineItem |
| 134 | + key={`${item.state}-${item.created_at}-${index}`} |
| 135 | + item={item} |
| 136 | + index={index} |
| 137 | + isLast={index === history.length - 1} |
| 138 | + /> |
| 139 | + ))} |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments