import { Button } from '@affine/admin/components/ui/button'; import { ScrollArea } from '@affine/admin/components/ui/scroll-area'; import { Separator } from '@affine/admin/components/ui/separator'; import type { RuntimeConfigType } from '@affine/graphql'; import { CheckIcon } from 'lucide-react'; import type { Dispatch, SetStateAction } from 'react'; import { useCallback, useMemo, useState } from 'react'; import { useNav } from '../nav/context'; import { ConfirmChanges } from './confirm-changes'; import { RuntimeSettingRow } from './runtime-setting-row'; import { useGetServerRuntimeConfig } from './use-get-server-runtime-config'; import { useUpdateServerRuntimeConfigs } from './use-update-server-runtime-config'; import { formatValue, formatValueForInput, isEqual, renderInput, } from './utils'; export type ModifiedValues = { id: string; expiredValue: any; newValue: any; }; export function SettingsPage() { const { trigger } = useUpdateServerRuntimeConfigs(); const { serverRuntimeConfig } = useGetServerRuntimeConfig(); const [open, setOpen] = useState(false); const [configValues, setConfigValues] = useState( serverRuntimeConfig.reduce( (acc, config) => { acc[config.id] = config.value; return acc; }, {} as Record ) ); const modifiedValues: ModifiedValues[] = useMemo(() => { return serverRuntimeConfig .filter(config => !isEqual(config.value, configValues[config.id])) .map(config => ({ id: config.id, key: config.key, expiredValue: config.value, newValue: configValues[config.id], })); }, [configValues, serverRuntimeConfig]); const handleSave = useCallback(() => { // post value example: { "key1": "newValue1","key2": "newValue2"} const updates: Record = {}; modifiedValues.forEach(item => { if (item.id && item.newValue !== undefined) { updates[item.id] = item.newValue; } }); trigger({ updates }); }, [modifiedValues, trigger]); const disableSave = modifiedValues.length === 0; const onOpen = useCallback(() => setOpen(true), [setOpen]); const onClose = useCallback(() => setOpen(false), [setOpen]); const onConfirm = useCallback(() => { if (disableSave) { return; } handleSave(); onClose(); }, [disableSave, handleSave, onClose]); return (
Settings
); } export const AdminPanel = ({ setConfigValues, configValues, }: { setConfigValues: Dispatch>>; configValues: Record; }) => { const { configGroup } = useGetServerRuntimeConfig(); const { currentModule } = useNav(); const handleInputChange = useCallback( (key: string, value: any, type: RuntimeConfigType) => { const newValue = formatValueForInput(value, type); setConfigValues(prevValues => ({ ...prevValues, [key]: newValue, })); }, [setConfigValues] ); return (
{configGroup .filter(group => group.moduleName === currentModule) .map(group => { const { moduleName, configs } = group; return (
{moduleName}
{configs?.map((config, index) => { const { id, type, description, updatedAt } = config; const isValueEqual = isEqual(config.value, configValues[id]); const formatServerValue = formatValue(config.value); const formatCurrentValue = formatValue(configValues[id]); return (
{index !== 0 && } handleInputChange(id, value, type) )} >
{formatServerValue} {' '} =>{' '} {formatCurrentValue}
); })}
); })}
); }; export { SettingsPage as Component };