Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: Migrate profile information to use `react-hook-form` (#19675)

Ivan Vasilove5cd2686f7af
diff --git a/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx b/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx
new file mode 100644
index 00000000000..903507db8bc
--- /dev/null
+++ b/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx
@@ -0,0 +1,113 @@
+import { zodResolver } from '@hookform/resolvers/zod'
+import { SubmitHandler, useForm } from 'react-hook-form'
+import {
+ FormControl_Shadcn_,
+ FormField_Shadcn_,
+ FormItem_Shadcn_,
+ FormLabel_Shadcn_,
+ FormMessage_Shadcn_,
+ Form_Shadcn_,
+ Input_Shadcn_,
+} from 'ui'
+import z from 'zod'
+
+import { FormActions } from 'components/ui/Forms'
+import Panel from 'components/ui/Panel'
+import { useProfileUpdateMutation } from 'data/profile/profile-update-mutation'
+import { Profile } from 'data/profile/types'
+import { useStore } from 'hooks'
+import { FormSchema } from 'types'
+
+const FormSchema = z.object({
+ first_name: z.string().optional(),
+ last_name: z.string().optional(),
+})
+
+const formId = 'profile-information-form'
+
+export const ProfileInformation = ({ profile }: { profile: Profile }) => {
+ const { ui } = useStore()
+
+ const form = useForm({
+ resolver: zodResolver(FormSchema),
+ defaultValues: { first_name: profile.first_name, last_name: profile.last_name },
+ })
+
+ const { mutateAsync: updateProfile, isLoading } = useProfileUpdateMutation({
+ onSuccess: () => {
+ ui.setNotification({ category: 'success', message: 'Successfully saved profile' })
+ },
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: "Couldn't update profile. Please try again later.",
+ })
+ },
+ })
+
+ const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (data) => {
+ try {
+ await updateProfile({
+ firstName: data.first_name || '',
+ lastName: data.last_name || '',
+ })
+ } finally {
+ }
+ }
+
+ return (
+ <>
+ <Panel
+ className="mb-8"
+ title={<h5>Profile Information</h5>}
+ footer={
+ <FormActions
+ form={formId}
+ isSubmitting={isLoading}
+ hasChanges={form.formState.isDirty}
+ handleReset={() => form.reset()}
+ />
+ }
+ >
+ <Form_Shadcn_ {...form}>
+ <form
+ id={formId}
+ className="space-y-6 w-full px-8 py-8"
+ onSubmit={form.handleSubmit(onSubmit)}
+ >
+ <FormField_Shadcn_
+ control={form.control}
+ name="first_name"
+ render={({ field }) => (
+ <FormItem_Shadcn_ className="grid gap-2 md:grid md:grid-cols-12 space-y-0">
+ <FormLabel_Shadcn_ className="flex flex-col space-y-2 col-span-4 text-sm justify-center text-foreground-light">
+ First name
+ </FormLabel_Shadcn_>
+ <FormControl_Shadcn_ className="col-span-8">
+ <Input_Shadcn_ {...field} className="w-full !mt-0" />
+ </FormControl_Shadcn_>
+ <FormMessage_Shadcn_ />
+ </FormItem_Shadcn_>
+ )}
+ />
+ <FormField_Shadcn_
+ control={form.control}
+ name="last_name"
+ render={({ field }) => (
+ <FormItem_Shadcn_ className="grid gap-2 md:grid md:grid-cols-12 space-y-0">
+ <FormLabel_Shadcn_ className="flex flex-col space-y-2 col-span-4 text-sm justify-center text-foreground-light">
+ Last name
+ </FormLabel_Shadcn_>
+ <FormControl_Shadcn_ className="col-span-8">
+ <Input_Shadcn_ {...field} className="w-full mt-0" />
+ </FormControl_Shadcn_>
+ </FormItem_Shadcn_>
+ )}
+ />
+ </form>
+ </Form_Shadcn_>
+ </Panel>
+ </>
+ )
+}
diff --git a/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx b/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
index a089c18cf60..488d786eb4d 100644
--- a/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/ThemeSettings.tsx
@@ -4,7 +4,7 @@ import { IconMoon, IconSun, Listbox, Theme, themes } from 'ui'
import Panel from 'components/ui/Panel'
const ThemeSettings = () => {
- const { theme, setTheme } = useTheme()
+ const { theme, resolvedTheme, setTheme } = useTheme()
return (
<Panel title={<h5 key="panel-title">Theme</h5>}>
@@ -15,7 +15,7 @@ const ThemeSettings = () => {
descriptionText="Choose a theme preference"
layout="horizontal"
style={{ width: '50%' }}
- icon={theme ? <IconMoon /> : <IconSun />}
+ icon={resolvedTheme === 'dark' ? <IconMoon /> : <IconSun />}
onChange={(value: string) => setTheme(value)}
>
{themes.map((theme: Theme) => (
diff --git a/apps/studio/components/ui/Forms/FormSection.tsx b/apps/studio/components/ui/Forms/FormSection.tsx
index e75ea3cdb2c..831b5da0e6b 100644
--- a/apps/studio/components/ui/Forms/FormSection.tsx
+++ b/apps/studio/components/ui/Forms/FormSection.tsx
@@ -5,7 +5,6 @@ const FormSection = ({
id,
header,
disabled,
- visible = true,
className,
}: {
children: React.ReactNode
@@ -18,7 +17,6 @@ const FormSection = ({
const classes = [
'grid grid-cols-12 gap-6 px-8 py-8',
`${disabled ? ' opacity-30' : ' opacity-100'}`,
- `${visible ? ' block' : ' hidden'}`,
`${className}`,
]
diff --git a/apps/studio/pages/account/me.tsx b/apps/studio/pages/account/me.tsx
index c360da2db8e..f432a1408b7 100644
--- a/apps/studio/pages/account/me.tsx
+++ b/apps/studio/pages/account/me.tsx
@@ -5,13 +5,12 @@ import {
AnalyticsSettings,
ThemeSettings,
} from 'components/interfaces/Account/Preferences'
+import { ProfileInformation } from 'components/interfaces/Account/Preferences/ProfileInformation'
import { AccountLayout } from 'components/layouts'
-import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
import AlertError from 'components/ui/AlertError'
import Panel from 'components/ui/Panel'
import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
-import { useProfileUpdateMutation } from 'data/profile/profile-update-mutation'
-import { useIsFeatureEnabled, useStore } from 'hooks'
+import { useIsFeatureEnabled } from 'hooks'
import { useProfile } from 'lib/profile'
import { NextPageWithLayout } from 'types'
@@ -40,33 +39,9 @@ User.getLayout = (page) => (
export default User
const ProfileCard = observer(() => {
- const { ui } = useStore()
-
const profileUpdateEnabled = useIsFeatureEnabled('profile:update')
const { profile, error, isLoading, isError, isSuccess } = useProfile()
- const { mutateAsync: updateProfile, isLoading: isUpdating } = useProfileUpdateMutation({
- onSuccess: () => {
- ui.setNotification({ category: 'success', message: 'Successfully saved profile' })
- },
- onError: (error) => {
- ui.setNotification({
- error,
- category: 'error',
- message: "Couldn't update profile. Please try again later.",
- })
- },
- })
-
- const updateUser = async (model: any) => {
- try {
- await updateProfile({
- firstName: model.first_name,
- lastName: model.last_name,
- })
- } finally {
- }
- }
return (
<article className="max-w-4xl p-4">
@@ -89,28 +64,7 @@ const ProfileCard = observer(() => {
<section>
<AccountInformation profile={profile} />
</section>
- {profileUpdateEnabled && (
- <section>
- {/* @ts-ignore */}
- <SchemaFormPanel
- title="Profile"
- schema={{
- type: 'object',
- required: [],
- properties: {
- first_name: { type: 'string' },
- last_name: { type: 'string' },
- },
- }}
- model={{
- first_name: profile?.first_name ?? '',
- last_name: profile?.last_name ?? '',
- }}
- onSubmit={updateUser}
- loading={isUpdating}
- />
- </section>
- )}
+ {profileUpdateEnabled && isSuccess ? <ProfileInformation profile={profile!} /> : null}
</>
)}
diff --git a/packages/ui/src/components/Listbox/Listbox2.tsx b/packages/ui/src/components/Listbox/Listbox2.tsx
index ba0f4b7736e..b0b797e1a20 100644
--- a/packages/ui/src/components/Listbox/Listbox2.tsx
+++ b/packages/ui/src/components/Listbox/Listbox2.tsx
@@ -181,10 +181,12 @@ function Listbox({
}
let selectClasses = [__styles.container, __styles.base, buttonClassName]
+ let addonBeforeClasses = [__styles.addOnBefore]
+
if (error) selectClasses.push(__styles.variants.error)
if (!error) selectClasses.push(__styles.variants.standard)
// if (icon) selectClasses.push(SelectStyles['sbui-listbox--with-icon'])
- if (icon) selectClasses.push(__styles.with_icon)
+ if (icon) addonBeforeClasses.push(__styles.with_icon)
// if (size) selectClasses.push(SelectStyles[`sbui-listbox--${size}`])
if (size) selectClasses.push(__styles.size[size])
// if (borderless) selectClasses.push(SelectStyles['sbui-listbox--borderless'])
@@ -212,7 +214,7 @@ function Listbox({
name={name}
id={id}
>
- <span className={__styles.addOnBefore}>
+ <span className={cn(addonBeforeClasses)}>
{icon && <InputIconContainer icon={icon} />}
{selectedNode?.addOnBefore && <selectedNode.addOnBefore />}
<span className={__styles.label}>{selectedNode?.label}</span>
diff --git a/packages/ui/src/lib/theme/defaultTheme.ts b/packages/ui/src/lib/theme/defaultTheme.ts
index 71fd50e4c56..07d90345eae 100644
--- a/packages/ui/src/lib/theme/defaultTheme.ts
+++ b/packages/ui/src/lib/theme/defaultTheme.ts
@@ -1336,7 +1336,7 @@ export default {
data-open:animate-dropdown-content-show
data-closed:animate-dropdown-content-hide
`,
- with_icon: 'pl-10',
+ with_icon: 'pl-2',
addOnBefore: `
w-full flex flex-row items-center space-x-3
`,