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 254 255 256 257 258 259 260 261 262 263 | 104x 104x 24x 24x 24x 104x 24x 24x 24x 24x 24x 1x 104x 306x 306x 306x 306x 306x 18x 306x 306x 12x 306x 114x 192x 174x 18x 23x | import AIGeneratedBadge from "@/components/badge/AIGeneratedBadge";
import DiffViewer from "@/components/diff/DiffViewer/DiffViewer";
import GenerationDetails from "@/components/diff/GenerationDetails/GenerationDetails";
import {
DiffResponse,
VersionList,
VersionSummary,
versionActor,
} from "@/objects/History";
import { useQuery } from "@tanstack/react-query";
import { ChevronDown, History, Pencil, Plus, Trash2 } from "lucide-react";
import React, { useMemo, useState } from "react";
import { parseServerDate } from "@/util/time";
const ACTION_ICON: Record<VersionSummary["action"], React.ElementType> = {
created: Plus,
updated: Pencil,
deleted: Trash2,
};
const formatTimestamp = (iso?: string | null): string => {
Iif (!iso) return "โ";
const date = parseServerDate(iso);
return `${date.toLocaleDateString()} ${date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})}`;
};
const VersionRow: React.FC<{
version: VersionSummary;
selected: boolean;
isBaseline: boolean;
onClick: () => void;
}> = ({ version, selected, isBaseline, onClick }) => {
const Icon = ACTION_ICON[version.action];
const actor = versionActor(version);
const [showGeneration, setShowGeneration] = useState(false);
const generation = version.generation;
return (
<div
className={`rounded-lg border transition-colors ${
selected
? "border-blue-300 dark:border-blue-700 bg-blue-50 dark:bg-blue-950/30"
: "border-transparent hover:bg-gray-50 dark:hover:bg-gray-800/60"
}`}
>
<button
type="button"
onClick={onClick}
className="w-full text-left flex items-start gap-2.5 px-3 py-2.5"
>
<span
className={`mt-0.5 flex items-center justify-center size-5 rounded-full shrink-0 ${
version.action === "created"
? "bg-green-100 dark:bg-green-900/50 text-green-700 dark:text-green-300"
: version.action === "deleted"
? "bg-red-100 dark:bg-red-900/50 text-red-700 dark:text-red-300"
: "bg-amber-100 dark:bg-amber-900/50 text-amber-700 dark:text-amber-300"
}`}
>
<Icon className="size-3" />
</span>
<span className="flex-1 min-w-0">
<span className="flex items-center gap-1.5 flex-wrap">
<span className="text-xs font-semibold text-gray-700 dark:text-gray-300">
v{version.version_number}
</span>
{isBaseline && (
<span className="text-[10px] text-gray-400 dark:text-gray-500">
(baseline)
</span>
)}
{version.is_ai_generated && <AIGeneratedBadge compact />}
</span>
<span
className="block text-xs text-gray-500 dark:text-gray-400 truncate"
title={
version.actor_kind === "platform"
? "A CHAIN-R release โ built-in resources change only when CHAIN-R updates them"
: undefined
}
>
{actor}
{version.actor_kind === "platform" ? " release" : ""} ยท{" "}
{formatTimestamp(version.created_at)}
</span>
{version.changed_fields.length > 0 && (
<span className="block text-[11px] text-gray-400 dark:text-gray-500 truncate">
{version.changed_fields.join(", ")}
</span>
)}
</span>
</button>
{/* Provenance. Nested as a sibling button rather than inside the row
above, because a button cannot contain another button. */}
{generation && (
<div className="px-3 pb-2.5 -mt-1">
<button
type="button"
onClick={() => setShowGeneration((open) => !open)}
aria-expanded={showGeneration}
className="flex items-center gap-1 text-[11px] font-medium text-violet-700 dark:text-violet-300 hover:underline"
>
<ChevronDown
className={`size-3 transition-transform ${
showGeneration ? "" : "-rotate-90"
}`}
/>
Why this exists
</button>
{showGeneration && (
<GenerationDetails generationId={generation.id} summary={generation} />
)}
</div>
)}
</div>
);
};
export interface ResourceHistoryPanelProps {
/** e.g. () => getApiHandler().agent_router.history(resourceId) */
fetchHistory: () => Promise<VersionList | null>;
/** e.g. (to, from) => getApiHandler().agent_router.historyDiff(resourceId, to, from) */
fetchDiff: (
toVersion: number,
fromVersion: number | null,
) => Promise<DiffResponse | null>;
historyQueryKey: unknown[];
fieldLabels?: Record<string, string>;
hiddenFields?: string[];
/**
* Version to open on, instead of the newest.
*
* Set when arriving from something that names a specific revision โ a
* result stamped with the agent version that produced it. Landing on the
* newest version there would answer a question nobody asked, and quietly
* show a configuration that is not the one being investigated.
*/
initialVersion?: number | null;
}
/**
* Full edit-history UI for one resource: a version timeline on the left,
* and a git-style diff of the selected version against its predecessor (or
* against a blank resource, for the first version) on the right.
*/
const ResourceHistoryPanel: React.FC<ResourceHistoryPanelProps> = ({
fetchHistory,
fetchDiff,
historyQueryKey,
initialVersion,
fieldLabels,
hiddenFields,
}) => {
const { data: history, isLoading } = useQuery({
queryKey: historyQueryKey,
queryFn: fetchHistory,
});
const versions = useMemo(() => history?.versions ?? [], [history]);
const [selectedVersion, setSelectedVersion] = useState<number | null>(null);
// `initialVersion` only seeds the *initial* view: once a reader picks a
// version the panel follows them, so a deep link does not fight the
// timeline they are clicking through.
const activeVersion =
selectedVersion ?? initialVersion ?? versions[0]?.version_number ?? null;
const activeIndex = versions.findIndex(
(v) => v.version_number === activeVersion,
);
const previousVersion =
activeIndex >= 0 && activeIndex < versions.length - 1
? versions[activeIndex + 1].version_number
: null;
const { data: diff, isLoading: diffLoading } = useQuery({
queryKey: [...historyQueryKey, "diff", activeVersion, previousVersion],
queryFn: () => fetchDiff(activeVersion as number, previousVersion),
enabled: activeVersion != null,
});
if (isLoading) {
return (
<div className="animate-pulse space-y-2">
<div className="h-10 rounded-lg bg-gray-100 dark:bg-gray-800" />
<div className="h-10 rounded-lg bg-gray-100 dark:bg-gray-800" />
</div>
);
}
if (versions.length === 0) {
return (
<p className="text-sm text-gray-400 dark:text-gray-500 italic flex items-center gap-1.5">
<History className="size-3.5" />
No edit history yet.
</p>
);
}
return (
<div className="grid gap-4 sm:grid-cols-[220px_1fr]">
<div className="space-y-1 sm:border-r sm:border-gray-100 sm:dark:border-gray-700 sm:pr-3 max-h-80 overflow-y-auto">
{versions.map((v) => (
<VersionRow
key={v.id}
version={v}
isBaseline={v.version_number === 1}
selected={v.version_number === activeVersion}
onClick={() => setSelectedVersion(v.version_number)}
/>
))}
</div>
<div>
{diffLoading ? (
<div className="animate-pulse h-20 rounded-lg bg-gray-100 dark:bg-gray-800" />
) : diff ? (
<>
<DiffViewer
fields={diff.fields}
fieldLabels={fieldLabels}
hiddenFields={hiddenFields}
emptyMessage={
previousVersion
? "No field changes in this version."
: "No changes."
}
/>
{Object.keys(diff.children_diff).length > 0 && (
<div className="mt-3 space-y-1.5">
{Object.entries(diff.children_diff).map(([key, entry]) => (
<div key={key} className="text-xs">
<span className="font-semibold text-gray-600 dark:text-gray-400 capitalize">
{key}:
</span>{" "}
{entry.added.length > 0 && (
<span className="text-green-600 dark:text-green-400">
+{entry.added.length} added{" "}
</span>
)}
{entry.removed.length > 0 && (
<span className="text-red-600 dark:text-red-400">
-{entry.removed.length} removed
</span>
)}
</div>
))}
</div>
)}
</>
) : (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
Select a version to view its changes.
</p>
)}
</div>
</div>
);
};
export default ResourceHistoryPanel;
|