import React, { useState } from 'react'; import { ArrowDown, ArrowUp, Plus, X } from 'lucide-react'; import type { AdminExtensionSettingField, AdminExtensionSettingValue } from '@/api/admin/extensions/queries'; import Button from '@/components/elements/Button'; import Icon from '@/components/elements/Icon'; import Label from '@/components/elements/Label'; import Select from '@/components/ui/Select'; import Switch from '@/components/ui/Switch'; import { FileInput, NumberInput, TextArea, TextInput } from '@/components/form/controls'; import { isString } from '@/lib/objects'; export type SettingValue = AdminExtensionSettingValue | undefined; export type SettingFileActions = { pending: boolean; onUpload: (file: File) => void; onClear: () => void; }; type ControlProps = { field: AdminExtensionSettingField; value: SettingValue; onChange: (value: AdminExtensionSettingValue) => void; }; const uploadedFilePrefix = '/extension-files/'; // only accepts #rrggbb. const pickerFallback = `#${'0'.repeat(6)}`; const hexColor = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i; const asText = (value: SettingValue): string => (isString(value) ? value : ''); const asItems = (value: SettingValue): string[] => (Array.isArray(value) ? value.filter(isString) : []); const asChoices = (value: SettingValue): (string | number)[] => (Array.isArray(value) ? value : []); const asNumber = (value: SettingValue): number | null => value === undefined || value === null || value === '' ? null : Number(value); // Row keys continue past the largest existing key so a reset or append never reuses a mounted row. const freshKeys = (existing: number[], count: number): number[] => { const start = existing.length > 0 ? Math.max(...existing) + 1 : 0; return Array.from({ length: count }, (_, i) => start + i); }; export const pickerColor = (value: string): string | null => { if (!hexColor.test(value)) { return null; } const digits = value.slice(1); const expanded = digits.length <= 4 ? Array.from(digits.slice(0, 3), (digit) => digit + digit).join('') : digits.slice(0, 6); return `#${expanded.toLowerCase()}`; }; export const submittedItems = (value: SettingValue): string[] => asItems(value) .map((item) => item.trim()) .filter((item) => item !== ''); function ColorControl({ field, value, onChange }: ControlProps) { const color = asText(value); return (