'use client'
import type { TextFieldClientComponent } from 'payload'
import { FieldDescription, FieldLabel, useField, useTranslation } from '@payloadcms/ui'
import { useState } from 'react'
import { ipalT } from '../../../modules/admin-i18n/t.js'
/**
* Masked text input for sensitive values (SMTP password, R2 secret key,
* Turnstile secret key, etc.).
*
* Renders the value as a password field (••••) so secrets aren't shown in
* plaintext on screen — protecting against shoulder-surfing and accidental
* exposure when the panel is open. The value is still stored and returned
* normally (UI masking, NOT hashing or encryption), because the plugin must
* read it back to authenticate with SMTP/R2/Turnstile. A toggle reveals the
* value when an authorised editor genuinely needs to check it.
*
* Uses a raw driven by useField rather than @payloadcms/ui's TextInput,
* because TextInput doesn't expose inputProps, so there's no way to switch its
* inner input to type="password".
*
* Assigned via a field's admin.components.Field. Works on any `text` field.
*/
export const MaskedField: TextFieldClientComponent = ({ field, path }) => {
const { t } = useTranslation()
const { setValue, value } = useField({ path })
const [revealed, setRevealed] = useState(false)
// A string or one string per language — FieldLabel / FieldDescription translate it.
const label = field?.label || field?.name || path
const description = field?.admin?.description
return (
{/* Labelled by above — the rule can't see through the component. */}
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
setValue(e.target.value)}
style={{ flex: 1 }}
type={revealed ? 'text' : 'password'}
value={value ?? ''}
/>