From 42284d046db1134b764bdacb50e8eb019cfc4392 Mon Sep 17 00:00:00 2001 From: Matt Milburn Date: Fri, 13 Dec 2024 12:33:21 -0600 Subject: [PATCH] Add type for hook props --- admin/src/hooks/usePreviewButton.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/admin/src/hooks/usePreviewButton.ts b/admin/src/hooks/usePreviewButton.ts index 44d26af..a5df20c 100644 --- a/admin/src/hooks/usePreviewButton.ts +++ b/admin/src/hooks/usePreviewButton.ts @@ -14,6 +14,13 @@ export interface UsePreviewButtonReturn { published: PreviewButtonStateConfig | null; } +export interface PreviewButtonBeforeBuildUrlProps { + data: any; + draft: PreviewButtonStateConfig | undefined; + published: PreviewButtonStateConfig | undefined; + uid: UID.ContentType; +} + const usePreviewButton = (uid: UID.ContentType | undefined, data: any): UsePreviewButtonReturn => { const runHookWaterfall = useStrapiApp(PLUGIN_ID, (value) => value.runHookWaterfall); const { data: config, isLoading } = usePluginConfig(); @@ -22,16 +29,20 @@ const usePreviewButton = (uid: UID.ContentType | undefined, data: any): UsePrevi const [published, setPublished] = useState(null); const uidConfig = config?.contentTypes?.find((type) => type.uid === uid); - const isSupported = !!uidConfig; + const isSupported = !!uid && !!uidConfig; const compileWithHooks = useCallback(async () => { + if (!isSupported) { + return; + } + // Run async hook then set state. const result = await runHookWaterfall(HOOK_BEFORE_BUILD_URL, { data, - draft: uidConfig?.draft, - published: uidConfig?.published, + draft: uidConfig.draft, + published: uidConfig.published, uid, - }); + } satisfies PreviewButtonBeforeBuildUrlProps); const draftConfig = getPublishStateConfig(result?.draft as PreviewButtonStateConfig, data); const publishedConfig = getPublishStateConfig(