|
| 1 | +import { eventMethodCalled } from '../../telemetry/events/method-called'; |
| 2 | +import type { EnvironmentResource } from '../../types/environment'; |
| 3 | +import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data'; |
| 4 | +import { useClerkQuery } from '../clerk-rq/useQuery'; |
| 5 | +import { useAssertWrappedByClerkProvider, useClerkInstanceContext, useUserContext } from '../contexts'; |
| 6 | +import { useOrganizationCreationDefaultsCacheKeys } from './useOrganizationCreationDefaults.shared'; |
| 7 | +import type { |
| 8 | + UseOrganizationCreationDefaultsParams, |
| 9 | + UseOrganizationCreationDefaultsReturn, |
| 10 | +} from './useOrganizationCreationDefaults.types'; |
| 11 | + |
| 12 | +const HOOK_NAME = 'useOrganizationCreationDefaults'; |
| 13 | + |
| 14 | +/** |
| 15 | + * The `useOrganizationCreationDefaults()` hook retrieves the organization creation defaults for the current user. |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ### Basic usage |
| 19 | + * |
| 20 | + * ```tsx |
| 21 | + * import { useOrganizationCreationDefaults } from '@clerk/clerk-react' |
| 22 | + * |
| 23 | + * export default function CreateOrganizationForm() { |
| 24 | + * const { data, isLoading } = useOrganizationCreationDefaults() |
| 25 | + * |
| 26 | + * if (isLoading) return <div>Loading...</div> |
| 27 | + * |
| 28 | + * return ( |
| 29 | + * <form> |
| 30 | + * <input defaultValue={data?.form.name} placeholder="Organization name" /> |
| 31 | + * <input defaultValue={data?.form.slug} placeholder="Slug" /> |
| 32 | + * <button type="submit">Create</button> |
| 33 | + * </form> |
| 34 | + * ) |
| 35 | + * } |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +export function useOrganizationCreationDefaults( |
| 39 | + params: UseOrganizationCreationDefaultsParams = {}, |
| 40 | +): UseOrganizationCreationDefaultsReturn { |
| 41 | + useAssertWrappedByClerkProvider(HOOK_NAME); |
| 42 | + |
| 43 | + const { keepPreviousData = true, enabled = true } = params; |
| 44 | + const clerk = useClerkInstanceContext(); |
| 45 | + const user = useUserContext(); |
| 46 | + |
| 47 | + // @ts-expect-error `__unstable__environment` is not typed |
| 48 | + const environment = clerk.__unstable__environment as unknown as EnvironmentResource | null | undefined; |
| 49 | + const featureEnabled = environment?.organizationSettings?.organizationCreationDefaults?.enabled ?? false; |
| 50 | + |
| 51 | + clerk.telemetry?.record(eventMethodCalled(HOOK_NAME)); |
| 52 | + |
| 53 | + const { queryKey } = useOrganizationCreationDefaultsCacheKeys({ userId: user?.id ?? null }); |
| 54 | + |
| 55 | + const queryEnabled = Boolean(user) && enabled && featureEnabled && clerk.loaded; |
| 56 | + |
| 57 | + const query = useClerkQuery({ |
| 58 | + queryKey, |
| 59 | + queryFn: user?.getOrganizationCreationDefaults, |
| 60 | + enabled: queryEnabled, |
| 61 | + placeholderData: defineKeepPreviousDataFn(keepPreviousData), |
| 62 | + }); |
| 63 | + |
| 64 | + return { |
| 65 | + data: query.data, |
| 66 | + error: (query.error ?? null) as UseOrganizationCreationDefaultsReturn['error'], |
| 67 | + isLoading: query.isLoading, |
| 68 | + isFetching: query.isFetching, |
| 69 | + }; |
| 70 | +} |
0 commit comments