/** * 应用管理页 —— 注册 / 编辑轻应用(iframe 与 window.open)。 * * 深蓝主题适配见 `./light-app-management.css`(《深蓝色 UI 规范》tokens.css)。 */ import { useEffect, useMemo, useRef, useState } from "react"; import { AlertCircle, AlignLeft, ArrowDown, ArrowUp, ChevronDown, ChevronUp, Copy, GripVertical, ExternalLink, FolderTree, Globe, HelpCircle, Info, Layers, Link2, Monitor, Pencil, Plus, Search, Sliders, Tag, Trash2, X, } from "lucide-react"; import { toast } from "sonner"; import { Select } from "tdesign-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { useLightAppActions, useLightApps, type LightApp, type LightAppRouteParam, type LightAppType, } from "@/strategy-components/api/light-apps"; import { APP_THEME_COLORS, LOGIN_SOURCE_OPTIONS, MOUNT_OPTIONS, mountLabel, parseThemeMap, stringifyThemeMap, } from "@/strategy-components/lib/light-app"; import "./light-app-management.css"; interface FormState { name: string; description: string; appType: LightAppType; url: string; mountLocation: string; enabled: boolean; routeParams: LightAppRouteParam[]; } const APP_TYPE_OPTIONS: { label: string; value: LightAppType }[] = [ { label: "iframe 嵌套 (挂载到菜单)", value: "iframe" }, { label: "window.open (新页跳转)", value: "window_open" }, ]; const PARAM_TYPE_OPTIONS: { label: string; value: "login" | "theme" }[] = [ { label: "登录参数", value: "login" }, { label: "主题参数", value: "theme" }, ]; const DIALOG_SELECT_POPUP = { attach: () => document.body, overlayClassName: "lam-select-popup", }; const emptyForm = (): FormState => ({ name: "", description: "", appType: "window_open", url: "", mountLocation: "", enabled: true, routeParams: [], }); const newParam = (): LightAppRouteParam => ({ key: "access_token", paramType: "login", source: "access_token", customValue: "", themeValue: "red=red;blue=blue;dark-blue=dark-blue", }); function formFromApp(app: LightApp): FormState { return { name: app.name, description: app.description ?? "", appType: app.appType, url: app.url, mountLocation: app.mountLocation ?? "", enabled: app.enabled, routeParams: app.routeParams.map((p) => ({ key: p.key, paramType: p.paramType, source: p.source ?? "access_token", customValue: p.customValue ?? "", themeValue: p.themeValue ?? "", })), }; } function getParamSourceLabel(param: LightAppRouteParam): string { if (param.paramType === "theme") { return `主题参数: ${param.themeValue || ""}`; } const source = param.source || "access_token"; const option = LOGIN_SOURCE_OPTIONS.find((o) => o.value === source); if (source === "other") { return `登录参数: ${param.customValue || ""}`; } return `登录参数: ${option?.label || source}`; } export default function LightAppManagementPage() { const [search, setSearch] = useState(""); const { data: apps = [], isLoading } = useLightApps({ search }); const actions = useLightAppActions(); const [dialogOpen, setDialogOpen] = useState(false); const [editing, setEditing] = useState(null); const [form, setForm] = useState(emptyForm); const [activeParamPopover, setActiveParamPopover] = useState(null); const [viewMode, setViewMode] = useState<"table" | "cards">("table"); const [categoryFilter, setCategoryFilter] = useState<"all" | LightAppType>("all"); const [mountFilter, setMountFilter] = useState(""); const saving = actions.create.isPending || actions.update.isPending; const openCreate = () => { setEditing(null); setForm(emptyForm()); setDialogOpen(true); }; const openEdit = (app: LightApp) => { setEditing(app); setForm(formFromApp(app)); setDialogOpen(true); }; const patchForm = (patch: Partial) => setForm((f) => ({ ...f, ...patch })); const patchParam = (idx: number, patch: Partial) => setForm((f) => ({ ...f, routeParams: f.routeParams.map((p, i) => (i === idx ? { ...p, ...patch } : p)), })); const addParam = () => patchForm({ routeParams: [...form.routeParams, newParam()] }); const removeParam = (idx: number) => patchForm({ routeParams: form.routeParams.filter((_, i) => i !== idx) }); const handleSave = async () => { const name = form.name.trim(); const url = form.url.trim(); if (!name) return toast.error("请输入应用名称"); if (!url) return toast.error("请输入应用地址"); if (form.appType === "iframe" && !form.mountLocation) { return toast.error("iframe 类型请选择挂载菜单"); } for (const p of form.routeParams) { if (!p.key.trim()) return toast.error("路由参数键名不可为空"); if (p.paramType === "login" && p.source === "other" && !p.customValue?.trim()) { return toast.error("登录参数自定义值不可为空"); } if (p.paramType === "theme" && !p.themeValue?.trim()) { return toast.error("主题参数值不可为空"); } } const payload = { name, description: form.description.trim() || null, appType: form.appType, url, routeParams: cleanParams(form.routeParams), mountLocation: form.appType === "iframe" ? form.mountLocation : null, enabled: form.enabled, }; try { if (editing) { await actions.update.mutateAsync({ appId: editing.appId, patch: payload }); toast.success(`已更新「${name}」`); } else { await actions.create.mutateAsync(payload); toast.success(`已注册「${name}」`); } setDialogOpen(false); } catch (err) { toast.error((err as Error).message || "保存失败"); } }; const handleDelete = async (app: LightApp) => { if (!window.confirm(`是否确定要删除「${app.name}」?该操作不可逆。`)) return; try { await actions.remove.mutateAsync(app.appId); toast.success(`已删除「${app.name}」`); } catch (err) { toast.error((err as Error).message || "删除失败"); } }; const handleCopyUrl = (url: string) => { navigator.clipboard?.writeText(url); toast.success("已复制地址"); }; const selectCategory = (cat: "all" | LightAppType) => { setCategoryFilter(cat); if (cat !== "iframe") setMountFilter(""); }; const tableApps = useMemo( () => apps.filter((app) => { if (categoryFilter !== "all" && app.appType !== categoryFilter) return false; if (categoryFilter === "iframe" && mountFilter && app.mountLocation !== mountFilter) { return false; } return true; }), [apps, categoryFilter, mountFilter], ); // ── 排序调整:仅当「iframe 嵌套 + 指定某个挂载菜单」时开启 ── // 同一菜单下的多个 iframe 子项可拖拽 / 上下移调整顺序;调整即写回各应用的 sortOrder, // 侧栏(use-sidebar-menu 按列表顺序注入,后端按 sort_order 升序返回)随之同步。 const reorderable = categoryFilter === "iframe" && !!mountFilter && viewMode === "table"; // 乐观顺序覆盖(保存期间稳定显示,刷新回来与服务端一致);切换筛选/搜索即清空,回落服务端顺序。 const [orderIds, setOrderIds] = useState(null); const [reordering, setReordering] = useState(false); const dragIndexRef = useRef(null); useEffect(() => { setOrderIds(null); }, [categoryFilter, mountFilter, search]); const displayApps = useMemo(() => { if (!reorderable || !orderIds) return tableApps; const pos = new Map(orderIds.map((id, i) => [id, i] as const)); return [...tableApps].sort( (a, b) => (pos.get(a.appId) ?? 1e9) - (pos.get(b.appId) ?? 1e9), ); }, [reorderable, orderIds, tableApps]); // 把整组按新顺序重排 sortOrder(组内 0-based;只 PUT 顺序变了的项),乐观先行。 const persistOrder = async (ordered: LightApp[]) => { setOrderIds(ordered.map((a) => a.appId)); const changed = ordered.filter((a, i) => a.sortOrder !== i); if (changed.length === 0) return; setReordering(true); try { await Promise.all( ordered.map((a, i) => a.sortOrder === i ? Promise.resolve() : actions.update.mutateAsync({ appId: a.appId, patch: { sortOrder: i } }), ), ); } catch (err) { toast.error((err as Error).message || "排序保存失败"); setOrderIds(null); } finally { setReordering(false); } }; const moveApp = (index: number, dir: -1 | 1) => { if (reordering) return; const target = index + dir; if (target < 0 || target >= displayApps.length) return; const next = [...displayApps]; [next[index], next[target]] = [next[target]!, next[index]!]; void persistOrder(next); }; const handleRowDrop = (toIndex: number) => { const from = dragIndexRef.current; dragIndexRef.current = null; if (from === null || from === toIndex || reordering) return; const next = [...displayApps]; const [moved] = next.splice(from, 1); if (!moved) return; next.splice(toIndex, 0, moved); void persistOrder(next); }; return (

