-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): in-app editor preview only call when tab is opened
- Loading branch information
Showing
5 changed files
with
106 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
apps/dashboard/src/components/workflow-editor/steps/use-debounced-preview.tsx
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
apps/dashboard/src/components/workflow-editor/steps/use-editor-preview.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import { usePreviewStep } from '@/hooks'; | ||
import { NovuApiError } from '@/api/api.client'; | ||
import { ToastIcon } from '@/components/primitives/sonner'; | ||
import { showToast } from '@/components/primitives/sonner-helpers'; | ||
import { useDataRef } from '@/hooks/use-data-ref'; | ||
|
||
export const useEditorPreview = ({ | ||
workflowSlug, | ||
stepSlug, | ||
stepName, | ||
controlValues, | ||
}: { | ||
workflowSlug: string; | ||
stepSlug: string; | ||
stepName: string; | ||
controlValues: Record<string, unknown>; | ||
}) => { | ||
const [editorValue, setEditorValue] = useState('{}'); | ||
const { | ||
previewStep, | ||
data: previewData, | ||
isPending: isPreviewPending, | ||
} = usePreviewStep({ | ||
onSuccess: (res) => { | ||
setEditorValue(JSON.stringify(res.previewPayloadExample, null, 2)); | ||
}, | ||
onError: (error) => { | ||
if (error instanceof NovuApiError) { | ||
showToast({ | ||
children: () => ( | ||
<> | ||
<ToastIcon variant="error" /> | ||
<span className="text-sm"> | ||
Failed to preview step <span className="font-bold">{stepName}</span> with error: {error.message} | ||
</span> | ||
</> | ||
), | ||
options: { | ||
position: 'bottom-right', | ||
classNames: { | ||
toast: 'ml-10 mb-4', | ||
}, | ||
}, | ||
}); | ||
} | ||
}, | ||
}); | ||
const dataRef = useDataRef({ | ||
workflowSlug, | ||
stepSlug, | ||
controlValues, | ||
editorValue, | ||
}); | ||
|
||
useEffect(() => { | ||
previewStep({ | ||
workflowSlug: dataRef.current.workflowSlug, | ||
stepSlug: dataRef.current.stepSlug, | ||
data: { controlValues: dataRef.current.controlValues, previewPayload: JSON.parse(dataRef.current.editorValue) }, | ||
}); | ||
}, [dataRef, previewStep]); | ||
|
||
const previewStepCallback = useCallback(() => { | ||
return previewStep({ | ||
workflowSlug, | ||
stepSlug, | ||
data: { controlValues, previewPayload: JSON.parse(editorValue) }, | ||
}); | ||
}, [workflowSlug, stepSlug, controlValues, editorValue, previewStep]); | ||
|
||
return { editorValue, setEditorValue, previewStep: previewStepCallback, previewData, isPreviewPending }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters