Files
supabase/apps/studio/components/interfaces/Organization/GeneralSettings/OrganizationDetailsForm.tsx
Joshen Lim 42b6571e7d Chore/housekeeping org pages (#53303)
## Context

Some minor UI issues that I found while reviewing another PR

- Fix `NoProjectsOnPaidOrgInfo` prematurely showing while project
details is still loading
<img width="1249" height="376" alt="image"
src="https://github.com/user-attachments/assets/3f41f4a3-849f-4685-bcc4-6fed3b433042"
/>
- Fix padding alignment for Organization details form group
<img width="1115" height="282" alt="image"
src="https://github.com/user-attachments/assets/c0865614-208c-4335-87d3-176c4411b6a4"
/>
- Fix projects list on `/org/[slug]` showing stale projects while
switching orgs - just removed `keepPreviousData` so that the card
loaders shows up
- Fix organization settings in sidebar menu not showing active state
while on certain settings pages like audit-log-drains, webhooks, etc
2026-11-13 10:16:48 +07:00

110 lines
4.1 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useQueryClient } from '@tanstack/react-query'
import { useParams } from 'common'
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import { Card, CardContent, CardFooter, Form, FormControl, FormField, Input } from 'ui'
import { Input as PasswordInput } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import * as z from 'zod'
import { FormActions } from '@/components/ui/Forms/FormActions'
import { useOrganizationUpdateMutation } from '@/data/organizations/organization-update-mutation'
import { invalidateOrganizationsQuery } from '@/data/organizations/organizations-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import type { StudioError } from '@/lib/errors/studio-errors'
const OrgDetailsSchema = z.object({
name: z.string().min(1, 'Organization name is required'),
})
export const OrganizationDetailsForm = () => {
const { slug } = useParams()
const queryClient = useQueryClient()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const { can: canUpdateOrganization } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.ORGANIZATION.ADMIN_WRITE
)
const { mutate: updateOrganization, isPending: isUpdatingDetails } =
useOrganizationUpdateMutation()
const orgDetailsForm = useForm<z.infer<typeof OrgDetailsSchema>>({
resolver: zodResolver(OrgDetailsSchema),
defaultValues: { name: selectedOrganization?.name ?? '' },
})
const onUpdateOrganizationDetails = async (values: z.infer<typeof OrgDetailsSchema>) => {
if (!canUpdateOrganization) {
return toast.error('You do not have the required permissions to update this organization')
}
if (!slug) return console.error('Slug is required')
updateOrganization(
{ slug, name: values.name },
{
onSuccess: () => {
invalidateOrganizationsQuery(queryClient)
toast.success('Successfully updated organization name')
},
onError: (error: StudioError) => {
toast.error(`Failed to update organization name: ${error.message}`)
},
}
)
}
const permissionsHelperText = !canUpdateOrganization
? "You need additional permissions to manage this organization's settings"
: undefined
useEffect(() => {
if (selectedOrganization && !isUpdatingDetails) {
orgDetailsForm.reset({ name: selectedOrganization.name ?? '' })
}
}, [selectedOrganization, orgDetailsForm, isUpdatingDetails])
return (
<Form {...orgDetailsForm}>
<form
id="org-details-form"
onSubmit={orgDetailsForm.handleSubmit(onUpdateOrganizationDetails)}
>
<Card>
<CardContent className="md:px-8">
<FormField
control={orgDetailsForm.control}
name="name"
render={({ field }) => (
<FormItemLayout label="Organization name" layout="flex-row-reverse">
<FormControl>
<Input {...field} disabled={!canUpdateOrganization || isUpdatingDetails} />
</FormControl>
</FormItemLayout>
)}
/>
</CardContent>
<CardContent className="md:px-8">
<FormItemLayout label="Organization slug" layout="flex-row-reverse">
<PasswordInput copy disabled id="slug" value={selectedOrganization?.slug ?? ''} />
</FormItemLayout>
</CardContent>
<CardFooter className="flex justify-end p-4 md:px-8">
<FormActions
form="org-details-form"
isSubmitting={isUpdatingDetails}
hasChanges={orgDetailsForm.formState.isDirty}
handleReset={() => orgDetailsForm.reset()}
helper={permissionsHelperText}
disabled={!canUpdateOrganization}
/>
</CardFooter>
</Card>
</form>
</Form>
)
}