import React, { useEffect, useState, useCallback } from 'react'; import { VariableBuilder } from './VariableBuilder'; import { Loader2 } from "lucide-react"; import { toast } from "sonner"; import { translate } from '@/i18n'; const STORAGE_KEY = 'variables-editor-playground'; const SYSTEM_VARIABLE_SCHEMA: Record = { showAuthor: { label: translate("Show Author"), description: translate("Toggle the display of the author name.") }, showDate: { label: translate("Show Date"), description: translate("Toggle the display of the publish date.") }, showCategories: { label: translate("Show Categories"), description: translate("Toggle the display of the category paths.") }, showActions: { label: translate("Show Actions"), description: translate("Toggle the display of the top-right page actions menu.") }, showParent: { label: translate("Show Parent Path"), description: translate("Toggle the display of the parent page path above the title.") }, showTitle: { label: translate("Show Title"), description: translate("Toggle the display of the page title block.") }, showToc: { label: translate("Show Table of Contents"), description: translate("Toggle the display of the side table of contents.") }, showLastUpdated: { label: translate("Show Last Updated"), description: translate("Toggle the display of the last updated footer.") }, showFooter: { label: translate("Show Global Footer"), description: translate("Toggle the display of the global app footer site-wide.") }, }; const defaultOnLoad = async (): Promise> => { try { const stored = localStorage.getItem(STORAGE_KEY); return stored ? JSON.parse(stored) : {}; } catch { return {}; } }; const defaultOnSave = async (data: Record): Promise => { localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); }; export interface VariablesEditorProps { onLoad?: () => Promise>; onSave?: (data: Record) => Promise; inheritedData?: Record; variableSchema?: Record; } export const VariablesEditor: React.FC = ({ onLoad = defaultOnLoad, onSave = defaultOnSave, inheritedData = {}, variableSchema }) => { const [variables, setVariables] = useState>({}); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const loadVariables = useCallback(async () => { setLoading(true); try { const data = await onLoad(); setVariables(data || {}); } catch (error) { console.error("Failed to fetch variables", error); toast.error(translate("Failed to load variables")); } finally { setLoading(false); } }, [onLoad]); useEffect(() => { loadVariables(); }, [loadVariables]); const handleSave = async (builderVariables: Record) => { setSaving(true); try { await onSave(builderVariables); setVariables(builderVariables); toast.success(translate("Variables saved successfully")); } catch (error) { console.error("Failed to save variables", error); toast.error(translate("Failed to save variables")); } finally { setSaving(false); } }; const mergedSchema = React.useMemo(() => ({ ...SYSTEM_VARIABLE_SCHEMA, ...variableSchema }), [variableSchema]); if (loading) { return (
); } return (
); };