Files
supabase/apps/studio/components/interfaces/Database/Roles/RolePasswordField.tsx
Joshen Lim 6d08d66b7d Add eslint rule for google translate dynamic spans + address all issues (#52949)
## Context

We've been seeing many `NotFoundError: Failed to execute 'removeChild'
on 'Node'` and `NotFoundError: Failed to execute 'insertBefore' on
'Node'` events on Sentry which are historically known to be caused by
Google Translate in the browser.

There's technically no _right way_ to address this unfortunately, but
one of the solutions which should address most of these issues is to
wrap any dynamic text within a span - we've considered that quite a
while back tbh but never took the time to do this until today with the
help of Claude. Although just a heads up that it might not necessarily
completely eradicate those issues.

Also added an eslint rule which should prevent future occurences of this
<img width="1264" height="192" alt="image"
src="https://github.com/user-attachments/assets/b2c32ea0-45a2-4ccd-82fe-1a859905fc29"
/>
2026-11-05 23:28:46 +08:00

82 lines
2.6 KiB
TypeScript

import { useState } from 'react'
import { useFormContext } from 'react-hook-form'
import { FormControl, FormField } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { InlineLinkClassName } from '@/components/ui/InlineLink'
import { generateStrongPassword } from '@/lib/project'
interface RolePasswordFieldProps {
/** Whether leaving the field empty is allowed. Defaults to true. */
isOptional?: boolean
/** Whether the role already exists, so it may already have a password. Defaults to false. */
isExistingRole?: boolean
layout?: 'horizontal' | 'vertical'
}
/**
* Password field for a role form. Render it inside a `<Form>` whose values include a
* `password` string, and only while the role can log in.
*/
export const RolePasswordField = ({
isOptional = true,
isExistingRole = false,
layout = 'horizontal',
}: RolePasswordFieldProps) => {
const { control, setValue } = useFormContext<{ password: string }>()
const [isPasswordGenerated, setIsPasswordGenerated] = useState(false)
// A role without login can still hold a password, which a new one replaces
const hint = isExistingRole
? 'Optional. Setting a password replaces any existing one.'
: "Optional. Without a password, the role can't log in using password authentication."
return (
<FormField
control={control}
name="password"
render={({ field }) => (
<FormItemLayout
layout={layout}
label="Password"
description={
<>
{isOptional && `${hint} `}
<button
type="button"
tabIndex={0}
className={InlineLinkClassName}
onClick={() => {
setValue('password', generateStrongPassword(), { shouldDirty: true })
setIsPasswordGenerated(true)
}}
>
Generate a password
</button>
<span>.</span>
</>
}
>
<FormControl>
<Input
{...field}
onChange={(event) => {
field.onChange(event)
setIsPasswordGenerated(false)
}}
copy={field.value.length > 0}
type="password"
autoComplete="new-password"
placeholder="Type in a password"
/>
</FormControl>
<span role="status" aria-live="polite" className="sr-only">
{isPasswordGenerated ? 'Password generated' : ''}
</span>
</FormItemLayout>
)}
/>
)
}