From 933f15aa2e71c7c352089b6921d43fb546f3b0c6 Mon Sep 17 00:00:00 2001 From: leon-schmid Date: Wed, 23 Oct 2024 17:09:35 +0200 Subject: [PATCH] feat: add dropdown to select stored secrets or redirect to set one to action node --- .../edit-panel/action-edit-panel.tsx | 61 ++++++++++++++++--- web/src/providers/save-workflow.tsx | 8 ++- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/web/src/components/workflow-editor/edit-panel/action-edit-panel.tsx b/web/src/components/workflow-editor/edit-panel/action-edit-panel.tsx index f6d3f1db..c9207dfd 100644 --- a/web/src/components/workflow-editor/edit-panel/action-edit-panel.tsx +++ b/web/src/components/workflow-editor/edit-panel/action-edit-panel.tsx @@ -1,7 +1,8 @@ -import { Code, Flex, Text, TextField } from "@radix-ui/themes"; +import { Code, Flex, Text, TextField, Select } from "@radix-ui/themes"; import ActionIcon from "../action-icon"; import WorkflowEditorRightPanelBase from "../right-panel-base"; import { useWorkflowStore } from "@/stores/workflow-store"; +import { useSecretsStore } from "@/stores/secrets-store"; import { useEditorActionStore } from "@/stores/editor-action-store"; import { TEditorWorkflowActionNode } from "@/types/react-flow"; import { produce } from "immer"; @@ -9,6 +10,8 @@ import { ChangeEvent, useEffect } from "react"; import { useImmer } from "use-immer"; import { TActionMetadata } from "@/types/editor-actions"; import CodeEditorWithDialog from "@/components/code-editor-with-dialog/code-editor-with-dialog"; +import useSaveWorkflow from "@/providers/save-workflow"; +import { useRouter } from "next/navigation"; function buildInitialArgs( action: TEditorWorkflowActionNode, @@ -24,8 +27,11 @@ function buildInitialArgs( } export default function ActionEditPanel() { + const router = useRouter(); const { nodes, selectedNodeIdx, updateNodeData } = useWorkflowStore(); const { actionsIndex } = useEditorActionStore(); + const { secrets } = useSecretsStore(); + const { saveWorkflow } = useSaveWorkflow(); const [args, updateArgs] = useImmer([]); @@ -55,14 +61,26 @@ export default function ActionEditPanel() { ); }; + const saveWorkflowAndRedirect = async () => { + const saveSuccessful = await saveWorkflow(); + if (saveSuccessful) { + router.push("/settings"); + } + }; + const onChangeSecretsMapping = ( secretPlaceholder: string, - event: ChangeEvent, + value: string, ) => { + if (value === "$$$save_and_redirect$$$") { + saveWorkflowAndRedirect(); + return; + } + updateNodeData( selectedNodeIdx, produce(actionData, (draft) => { - draft.secretsMapping[secretPlaceholder] = event.target.value; + draft.secretsMapping[secretPlaceholder] = value; }), ); }; @@ -134,20 +152,47 @@ export default function ActionEditPanel() { {secretPlaceholder} - + onValueChange={(value) => onChangeSecretsMapping( secretPlaceholder, - event, + value, ) } - /> + > + + + + + Select a secret + + {secrets.map((_, idx) => ( + + {secrets[idx].secretId} + + ))} + + + Save Workflow and Redirect + to Secrets + + + + ), )} diff --git a/web/src/providers/save-workflow.tsx b/web/src/providers/save-workflow.tsx index 8aa275da..44c04770 100644 --- a/web/src/providers/save-workflow.tsx +++ b/web/src/providers/save-workflow.tsx @@ -17,12 +17,12 @@ class WorkflowValidationError extends Error { const SPACE = " "; interface SaveWorkflowContextType { - saveWorkflow: () => Promise; + saveWorkflow: () => Promise; isPending: boolean; } const SaveWorkflowContext = createContext({ - saveWorkflow: async () => {}, + saveWorkflow: async () => false, isPending: false, }); @@ -39,7 +39,7 @@ export function SaveWorkflowProvider({ const handleSaveWorkflow = async () => { if (isPending) { - return; + return false; } setIsPending(true); @@ -100,6 +100,7 @@ export function SaveWorkflowProvider({ if (webhookId && webhookSecret) { updateWebhookIdAndSecret(webhookId, webhookSecret); } + return true; } catch (error) { if ( error instanceof AxiosError && @@ -113,6 +114,7 @@ export function SaveWorkflowProvider({ } else { errorToast("Failed to save workflow. Please try again."); } + return false; } finally { setIsPending(false); }