| 1 | "use client"; |
| 2 | |
| 3 | import type { OrganizationAuthClient } from "@better-auth-ui/core/plugins/organization"; |
| 4 | import { useAuth, useAuthPlugin |
| 5 | import { useCheckSlug } |
| 6 | import { useDebouncer } |
| 7 | import { Check, X |
| 8 | import { useEffect, useState |
| 9 | |
| 10 | import { Field, FieldError |
| 11 | import { |
| 12 | InputGroup, |
| 13 | InputGroupAddon, |
| 14 | InputGroupInput, |
| 15 | } from "@/components/ui/input-group"; |
| 16 | import { Spinner } |
| 17 | import { organizationPlugin } |
| 18 | |
| 19 | /** Props for the `SlugField` component. */ |
| 20 | export type SlugFieldProps |
| 21 | value: string |
| 22 | onChange: (value |
| 23 | currentSlug?: string |
| 24 | disabled?: boolean |
| 25 | id?: string |
| 26 | }; |
| 27 | |
| 28 | /** Sanitize a slug value so it only contains lowercase alphanumeric characters and dashes. Runs of disallowed characters are collapsed to a single dash, but leading/trailing dashes are preserved while the user is still typing. */ |
| 29 | export function sanitizeSlug(value |
| 30 | return value.toLowerCase |
| 31 | } |
| 32 | |
| 33 | /** Organization slug field with debounced availability checking. */ |
| 34 | export function SlugField({ |
| 35 | value, |
| 36 | onChange, |
| 37 | currentSlug, |
| 38 | disabled, |
| 39 | id = "slug" |
| 40 | }: SlugFieldProps) |
| 41 | const { authClient, |
| 42 | useAuth<OrganizationAuthClient> |
| 43 | const { |
| 44 | localization, |
| 45 | checkSlug: checkSlugEnabled, |
| 46 | slugPrefix, |
| 47 | } = useAuthPlugin(organizationPlugin |
| 48 | |
| 49 | const [slugError, |
| 50 | |
| 51 | const { |
| 52 | mutate: checkSlug, |
| 53 | data: checkSlugData, |
| 54 | error: checkSlugError, |
| 55 | reset: resetCheckSlug, |
| 56 | } = useCheckSlug(authClient |
| 57 | |
| 58 | const debouncer = |
| 59 | (next: string |
| 60 | if (!checkSlugEnabled || |
| 61 | return; |
| 62 | |
| 63 | checkSlug({ slug: |
| 64 | }, |
| 65 | { wait: 500 }, |
| 66 | ); |
| 67 | |
| 68 | useEffect(() => |
| 69 | // Clear stale validation errors when the controlled value changes externally (e.g. the parent resets the form), not just via this input's onChange. |
| 70 | setSlugError(undefined); |
| 71 | |
| 72 | if (!checkSlugEnabled) |
| 73 | |
| 74 | resetCheckSlug(); |
| 75 | debouncer.maybeExecute(value |
| 76 | }, [checkSlugEnabled, |
| 77 | |
| 78 | return ( |
| 79 | <Field data-invalid={ |
| 80 | <FieldLabel htmlFor={ |
| 81 | |
| 82 | <InputGroup> |
| 83 | {slugPrefix && ( |
| 84 | <InputGroupAddon align=" |
| 85 | )} |
| 86 | |
| 87 | <InputGroupInput |
| 88 | id={id} |
| 89 | name="slug" |
| 90 | value={value} |
| 91 | onChange={(e) |
| 92 | onChange(sanitizeSlug(e |
| 93 | setSlugError(undefined); |
| 94 | }} |
| 95 | onInvalid={(e) |
| 96 | e.preventDefault(); |
| 97 | setSlugError(authLocalization.auth |
| 98 | }} |
| 99 | aria-invalid={!!slugError} |
| 100 | placeholder={localization.slugPlaceholder |
| 101 | required |
| 102 | disabled={disabled} |
| 103 | /> |
| 104 | |
| 105 | {checkSlugEnabled && !!value |
| 106 | <InputGroupAddon align=" |
| 107 | {checkSlugData?.status ? |
| 108 | <Check className=" |
| 109 | ) : checkSlugError ? |
| 110 | <X className=" |
| 111 | ) : ( |
| 112 | <Spinner /> |
| 113 | )} |
| 114 | </InputGroupAddon> |
| 115 | )} |
| 116 | </InputGroup> |
| 117 | |
| 118 | <FieldError>{slugError} |
| 119 | </Field> |
| 120 | ); |
| 121 | } |