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 | 6x 10x 10x 10x 10x 10x 1x 9x 1x 9x 1x 9x 10x 2x 6x 4x 4x 4x 6x 4x 6x 2x 2x 2x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 2x 2x 2x 2x 1x 1x 1x 1x 2x | import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import {
DocumentDownloadProvider,
DownloadPdfButton,
type DocumentDownloader,
type DocumentImageUrl,
} from "@/components/document";
import {
AutomationConfig,
AutomationPayload,
ComparisonAxis,
ComparisonPayload,
ComparisonRow,
} from "@/objects/Dashboard";
import { extractDocuments } from "@/objects/Document";
import { useApiHandler } from "@/services/useApiHandler";
import React from "react";
import { TONES, toneForLabel } from "@/components/appearance/tones";
import { WidgetBodyProps } from "./types";
// ─── Automation ───────────────────────────────────────────────────────────────
/** A tile's latest answer. Its document downloads from what the tile stored
* (svc-core `GET /dashboards/{id}/widgets/{id}/document.pdf`), and with the
* tile's PDF on, an answer with no document downloads as itself — so a PDF
* tile offers exactly one download on every refresh. */
export const AutomationWidgetBody: React.FC<WidgetBodyProps> = ({ widget }) => {
const { getApiHandler } = useApiHandler();
const payload = widget.cache_payload as unknown as AutomationPayload | null;
const config = widget.config as unknown as Partial<AutomationConfig> | null;
const markdown = payload?.markdown?.trim();
if (!markdown) {
return (
<p className="text-xs text-gray-400 dark:text-gray-500 italic">
No result yet — this runs on its next refresh.
</p>
);
}
const download: DocumentDownloader = (index, document) =>
getApiHandler().dashboard_router.downloadWidgetDocument(
widget.dashboard_id,
widget.id,
index,
`${document.title}.pdf`,
);
const imageUrl: DocumentImageUrl = (asset) =>
getApiHandler().dashboard_router.widgetImageUrl(
widget.dashboard_id,
widget.id,
asset,
);
const answerOnly =
config?.pdf === true && extractDocuments(markdown).documents.length === 0;
return (
<DocumentDownloadProvider download={download} imageUrl={imageUrl}>
<RichMarkdown content={markdown} />
{answerOnly && (
<div className="mt-3 flex justify-end">
<DownloadPdfButton
download={() =>
getApiHandler().dashboard_router.downloadWidgetDocument(
widget.dashboard_id,
widget.id,
0,
`${widget.title || "Dashboard tile"}.pdf`,
)
}
/>
</div>
)}
</DocumentDownloadProvider>
);
};
// ─── Comparison ───────────────────────────────────────────────────────────────
/** Where a value sits along its axis, as a 0–1 fraction. */
const fraction = (value: number, axis: ComparisonAxis): number => {
const span = axis.max - axis.min;
Iif (span <= 0) return 0;
return Math.min(1, Math.max(0, (value - axis.min) / span));
};
const formatScore = (value: number | null): string =>
value === null ? "—" : Number.isInteger(value) ? String(value) : value.toFixed(1);
/** One subject's position on one axis, as a track with a marker.
*
* A track rather than a bar: these axes are positional (where does this
* outlet sit between "Left" and "Right"), not magnitudes, and a bar growing
* from zero would misread a -1..1 scale entirely.
*/
const AxisTrack: React.FC<{
value: number | null;
axis: ComparisonAxis;
colour: string;
}> = ({ value, axis, colour }) => {
Iif (value === null) {
return (
<div className="h-1.5 rounded-full bg-gray-100 dark:bg-gray-700/60" />
);
}
const pct = fraction(value, axis) * 100;
return (
<div className="relative h-1.5 rounded-full bg-gray-100 dark:bg-gray-700/60">
{/* Midpoint tick, so a diverging scale reads against its centre. */}
<span className="absolute inset-y-0 left-1/2 w-px bg-gray-300 dark:bg-gray-600" />
<span
className="absolute top-1/2 h-2.5 w-2.5 -translate-y-1/2 -translate-x-1/2 rounded-full ring-2 ring-white dark:ring-gray-800"
style={{ left: `${pct}%`, backgroundColor: colour }}
/>
</div>
);
};
/** Two axes plotted against each other — the shape the comparison is for. */
const ScatterPlot: React.FC<{
rows: ComparisonRow[];
axes: ComparisonAxis[];
}> = ({ rows, axes }) => {
const [xAxis, yAxis] = axes;
const plotted = rows.filter(
(row) =>
row.scores[xAxis.name] !== null && row.scores[yAxis.name] !== null,
);
Iif (plotted.length === 0) return null;
return (
<div className="mb-3">
<div className="relative aspect-[4/3] w-full rounded-lg border border-gray-100 dark:border-gray-700/60 bg-gray-50/50 dark:bg-gray-900/20">
{/* Quadrant guides */}
<span className="absolute inset-x-0 top-1/2 h-px bg-gray-200 dark:bg-gray-700" />
<span className="absolute inset-y-0 left-1/2 w-px bg-gray-200 dark:bg-gray-700" />
{plotted.map((row) => {
const x = fraction(row.scores[xAxis.name] as number, xAxis) * 100;
const y = fraction(row.scores[yAxis.name] as number, yAxis) * 100;
const colour = TONES[toneForLabel(row.subject)].hex;
return (
<span
key={row.subject}
title={`${row.subject} — ${xAxis.name}: ${formatScore(
row.scores[xAxis.name],
)}, ${yAxis.name}: ${formatScore(row.scores[yAxis.name])}`}
className="absolute -translate-x-1/2 translate-y-1/2 flex flex-col items-center gap-0.5"
style={{ left: `${x}%`, bottom: `${y}%` }}
>
<span
className="h-2.5 w-2.5 rounded-full ring-2 ring-white dark:ring-gray-800"
style={{ backgroundColor: colour }}
/>
<span className="text-[9px] leading-none text-gray-500 dark:text-gray-400 whitespace-nowrap">
{row.subject}
</span>
</span>
);
})}
</div>
<div className="mt-1 flex items-center justify-between text-[10px] text-gray-400 dark:text-gray-500">
<span>{xAxis.min_label || xAxis.min}</span>
<span className="font-medium text-gray-500 dark:text-gray-400">
{xAxis.name} → · ↑ {yAxis.name}
</span>
<span>{xAxis.max_label || xAxis.max}</span>
</div>
</div>
);
};
export const ComparisonWidgetBody: React.FC<WidgetBodyProps> = ({ widget }) => {
const payload = widget.cache_payload as unknown as ComparisonPayload | null;
const rows = payload?.rows ?? [];
const axes = payload?.axes ?? [];
if (rows.length === 0) {
return (
<p className="text-xs text-gray-400 dark:text-gray-500 italic">
No comparison yet — this runs on its next refresh.
</p>
);
}
return (
<div>
{axes.length === 2 && <ScatterPlot rows={rows} axes={axes} />}
<div className="space-y-2.5">
{rows.map((row) => {
const colour = TONES[toneForLabel(row.subject)].hex;
return (
<div key={row.subject}>
<div className="flex items-baseline justify-between gap-2">
<span className="flex items-center gap-1.5 min-w-0">
<span
className="h-2 w-2 shrink-0 rounded-full"
style={{ backgroundColor: colour }}
/>
<span className="text-xs font-medium text-gray-800 dark:text-gray-100 truncate">
{row.subject}
</span>
</span>
{row.missing && (
<span className="text-[10px] text-amber-600 dark:text-amber-400 shrink-0">
no data
</span>
)}
</div>
<div className="mt-1 space-y-1">
{axes.map((axis) => (
<div key={axis.name} className="flex items-center gap-2">
<span className="w-24 shrink-0 truncate text-[10px] text-gray-400 dark:text-gray-500">
{axis.name}
</span>
<span className="flex-1">
<AxisTrack
value={row.scores[axis.name] ?? null}
axis={axis}
colour={colour}
/>
</span>
<span className="w-8 shrink-0 text-right text-[10px] tabular-nums text-gray-500 dark:text-gray-400">
{formatScore(row.scores[axis.name] ?? null)}
</span>
</div>
))}
</div>
{row.note && (
<p className="mt-1 pl-3.5 text-[11px] leading-snug text-gray-500 dark:text-gray-400">
{row.note}
</p>
)}
</div>
);
})}
</div>
{payload?.summary && (
<p className="mt-3 pt-2 border-t border-gray-100 dark:border-gray-700/60 text-[11px] italic text-gray-500 dark:text-gray-400">
{payload.summary}
</p>
)}
</div>
);
};
|