"use client"; import React, { useState, useEffect } from "react"; import { calculateFileSha256 } from "@/shared/utils/crypto"; import { Input, Button, AccordionItem, Accordion, Select, SelectItem, } from "@nextui-org/react"; import MDEditor, { commands } from "@uiw/react-md-editor"; import { PlatformConfig, Theme } from "@/shared/types/system"; import { useTranslation } from "@/shared/lib/useTranslation"; import { SUPPORTED_LANGUAGES } from "@/shared/lib/i18n"; import { ThemeSelector } from "./ThemeSelector"; import "@uiw/react-md-editor/markdown-editor.css"; import "@uiw/react-markdown-preview/markdown.css"; interface PlatformConfigFormProps { config: PlatformConfig; onSave: (config: PlatformConfig) => void; onThemeChange?: (theme: Theme) => Promise; onAboutSave?: (aboutContent: string) => Promise; onImageUpload?: (file: File, sha256?: string) => Promise; isLoading?: boolean; isAboutLoading?: boolean; } export const PlatformConfigForm: React.FC = ({ config, onSave, onThemeChange, onAboutSave, onImageUpload, isLoading = false, isAboutLoading = false, }) => { const { t } = useTranslation(); const [formData, setFormData] = useState(config); // when context config updated, sync to form useEffect(() => { setFormData(config); }, [config]); const handleInputChange = (field: keyof PlatformConfig, value: string) => { setFormData((prev) => ({ ...prev, [field]: value, })); }; const handleSave = async () => { onSave(formData); }; const handleThemeChange = async (theme: Theme) => { handleInputChange("theme", theme); onThemeChange?.(theme); }; const handleAboutSave = async () => { onAboutSave?.(formData.about_page || ""); }; const handleImageUpload = async (file: File): Promise => { try { // 计算文件的 SHA256 哈希值 const sha256 = await calculateFileSha256(file); return await onImageUpload!(file, sha256); } catch (error) { console.error("Image upload failed:", error); throw error; } }; return (
{/* Platform Information Accordion */}

{t("Platform Information")}

{t("Configure platform logo, name, titles, language")}

} >
{/* platform name */} handleInputChange("name", e.target.value)} /> {/* platform logo */} handleInputChange("logo", e.target.value)} /> {/* website title */} handleInputChange("website_title", e.target.value) } /> {/* homepage headline */} handleInputChange("headline", e.target.value)} /> {/* homepage subheadline */} handleInputChange("subheadline", e.target.value)} /> {/* platform language */}
{/* Action Buttons */}
{/* About Page Accordion */}

{t("About Page Content")}

{t("Configure about page content")}

} >
handleInputChange("about_page", value || "") } preview="live" hideToolbar={false} visibleDragbar={false} data-color-mode="light" textareaProps={{ placeholder: t("Enter about page content (supports Markdown)"), style: { fontSize: 14, lineHeight: 1.6, fontFamily: 'ui-monospace, SFMono-Regular, "SF Mono", Monaco, Consolas, "Liberation Mono", "Courier New", monospace', }, }} height={300} commands={[ // 默认命令 commands.bold, commands.italic, commands.strikethrough, commands.hr, commands.title, commands.divider, commands.link, commands.quote, commands.code, commands.codeBlock, commands.comment, commands.divider, commands.unorderedListCommand, commands.orderedListCommand, commands.checkedListCommand, commands.divider, // 自定义图片上传命令 { name: "image-upload", keyCommand: "image-upload", buttonProps: { "aria-label": "Upload image", title: "Upload image", }, icon: ( ), execute: (state, api) => { const input = document.createElement("input"); input.type = "file"; input.accept = "image/*"; input.onchange = async (e) => { const file = (e.target as HTMLInputElement).files?.[0]; if (file) { try { const dataUrl = await handleImageUpload(file); const imageMarkdown = `![${file.name}](${window.location.origin}${dataUrl})`; api.replaceSelection(imageMarkdown); } catch (error) { console.error("Image upload failed:", error); } } }; input.click(); }, }, ]} />
{/* About Save Button */}
{/* Theme Accordion */}

{t("Theme")}

{t("Choose your preferred theme style")}

} >
); };