import { Input } from '@affine/admin/components/ui/input';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@affine/admin/components/ui/select';
import { Switch } from '@affine/admin/components/ui/switch';
import { useCallback } from 'react';
import { Textarea } from '../../components/ui/textarea';
export type ConfigInputProps = {
field: string;
desc: string;
defaultValue: any;
onChange: (field: string, value: any) => void;
error?: string;
} & (
| {
type: 'String' | 'Number' | 'Boolean' | 'JSON';
}
| {
type: 'Enum';
options: string[];
}
);
const Inputs: Record<
ConfigInputProps['type'],
React.ComponentType<{
defaultValue: any;
onChange: (value?: any) => void;
options?: string[];
error?: string;
}>
> = {
Boolean: function SwitchInput({ defaultValue, onChange }) {
const handleSwitchChange = (checked: boolean) => {
onChange(checked);
};
return (
);
},
String: function StringInput({ defaultValue, onChange }) {
const handleInputChange = (e: React.ChangeEvent) => {
onChange(e.target.value);
};
return (
);
},
Number: function NumberInput({ defaultValue, onChange }) {
const handleInputChange = (e: React.ChangeEvent) => {
onChange(parseInt(e.target.value));
};
return (
);
},
JSON: function ObjectInput({ defaultValue, onChange }) {
const handleInputChange = (e: React.ChangeEvent) => {
try {
const value = JSON.parse(e.target.value);
onChange(value);
} catch {}
};
return (
);
},
Enum: function EnumInput({ defaultValue, onChange, options }) {
return (
);
},
};
export const ConfigRow = ({
field,
desc,
type,
defaultValue,
onChange,
error,
...props
}: ConfigInputProps) => {
const Input = Inputs[type] ?? Inputs.JSON;
const onValueChange = useCallback(
(value?: any) => {
onChange(field, value);
},
[field, onChange]
);
return (
);
};