mirror of
https://github.com/supabase/supabase.git
synced 2026-06-20 03:27:24 +08:00
## Problem Our `<Button>` component breaks the default `button` contract by redefining the `type` prop to set its variant (`primary`, `default`, etc) instead of the button type (`submit`, `button`, etc). This is confusing and forces to write more code when using it with shadcn components that expect/inject the standard button props. ## Solution - rename the `type` prop to `variant` - rename the `htmlType` prop to `type` - propagate the changes where necessary - format code ## How to test As this is just prop renaming, if it builds it's ok --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
151 lines
4.5 KiB
TypeScript
151 lines
4.5 KiB
TypeScript
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),
|
|
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 variant="default" onClick={() => siteUrlForm.reset()}>
|
|
Cancel
|
|
</Button>
|
|
)}
|
|
<Button
|
|
variant="primary"
|
|
type="submit"
|
|
disabled={!canUpdateConfig || isUpdatingSiteUrl || !isDirty}
|
|
loading={isUpdatingSiteUrl}
|
|
>
|
|
Save changes
|
|
</Button>
|
|
</CardFooter>
|
|
</Card>
|
|
</form>
|
|
</Form>
|
|
</PageSectionContent>
|
|
</PageSection>
|
|
)
|
|
}
|
|
|
|
export default SiteUrl
|