| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { zodResolver } from '@hookform/resolvers/zod'
- import { PermissionAction } from '@supabase/shared-types/out/constants'
- import { useParams } from 'common'
- import { useEffect, useState } from 'react'
- import { useForm } from 'react-hook-form'
- import { toast } from 'sonner'
- import { Button, Card, CardContent, CardFooter, Form, FormControl, FormField, Input } from 'ui'
- import { GenericSkeletonLoader } from 'ui-patterns'
- import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
- import {
- PageSection,
- PageSectionContent,
- PageSectionMeta,
- PageSectionSummary,
- PageSectionTitle,
- } from 'ui-patterns/PageSection'
- import * as z from 'zod'
- import AlertError from '@/components/ui/AlertError'
- import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
- import { useAuthConfigUpdateMutation } from '@/data/auth/auth-config-update-mutation'
- import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
- const schema = z.object({
- SITE_URL: z.string().min(1, 'Must have a Site URL'),
- })
- const SiteUrl = () => {
- const { ref: projectRef } = useParams()
- const {
- data: authConfig,
- error: authConfigError,
- isError,
- isPending: isLoading,
- } = useAuthConfigQuery({ projectRef })
- const { mutate: updateAuthConfig } = useAuthConfigUpdateMutation()
- const [isUpdatingSiteUrl, setIsUpdatingSiteUrl] = useState(false)
- const { can: canUpdateConfig } = useAsyncCheckPermissions(
- PermissionAction.UPDATE,
- 'custom_config_gotrue'
- )
- const siteUrlForm = useForm({
- resolver: zodResolver(schema as any),
- defaultValues: {
- SITE_URL: '',
- },
- })
- const { isDirty } = siteUrlForm.formState
- useEffect(() => {
- if (authConfig && !isUpdatingSiteUrl) {
- siteUrlForm.reset({
- SITE_URL: authConfig.SITE_URL || '',
- })
- }
- }, [authConfig, isUpdatingSiteUrl])
- const onSubmitSiteUrl = (values: any) => {
- setIsUpdatingSiteUrl(true)
- updateAuthConfig(
- { projectRef: projectRef!, config: values },
- {
- onError: (error) => {
- toast.error(`Failed to update site URL: ${error?.message}`)
- setIsUpdatingSiteUrl(false)
- },
- onSuccess: () => {
- toast.success('Successfully updated site URL')
- setIsUpdatingSiteUrl(false)
- },
- }
- )
- }
- if (isError) {
- return (
- <PageSection>
- <PageSectionContent>
- <AlertError error={authConfigError} subject="Failed to retrieve auth configuration" />
- </PageSectionContent>
- </PageSection>
- )
- }
- if (isLoading) {
- return (
- <PageSection>
- <PageSectionContent>
- <GenericSkeletonLoader />
- </PageSectionContent>
- </PageSection>
- )
- }
- return (
- <PageSection>
- <PageSectionMeta>
- <PageSectionSummary>
- <PageSectionTitle>Site URL</PageSectionTitle>
- </PageSectionSummary>
- </PageSectionMeta>
- <PageSectionContent>
- <Form {...siteUrlForm}>
- <form onSubmit={siteUrlForm.handleSubmit(onSubmitSiteUrl)}>
- <Card>
- <CardContent>
- <FormField
- control={siteUrlForm.control}
- name="SITE_URL"
- render={({ field }) => (
- <FormItemLayout
- layout="flex-row-reverse"
- label="Site URL"
- description="Configure the default redirect URL used when a redirect URL is not specified or doesn't match one from the allow list. This value is also exposed as a template variable in the email templates section. Wildcards cannot be used here."
- >
- <FormControl>
- <Input {...field} disabled={!canUpdateConfig} />
- </FormControl>
- </FormItemLayout>
- )}
- />
- </CardContent>
- <CardFooter className="justify-end space-x-2">
- {isDirty && (
- <Button type="default" onClick={() => siteUrlForm.reset()}>
- Cancel
- </Button>
- )}
- <Button
- type="primary"
- htmlType="submit"
- disabled={!canUpdateConfig || isUpdatingSiteUrl || !isDirty}
- loading={isUpdatingSiteUrl}
- >
- Save changes
- </Button>
- </CardFooter>
- </Card>
- </form>
- </Form>
- </PageSectionContent>
- </PageSection>
- )
- }
- export default SiteUrl
|