Skip to content

Commit cac5315

Browse files
committed
ComponentSpecProvider Computes Digest
1 parent 304f7b9 commit cac5315

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/components/Editor/PipelineDetails.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const PipelineDetails = () => {
2828
const {
2929
componentSpec,
3030
graphSpec,
31+
digest,
3132
isComponentTreeValid,
3233
globalValidationIssues,
3334
} = useComponentSpec();
@@ -54,7 +55,6 @@ const PipelineDetails = () => {
5455
creationTime?: Date;
5556
modificationTime?: Date;
5657
createdBy?: string;
57-
digest?: string;
5858
}>({});
5959

6060
// Fetch file metadata on mount or when componentSpec.name changes
@@ -72,7 +72,6 @@ const PipelineDetails = () => {
7272
createdBy: file.componentRef.spec.metadata?.annotations?.author as
7373
| string
7474
| undefined,
75-
digest: file.componentRef.digest,
7675
});
7776
}
7877
};
@@ -109,6 +108,11 @@ const PipelineDetails = () => {
109108
notify("Input value copied to clipboard", "success");
110109
};
111110

111+
const handleDigestCopy = () => {
112+
navigator.clipboard.writeText(digest);
113+
notify("Digest copied to clipboard", "success");
114+
};
115+
112116
if (!componentSpec) {
113117
return (
114118
<div className="flex flex-col gap-8 items-center justify-center h-full">
@@ -181,21 +185,16 @@ const PipelineDetails = () => {
181185
)}
182186

183187
{/* Component Digest */}
184-
{fileMeta.digest && (
188+
{digest && (
185189
<div className="mb-2">
186190
<h3 className="text-md font-medium mb-1">Digest</h3>
187191
<Button
188192
className="bg-gray-100 border border-gray-300 rounded p-2 h-fit text-xs w-full text-left hover:bg-gray-200 active:bg-gray-300 transition cursor-pointer"
189-
onClick={() => {
190-
if (fileMeta.digest) {
191-
navigator.clipboard.writeText(fileMeta.digest);
192-
notify("Digest copied to clipboard", "success");
193-
}
194-
}}
193+
onClick={handleDigestCopy}
195194
variant="ghost"
196195
>
197196
<span className="font-mono break-all w-full text-wrap">
198-
{fileMeta.digest}
197+
{digest}
199198
</span>
200199
</Button>
201200
</div>

src/providers/ComponentSpecProvider.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import equal from "fast-deep-equal";
2-
import { type ReactNode, useCallback, useMemo, useRef, useState } from "react";
2+
import {
3+
type ReactNode,
4+
useCallback,
5+
useEffect,
6+
useMemo,
7+
useRef,
8+
useState,
9+
} from "react";
310

411
import { type UndoRedo, useUndoRedo } from "@/hooks/useUndoRedo";
512
import { loadPipelineByName } from "@/services/pipelineService";
@@ -28,6 +35,7 @@ import {
2835
import {
2936
type ComponentReferenceWithSpec,
3037
componentSpecToYaml,
38+
generateDigest,
3139
writeComponentToFileListFromText,
3240
} from "../utils/componentStore";
3341

@@ -47,6 +55,7 @@ interface ComponentSpecContextType {
4755
graphSpec: GraphSpec;
4856
currentGraphSpec: GraphSpec;
4957
currentSubgraphSpec: ComponentSpec;
58+
digest: string;
5059
isLoading: boolean;
5160
isValid: boolean;
5261
errors: ValidationError[];
@@ -80,6 +89,7 @@ export const ComponentSpecProvider = ({
8089
const [componentSpec, setComponentSpec] = useState<ComponentSpec>(
8190
spec ?? EMPTY_GRAPH_COMPONENT_SPEC,
8291
);
92+
const [digest, setDigest] = useState<string>("");
8393

8494
const [isLoading, setIsLoading] = useState(!!spec);
8595

@@ -118,6 +128,24 @@ export const ComponentSpecProvider = ({
118128
);
119129
const isComponentTreeValid = globalValidationIssues.length === 0;
120130

131+
useEffect(() => {
132+
let isCancelled = false;
133+
134+
const computeDigest = async () => {
135+
const text = componentSpecToYaml(componentSpec);
136+
const newDigest = await generateDigest(text);
137+
if (!isCancelled) {
138+
setDigest(newDigest);
139+
}
140+
};
141+
142+
computeDigest();
143+
144+
return () => {
145+
isCancelled = true;
146+
};
147+
}, [componentSpec]);
148+
121149
const clearComponentSpec = useCallback(() => {
122150
setComponentSpec(EMPTY_GRAPH_COMPONENT_SPEC);
123151
setIsLoading(false);
@@ -239,6 +267,7 @@ export const ComponentSpecProvider = ({
239267
graphSpec,
240268
currentGraphSpec,
241269
currentSubgraphSpec,
270+
digest,
242271
isLoading,
243272
isValid,
244273
errors,
@@ -262,6 +291,7 @@ export const ComponentSpecProvider = ({
262291
graphSpec,
263292
currentGraphSpec,
264293
currentSubgraphSpec,
294+
digest,
265295
isLoading,
266296
isValid,
267297
errors,

0 commit comments

Comments
 (0)