Skip to content

Commit afafd58

Browse files
committed
ComponentSpecProvider Computes Digest
1 parent 96dcbf7 commit afafd58

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/components/Editor/PipelineDetails.tsx

Lines changed: 6 additions & 5 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

@@ -172,21 +173,21 @@ const PipelineDetails = () => {
172173
)}
173174

174175
{/* Component Digest */}
175-
{fileMeta.digest && (
176+
{digest && (
176177
<div className="mb-2">
177178
<h3 className="text-md font-medium mb-1">Digest</h3>
178179
<Button
179180
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"
180181
onClick={() => {
181-
if (fileMeta.digest) {
182-
navigator.clipboard.writeText(fileMeta.digest);
182+
if (digest) {
183+
navigator.clipboard.writeText(digest);
183184
notify("Digest copied to clipboard", "success");
184185
}
185186
}}
186187
variant="ghost"
187188
>
188189
<span className="font-mono break-all w-full text-wrap">
189-
{fileMeta.digest}
190+
{digest}
190191
</span>
191192
</Button>
192193
</div>

src/providers/ComponentSpecProvider.tsx

Lines changed: 23 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,16 @@ export const ComponentSpecProvider = ({
105115
[currentSubgraphSpec, isRootSubgraph],
106116
);
107117

118+
useEffect(() => {
119+
const computeDigest = async () => {
120+
const text = componentSpecToYaml(componentSpec);
121+
const newDigest = await generateDigest(text);
122+
setDigest(newDigest);
123+
};
124+
125+
computeDigest();
126+
}, [componentSpec]);
127+
108128
const clearComponentSpec = useCallback(() => {
109129
setComponentSpec(EMPTY_GRAPH_COMPONENT_SPEC);
110130
setIsLoading(false);
@@ -226,6 +246,7 @@ export const ComponentSpecProvider = ({
226246
graphSpec,
227247
currentGraphSpec,
228248
currentSubgraphSpec,
249+
digest,
229250
isLoading,
230251
isValid,
231252
errors,
@@ -247,6 +268,7 @@ export const ComponentSpecProvider = ({
247268
graphSpec,
248269
currentGraphSpec,
249270
currentSubgraphSpec,
271+
digest,
250272
isLoading,
251273
isValid,
252274
errors,

0 commit comments

Comments
 (0)