|
| 1 | +import { |
| 2 | + AlertTriangleIcon, |
| 3 | + ArrowRightIcon, |
| 4 | + CalendarIcon, |
| 5 | + CrosshairIcon, |
| 6 | + DollarSignIcon, |
| 7 | + HighlighterIcon, |
| 8 | + LightbulbIcon, |
| 9 | + MapPinIcon, |
| 10 | + MessageSquareIcon, |
| 11 | +} from "lucide-react"; |
| 12 | +import type { FeedAction } from "../lib/feed-actions"; |
| 13 | + |
| 14 | +type Variant = "insight" | "anomaly"; |
| 15 | +type Severity = "low" | "medium" | "high"; |
| 16 | + |
| 17 | +interface ActionableCardProps { |
| 18 | + variant: Variant; |
| 19 | + severity?: Severity; |
| 20 | + title: string; |
| 21 | + description: string; |
| 22 | + actions: FeedAction[]; |
| 23 | + /** Fired for non-ask actions. Route applies them to dashboard state. */ |
| 24 | + onAction: (action: FeedAction) => void; |
| 25 | + /** Fired for `ask` actions. Route forwards the prompt to the chat drawer. */ |
| 26 | + onAsk: (prompt: string) => void; |
| 27 | +} |
| 28 | + |
| 29 | +// Backgrounds are written as arbitrary 8-digit hex (e.g. `bg-[#eff6ff80]`) |
| 30 | +// instead of Tailwind's `/N` alpha shorthand. Rationale: `bg-blue-50/50` |
| 31 | +// compiles in Tailwind v4 to a pair — an sRGB hex fallback and a |
| 32 | +// `@supports (color-mix)` override that re-mixes in oklab over the oklch |
| 33 | +// palette token. Browsers that support `color-mix` (recent Chrome/Arc) take |
| 34 | +// the oklab path; older embedded Chromiums (e.g. Cursor's built-in browser |
| 35 | +// at the time of writing) fall through to the sRGB hex. Because oklab and |
| 36 | +// sRGB interpolation produce visibly different tints — especially against |
| 37 | +// the dark `--card` token — the same card ends up looking different in each |
| 38 | +// browser. Pinning the colour to a literal hex (no `/N`, no @supports |
| 39 | +// override) keeps all browsers on the same sRGB path and therefore the same |
| 40 | +// visual result. |
| 41 | +const INSIGHT_STYLES = { |
| 42 | + border: "border-blue-200 dark:border-blue-900", |
| 43 | + bg: "bg-[#eff6ff80] dark:bg-[#1624564d]", |
| 44 | + icon: "text-blue-500", |
| 45 | +}; |
| 46 | + |
| 47 | +const ANOMALY_STYLES: Record< |
| 48 | + Severity, |
| 49 | + { border: string; bg: string; icon: string; badge: string } |
| 50 | +> = { |
| 51 | + low: { |
| 52 | + border: "border-yellow-200 dark:border-yellow-900", |
| 53 | + bg: "bg-[#fefce880] dark:bg-[#4320044d]", |
| 54 | + icon: "text-yellow-500", |
| 55 | + badge: |
| 56 | + "bg-yellow-100 text-yellow-700 dark:bg-yellow-900/50 dark:text-yellow-400", |
| 57 | + }, |
| 58 | + medium: { |
| 59 | + border: "border-orange-200 dark:border-orange-900", |
| 60 | + bg: "bg-[#fff7ed80] dark:bg-[#4413064d]", |
| 61 | + icon: "text-orange-500", |
| 62 | + badge: |
| 63 | + "bg-orange-100 text-orange-700 dark:bg-orange-900/50 dark:text-orange-400", |
| 64 | + }, |
| 65 | + high: { |
| 66 | + border: "border-red-200 dark:border-red-900", |
| 67 | + bg: "bg-[#fef2f280] dark:bg-[#4608094d]", |
| 68 | + icon: "text-red-500", |
| 69 | + badge: "bg-red-100 text-red-700 dark:bg-red-900/50 dark:text-red-400", |
| 70 | + }, |
| 71 | +}; |
| 72 | + |
| 73 | +function iconForAction(kind: FeedAction["kind"]): React.ReactNode { |
| 74 | + const cls = "h-3 w-3"; |
| 75 | + switch (kind) { |
| 76 | + case "filter_date": |
| 77 | + return <CalendarIcon className={cls} />; |
| 78 | + case "filter_zip": |
| 79 | + return <MapPinIcon className={cls} />; |
| 80 | + case "filter_fare": |
| 81 | + return <DollarSignIcon className={cls} />; |
| 82 | + case "highlight_period": |
| 83 | + return <HighlighterIcon className={cls} />; |
| 84 | + case "highlight_zone": |
| 85 | + return <MapPinIcon className={cls} />; |
| 86 | + case "focus_chart": |
| 87 | + return <CrosshairIcon className={cls} />; |
| 88 | + case "ask": |
| 89 | + return <MessageSquareIcon className={cls} />; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Action chip for a single feed suggestion. The chip's visual weight depends |
| 95 | + * on its kind: structural mutations (filter/highlight/focus) use the primary |
| 96 | + * tint, `ask` uses a neutral outline so the user can tell "this opens the |
| 97 | + * chat" from "this changes the dashboard" without reading the label. |
| 98 | + */ |
| 99 | +function ActionChip({ |
| 100 | + action, |
| 101 | + onAction, |
| 102 | + onAsk, |
| 103 | +}: { |
| 104 | + action: FeedAction; |
| 105 | + onAction: (a: FeedAction) => void; |
| 106 | + onAsk: (prompt: string) => void; |
| 107 | +}) { |
| 108 | + const isAsk = action.kind === "ask"; |
| 109 | + const isHighlight = |
| 110 | + action.kind === "highlight_period" || action.kind === "highlight_zone"; |
| 111 | + |
| 112 | + return ( |
| 113 | + <button |
| 114 | + type="button" |
| 115 | + onClick={() => { |
| 116 | + if (isAsk) onAsk(action.prompt); |
| 117 | + else onAction(action); |
| 118 | + }} |
| 119 | + className={`inline-flex items-center gap-1 text-[11px] font-medium px-2 py-1 rounded-md transition-colors ${ |
| 120 | + isAsk |
| 121 | + ? "border border-border bg-background text-foreground/80 hover:bg-muted hover:text-foreground" |
| 122 | + : isHighlight |
| 123 | + ? "bg-amber-100 text-amber-800 hover:bg-amber-200 dark:bg-amber-900/40 dark:text-amber-200 dark:hover:bg-amber-900/60" |
| 124 | + : "bg-primary/10 text-primary hover:bg-primary/20" |
| 125 | + }`} |
| 126 | + > |
| 127 | + {iconForAction(action.kind)} |
| 128 | + <span>{action.label}</span> |
| 129 | + {isAsk && <ArrowRightIcon className="h-3 w-3 opacity-70" />} |
| 130 | + </button> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export function ActionableCard({ |
| 135 | + variant, |
| 136 | + severity, |
| 137 | + title, |
| 138 | + description, |
| 139 | + actions, |
| 140 | + onAction, |
| 141 | + onAsk, |
| 142 | +}: ActionableCardProps) { |
| 143 | + const isAnomaly = variant === "anomaly"; |
| 144 | + const styles = isAnomaly |
| 145 | + ? ANOMALY_STYLES[severity ?? "low"] |
| 146 | + : { ...INSIGHT_STYLES, badge: "" }; |
| 147 | + |
| 148 | + return ( |
| 149 | + <div className={`rounded-lg border ${styles.border} ${styles.bg} p-3`}> |
| 150 | + <div className="flex items-start gap-2 mb-2"> |
| 151 | + {isAnomaly ? ( |
| 152 | + <AlertTriangleIcon |
| 153 | + className={`h-4 w-4 ${styles.icon} mt-0.5 shrink-0`} |
| 154 | + /> |
| 155 | + ) : ( |
| 156 | + <LightbulbIcon className={`h-4 w-4 ${styles.icon} mt-0.5 shrink-0`} /> |
| 157 | + )} |
| 158 | + <div className="min-w-0 flex-1"> |
| 159 | + <div className="flex items-start gap-2"> |
| 160 | + <p className="text-sm font-medium text-foreground leading-tight flex-1"> |
| 161 | + {title} |
| 162 | + </p> |
| 163 | + {isAnomaly && severity && ( |
| 164 | + <span |
| 165 | + className={`text-[10px] font-medium px-1.5 py-0.5 rounded shrink-0 ${styles.badge}`} |
| 166 | + > |
| 167 | + {severity} |
| 168 | + </span> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + <p className="text-xs text-muted-foreground mt-1 leading-relaxed"> |
| 172 | + {description} |
| 173 | + </p> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + |
| 177 | + {actions.length > 0 && ( |
| 178 | + <div className="flex flex-wrap gap-1.5 pl-6"> |
| 179 | + {actions.map((action, i) => ( |
| 180 | + <ActionChip |
| 181 | + key={`${action.kind}-${i}-${action.label}`} |
| 182 | + action={action} |
| 183 | + onAction={onAction} |
| 184 | + onAsk={onAsk} |
| 185 | + /> |
| 186 | + ))} |
| 187 | + </div> |
| 188 | + )} |
| 189 | + </div> |
| 190 | + ); |
| 191 | +} |
0 commit comments