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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 3x 16x 15x 15x 14x 14x 12x 12x 12x 12x 31x 19x 19x 12x 12x 3x 3x 6x 6x 6x 6x 6x 6x 4x 6x | import { DashboardWidget, DataKind } from "@/objects/Dashboard";
import { Tone, TONES } from "@/components/appearance/tones";
import {
AlertTriangle,
Database,
Loader2,
MoreVertical,
Pencil,
RefreshCw,
Radio,
Sparkles,
Trash2,
} from "lucide-react";
import React from "react";
import { parseServerDate } from "@/util/time";
/** Relative "2 hours ago" without pulling a formatter into every tile. */
export const timeAgo = (iso: string | null): string => {
if (!iso) return "never";
const then = parseServerDate(iso).getTime();
if (Number.isNaN(then)) return "never";
const seconds = Math.max(0, Math.round((Date.now() - then) / 1000));
if (seconds < 60) return "just now";
// Each rung is "how many of the current unit make one of the next", so the
// same number is both the threshold to climb and the divisor to climb by.
const ladder: [number, string][] = [
[60, "hour"],
[24, "day"],
[7, "week"],
[4.35, "month"],
];
let value = seconds / 60;
let label = "minute";
for (const [per, next] of ladder) {
if (value < per) break;
value /= per;
label = next;
}
const rounded = Math.floor(value);
return `${rounded} ${label}${rounded === 1 ? "" : "s"} ago`;
};
/** How each provenance class presents itself.
*
* A board mixes exact data with model inference, and the difference matters:
* a forecast is a fact from a named source, a comparison is a model's
* judgement that can be wrong. Every tile says which it is, in the same place,
* so the distinction is never something the reader has to infer from the
* widget's shape.
*/
const PROVENANCE: Record<
DataKind,
{ icon: React.ElementType | null; label: string; className: string }
> = {
stored: { icon: null, label: "", className: "" },
platform: {
icon: Database,
label: "Your data",
className: "text-gray-400 dark:text-gray-500",
},
feed: {
icon: Radio,
label: "Live source",
className: "text-sky-600 dark:text-sky-400",
},
generated: {
icon: Sparkles,
label: "AI generated",
className: "text-violet-600 dark:text-violet-400",
},
};
export interface WidgetFrameProps {
widget: DashboardWidget;
tone: Tone;
icon: React.ElementType;
/** Drives the provenance marker under the title. */
dataKind: DataKind;
/** Named origin for a feed tile, e.g. "Open-Meteo" or the feed's title. */
sourceName?: string | null;
editing: boolean;
isDynamic: boolean;
/** A built-in board: no refresh, configure or remove — the API refuses
* each, and its tiles refresh on their own cadence. */
readOnly?: boolean;
onEdit: () => void;
onDelete: () => void;
onRefresh: () => void;
children: React.ReactNode;
}
/** The chrome every tile shares: header, controls, and cache state.
*
* Frame owns the refreshing/failed presentation so each renderer only has to
* draw its own content for the happy path.
*/
const WidgetFrame: React.FC<WidgetFrameProps> = ({
widget,
tone,
icon: Icon,
dataKind,
sourceName,
editing,
isDynamic,
readOnly = false,
onEdit,
onDelete,
onRefresh,
children,
}) => {
const [menuOpen, setMenuOpen] = React.useState(false);
const t = TONES[tone];
const provenance = PROVENANCE[dataKind] ?? PROVENANCE.stored;
const refreshing = widget.cache_status === "refreshing";
const failed = widget.cache_status === "failed";
// Close the menu on any outside click, so a tile never keeps a stray popover
// open while the user drags another one.
React.useEffect(() => {
Eif (!menuOpen) return undefined;
const close = () => setMenuOpen(false);
window.addEventListener("click", close);
return () => window.removeEventListener("click", close);
}, [menuOpen]);
return (
<div className="h-full w-full flex flex-col rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm overflow-hidden">
{/* Header — doubles as the drag handle while the board is in edit mode */}
<div
className={`widget-drag-handle flex items-start gap-2 px-3 py-2 border-b border-gray-100 dark:border-gray-700/60 ${
editing ? "cursor-move" : ""
}`}
>
<span className={`shrink-0 rounded-lg p-1.5 ${t.bg}`}>
<Icon className={`w-4 h-4 ${t.fg}`} />
</span>
<div className="min-w-0 flex-1">
<p className="text-sm font-semibold text-gray-900 dark:text-white truncate">
{widget.title || "Untitled"}
</p>
{(provenance.icon || isDynamic) && (
<p className="flex items-center gap-1 text-[11px] truncate">
{provenance.icon && (
<span
className={`inline-flex items-center gap-0.5 shrink-0 ${provenance.className}`}
>
<provenance.icon className="w-2.5 h-2.5" />
{sourceName || provenance.label}
</span>
)}
{isDynamic && (
<span className="text-gray-400 dark:text-gray-500 truncate">
{provenance.icon ? "· " : ""}
{timeAgo(widget.cached_at)}
</span>
)}
</p>
)}
</div>
{!readOnly && (
<div className="flex items-center gap-0.5 shrink-0">
{isDynamic && (
<button
type="button"
title="Refresh now"
onClick={onRefresh}
disabled={refreshing}
className="p-1.5 rounded-lg text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 disabled:opacity-40 transition-colors"
>
<RefreshCw
className={`w-3.5 h-3.5 ${refreshing ? "animate-spin" : ""}`}
/>
</button>
)}
<div className="relative">
<button
type="button"
title="Widget options"
onClick={(e) => {
e.stopPropagation();
setMenuOpen((open) => !open);
}}
className="p-1.5 rounded-lg text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
>
<MoreVertical className="w-3.5 h-3.5" />
</button>
{menuOpen && (
<div className="absolute right-0 top-8 z-20 w-36 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-lg py-1">
<button
type="button"
onClick={onEdit}
className="w-full flex items-center gap-2 px-3 py-1.5 text-xs text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700"
>
<Pencil className="w-3.5 h-3.5" /> Configure
</button>
<button
type="button"
onClick={onDelete}
className="w-full flex items-center gap-2 px-3 py-1.5 text-xs text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20"
>
<Trash2 className="w-3.5 h-3.5" /> Remove
</button>
</div>
)}
</div>
</div>
)}
</div>
{/* Body */}
<div className="flex-1 min-h-0 overflow-auto p-3">
{failed ? (
<div className="h-full flex flex-col items-center justify-center text-center gap-2 px-3">
<AlertTriangle className="w-5 h-5 text-amber-500" />
<p className="text-xs text-gray-500 dark:text-gray-400">
Could not refresh this widget.
</p>
{widget.cache_error && (
<p className="text-[11px] text-gray-400 dark:text-gray-500 line-clamp-3">
{widget.cache_error}
</p>
)}
{!readOnly && (
<button
type="button"
onClick={onRefresh}
className="mt-1 text-[11px] font-medium text-blue-600 dark:text-blue-400 hover:underline"
>
Try again
</button>
)}
</div>
) : refreshing && !widget.cache_payload ? (
<div className="h-full flex flex-col items-center justify-center gap-2 text-gray-400">
<Loader2 className="w-5 h-5 animate-spin" />
<p className="text-xs">Generating…</p>
</div>
) : (
children
)}
</div>
</div>
);
};
export default WidgetFrame;
|