Files
formbricks/apps/web/modules/survey/hooks/useSingleUseId.tsx
T
2025-11-19 06:36:07 +00:00

38 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useState } from "react";
import toast from "react-hot-toast";
import type { TSurvey } from "@formbricks/types/surveys/types";
import { getFormattedErrorMessage } from "@/lib/utils/helper";
import { generateSingleUseIdsAction } from "@/modules/survey/list/actions";
import type { TSurvey as TSurveyList } from "@/modules/survey/list/types/surveys";
export const useSingleUseId = (survey: TSurvey | TSurveyList, isReadOnly: boolean) => {
const [singleUseId, setSingleUseId] = useState<string>();
const refreshSingleUseId = useCallback(async (): Promise<string | undefined> => {
if (isReadOnly || !survey.singleUse?.enabled) {
// If readonly or singleUse disabled, just clear and bail out
setSingleUseId(undefined);
return undefined;
}
const response = await generateSingleUseIdsAction({
surveyId: survey.id,
isEncrypted: Boolean(survey.singleUse?.isEncrypted),
count: 1,
});
if (response?.data?.length) {
setSingleUseId(response.data[0]);
return response.data[0];
} else {
toast.error(getFormattedErrorMessage(response));
return undefined;
}
}, [survey, isReadOnly]);
return {
singleUseId: isReadOnly ? undefined : singleUseId,
refreshSingleUseId: isReadOnly ? async () => undefined : refreshSingleUseId,
};
};