Skip to content

Commit 71b05e1

Browse files
committed
ComponentSpecProvider Computes Digest
1 parent 89cf3ca commit 71b05e1

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/components/Editor/PipelineDetails.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { getOutputConnectedDetails } from "./utils/getOutputConnectedDetails";
2525

2626
const PipelineDetails = () => {
2727
const { setContent } = useContextPanel();
28-
const { componentSpec, graphSpec, isValid, errors } = useComponentSpec();
28+
const { componentSpec, graphSpec, digest, isValid, errors } =
29+
useComponentSpec();
2930

3031
const notify = useToastNotification();
3132

@@ -45,7 +46,6 @@ const PipelineDetails = () => {
4546
creationTime?: Date;
4647
modificationTime?: Date;
4748
createdBy?: string;
48-
digest?: string;
4949
}>({});
5050

5151
// Fetch file metadata on mount or when componentSpec.name changes
@@ -63,7 +63,6 @@ const PipelineDetails = () => {
6363
createdBy: file.componentRef.spec.metadata?.annotations?.author as
6464
| string
6565
| undefined,
66-
digest: file.componentRef.digest,
6766
});
6867
}
6968
};
@@ -172,21 +171,19 @@ const PipelineDetails = () => {
172171
)}
173172

174173
{/* Component Digest */}
175-
{fileMeta.digest && (
174+
{digest && (
176175
<div className="mb-2">
177176
<h3 className="text-md font-medium mb-1">Digest</h3>
178177
<Button
179178
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"
180179
onClick={() => {
181-
if (fileMeta.digest) {
182-
navigator.clipboard.writeText(fileMeta.digest);
183-
notify("Digest copied to clipboard", "success");
184-
}
180+
navigator.clipboard.writeText(digest);
181+
notify("Digest copied to clipboard", "success");
185182
}}
186183
variant="ghost"
187184
>
188185
<span className="font-mono break-all w-full text-wrap">
189-
{fileMeta.digest}
186+
{digest}
190187
</span>
191188
</Button>
192189
</div>

src/providers/ComponentSpecProvider.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
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";
12+
import { generateDigest } from "@/services/componentService";
513
import { loadPipelineByName } from "@/services/pipelineService";
614
import { USER_PIPELINES_LIST_NAME } from "@/utils/constants";
715
import { prepareComponentRefForEditor } from "@/utils/prepareComponentRefForEditor";
@@ -42,6 +50,7 @@ interface ComponentSpecContextType {
4250
graphSpec: GraphSpec;
4351
currentGraphSpec: GraphSpec;
4452
currentSubgraphSpec: ComponentSpec;
53+
digest: string;
4554
isLoading: boolean;
4655
isValid: boolean;
4756
errors: string[];
@@ -73,6 +82,7 @@ export const ComponentSpecProvider = ({
7382
const [componentSpec, setComponentSpec] = useState<ComponentSpec>(
7483
spec ?? EMPTY_GRAPH_COMPONENT_SPEC,
7584
);
85+
const [digest, setDigest] = useState<string>("");
7686

7787
const [isLoading, setIsLoading] = useState(!!spec);
7888

@@ -105,6 +115,24 @@ export const ComponentSpecProvider = ({
105115
[currentSubgraphSpec, isRootSubgraph],
106116
);
107117

118+
useEffect(() => {
119+
let isCancelled = false;
120+
121+
const computeDigest = async () => {
122+
const text = componentSpecToYaml(componentSpec);
123+
const newDigest = await generateDigest(text);
124+
if (!isCancelled) {
125+
setDigest(newDigest);
126+
}
127+
};
128+
129+
computeDigest();
130+
131+
return () => {
132+
isCancelled = true;
133+
};
134+
}, [componentSpec]);
135+
108136
const clearComponentSpec = useCallback(() => {
109137
setComponentSpec(EMPTY_GRAPH_COMPONENT_SPEC);
110138
setIsLoading(false);
@@ -226,6 +254,7 @@ export const ComponentSpecProvider = ({
226254
graphSpec,
227255
currentGraphSpec,
228256
currentSubgraphSpec,
257+
digest,
229258
isLoading,
230259
isValid,
231260
errors,
@@ -247,6 +276,7 @@ export const ComponentSpecProvider = ({
247276
graphSpec,
248277
currentGraphSpec,
249278
currentSubgraphSpec,
279+
digest,
250280
isLoading,
251281
isValid,
252282
errors,

0 commit comments

Comments
 (0)