Skip to content

Commit

Permalink
Add type for hook props
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmilburn committed Dec 14, 2024
1 parent c3fd119 commit 42284d0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions admin/src/hooks/usePreviewButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,16 +29,20 @@ const usePreviewButton = (uid: UID.ContentType | undefined, data: any): UsePrevi
const [published, setPublished] = useState<PreviewButtonStateConfig | null>(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(
Expand Down

0 comments on commit 42284d0

Please sign in to comment.