应用管理

注册轻应用。 window.open 类型在「应用列表」以卡片展示; iframe 类型挂载到所选菜单下并注入侧栏。

setSearch(e.target.value)} placeholder="搜索应用名称 / 描述..." className="lam-search-input w-full rounded-[5px] border border-neutral-100 bg-neutral-50/60 py-2 pl-10 pr-10 text-xs text-neutral-800 placeholder-neutral-400 transition-all hover:bg-white focus:border-[#F5222D]/40 focus:outline-none focus:ring-4 focus:ring-[#F5222D]/10" /> {search && ( )}
{( [ { value: "all", label: "全部" }, { value: "window_open", label: "新页跳转" }, { value: "iframe", label: "iframe 嵌套" }, ] as const ).map((c) => ( ))}
{categoryFilter === "iframe" && (
patchForm({ appType: v as LightAppType })} />

{form.appType === "window_open" ? "在新标签页或卡片式路由容器打开。多用于外部高度复杂的系统跳转。" : "通过标准 iframe 嵌入页面,可直接挂载到左侧导航树相应分类内。"}

patchForm({ name: e.target.value })} placeholder="如: 舆情看板" className="lam-input h-9 rounded-[5px] border-[#DBDBDB] text-[14px]" />
patchForm({ url: e.target.value })} placeholder="https://example.com/page 或 http://47.88.25.99:3000/" className="lam-input h-9 rounded-[5px] border-[#DBDBDB] font-mono text-[14px]" />
{form.appType === "iframe" && (