All files / features/admin/components/global-settings LocalizationForm.tsx

87.8% Statements 36/41
61.11% Branches 11/18
92.3% Functions 12/13
94.59% Lines 35/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216                                    1x             1x       9x 9x 9x 9x   9x   9x 1x 1x       1x     9x 1x 1x 1x     9x 1x 1x 1x     9x   9x                                       36x 36x       1x 1x 1x           1x   1x 1x                                                                         36x                         1x                                                   2x 2x                             1x                         1x                              
import { Input } from '@core/components/ui/input'
import { Label } from '@core/components/ui/label'
import { Button } from '@core/components/ui/button'
import { Plus, Trash2 } from 'lucide-react'
import { useFormContext } from 'react-hook-form'
import { useState } from 'react'
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@core/components/ui/card'
import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from '@core/components/ui/select'
import { useTranslation } from 'react-i18next'
 
import { LOCALE_LABELS } from '@fundraising/white-labeling'
 
const CLIENT_LOCALE_GROUPS = LOCALE_LABELS
 
interface LocalizationFormProps {
    localeOverrides: { key: string; value: string; locale: string }[]
    setLocaleOverrides: (val: { key: string; value: string; locale: string }[]) => void
}
 
export const LocalizationForm = ({
    localeOverrides,
    setLocaleOverrides,
}: LocalizationFormProps) => {
    const { t } = useTranslation()
    const { watch, setValue } = useFormContext()
    const [selectedLocale, setSelectedLocale] = useState('en')
    const [newLabelKey, setNewLabelKey] = useState('')
 
    const supportedLocales = watch('locales.supported') || []
 
    const addLocaleOverride = () => {
        Iif (!newLabelKey || !selectedLocale) return
        setLocaleOverrides([
            ...localeOverrides,
            { key: newLabelKey, value: '', locale: selectedLocale },
        ])
        setNewLabelKey('')
    }
 
    const removeLocaleField = (idx: number) => {
        const newItems = [...localeOverrides]
        newItems.splice(idx, 1)
        setLocaleOverrides(newItems)
    }
 
    const updateLocaleField = (idx: number, val: string) => {
        const newItems = [...localeOverrides]
        newItems[idx].value = val
        setLocaleOverrides(newItems)
    }
 
    const filteredOverrides = localeOverrides.filter((l) => l.locale === selectedLocale)
 
    return (
        <Card
            style={{
                backgroundColor: 'var(--admin-card-bg)',
                borderColor: 'var(--admin-border-color)',
            }}
        >
            <CardHeader>
                <CardTitle>{t('admin_branding.localization.title')}</CardTitle>
                <CardDescription>{t('admin_branding.localization.description')}</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="flex flex-col space-y-8">
                    {/* Supported Languages */}
                    <div>
                        <Label className="mb-4 block font-medium">
                            {t('admin_branding.localization.supported_languages')}
                        </Label>
                        <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-3">
                            {Object.entries(CLIENT_LOCALE_GROUPS).map(([code, name]) => {
                                const isSelected = supportedLocales.includes(code)
                                return (
                                    <div
                                        key={code}
                                        onClick={() => {
                                            const current = [...supportedLocales]
                                            let next = current
                                            Iif (isSelected) {
                                                if (current.length > 1) {
                                                    // Prevent deselecting all
                                                    next = current.filter((c: string) => c !== code)
                                                }
                                            } else {
                                                next = [...current, code]
                                            }
                                            Eif (next !== current) {
                                                setValue('locales.supported', next, {
                                                    shouldDirty: true,
                                                    shouldTouch: true,
                                                })
                                            }
                                        }}
                                        className={`cursor-pointer rounded-md border px-3 py-2 flex items-center justify-center text-center transition-all text-sm font-medium ${
                                            isSelected
                                                ? 'border-primary bg-primary/10 text-primary ring-1 ring-primary/20'
                                                : 'bg-background hover:bg-muted hover:border-muted-foreground/50'
                                        }`}
                                    >
                                        {name}
                                    </div>
                                )
                            })}
                        </div>
                    </div>
 
                    {/* Label Overrides */}
                    <div className="space-y-4">
                        <Label className="font-medium">
                            {t('admin_branding.localization.label_overrides')}
                        </Label>
 
                        {/* Controls */}
                        <div className="flex gap-2 p-3 border rounded-md bg-muted/5 items-end">
                            <div className="grid gap-1.5 flex-1">
                                <Label className="text-xs text-muted-foreground">
                                    {t('admin_branding.localization.language')}
                                </Label>
                                <Select value={selectedLocale} onValueChange={setSelectedLocale}>
                                    <SelectTrigger className="h-8 text-xs bg-background">
                                        <SelectValue />
                                    </SelectTrigger>
                                    <SelectContent>
                                        {Object.keys(CLIENT_LOCALE_GROUPS).map((code) => (
                                            <SelectItem key={code} value={code}>
                                                {code.toUpperCase()}
                                            </SelectItem>
                                        ))}
                                    </SelectContent>
                                </Select>
                            </div>
                            <div className="grid gap-1.5 flex-[1.5]">
                                <Label className="text-xs text-muted-foreground">
                                    {t('admin_branding.localization.key')}
                                </Label>
                                <Input
                                    value={newLabelKey}
                                    onChange={(e) => setNewLabelKey(e.target.value)}
                                    placeholder={t('admin_branding.localization.key_placeholder')}
                                    className="h-8 text-xs bg-background"
                                />
                            </div>
                            <Button
                                type="button"
                                size="sm"
                                onClick={addLocaleOverride}
                                className="h-8 px-3"
                                aria-label="Add Override"
                            >
                                <Plus className="h-3 w-3" />
                            </Button>
                        </div>
 
                        {/* List */}
                        <div className="space-y-2 max-h-[300px] overflow-y-auto border rounded-md p-2">
                            {filteredOverrides.length === 0 && (
                                <div className="text-center py-8 text-muted-foreground text-sm italic">
                                    {t('admin_branding.localization.no_overrides', {
                                        locale: selectedLocale.toUpperCase(),
                                    })}
                                </div>
                            )}
                            {localeOverrides.map((item, idx) => {
                                Iif (item.locale !== selectedLocale) return null
                                return (
                                    <div
                                        key={idx}
                                        className="flex gap-2 items-center bg-muted/10 p-2 rounded border border-transparent hover:border-border transition-colors"
                                    >
                                        <div className="flex-1 grid gap-1">
                                            <span
                                                className="text-[10px] font-mono text-muted-foreground truncate"
                                                title={item.key}
                                            >
                                                {item.key}
                                            </span>
                                            <Input
                                                value={item.value}
                                                onChange={(e) =>
                                                    updateLocaleField(idx, e.target.value)
                                                }
                                                className="h-7 text-xs"
                                                placeholder={t(
                                                    'admin_branding.localization.translation',
                                                )}
                                            />
                                        </div>
                                        <Button
                                            type="button"
                                            variant="ghost"
                                            size="icon"
                                            className="h-6 w-6 shrink-0 text-muted-foreground hover:text-destructive"
                                            onClick={() => removeLocaleField(idx)}
                                            aria-label="Remove Override"
                                        >
                                            <Trash2 className="h-3 w-3" />
                                        </Button>
                                    </div>
                                )
                            })}
                        </div>
                    </div>
                </div>
            </CardContent>
        </Card>
    )
}