import React, { useEffect, useMemo, useState } from "react"; import { Button, ButtonProps, Dropdown, MenuProps, theme } from "antd"; import { useSessionStorageState } from "ahooks"; import AliIcon from "../../icon/aliIcon"; import { KeepAliveTab } from "../../hooks/use-tabs"; import ProgramConfig from "../../units"; import { useLocale } from "../../locale/useLocale"; const { useToken } = theme; const regex = /#\/v\d+\/(.*)/; const defThemes = [{ label: "默认", value: "v2" }, { label: "科技风一", value: "v3" }, { label: "科技风二", value: "v4" }, { label: "侧边布局", value: "v5" }]; interface Props extends ButtonProps { children?: React.ReactNode, placement?: "topLeft" | "topCenter" | "topRight" | "bottomLeft" | "bottomCenter" | "bottomRight" | "top" | "bottom", } const ThemeButton = (props: Props) => { const { children, placement, ...otherProps } = props; const { token } = useToken(); const [version, setVersion] = useState(); const [themes, setThemes] = useState<{ label: string, value: string }[]>([]); const [keepAliveTabs, setKeepAliveTabs] = useSessionStorageState('keepAliveTabs', { defaultValue: [] }); const { translate } = useLocale(); const getThemeConfig = async () => { const themeConfig = await ProgramConfig.getThemeConfig(); const themeKeys = themeConfig?.styleTheme?.split(",") || ProgramConfig.themes(); const temThemes = defThemes.filter(item => themeKeys?.includes(item.value)); setThemes(temThemes.length ? temThemes : [{ label: "默认", value: "v2" }]); } useEffect(() => { getThemeConfig(); const version = localStorage.getItem("version") || "v2"; setVersion(version); }, []); const onClick: MenuProps["onClick"] = ({ key }) => { setKeepAliveTabs([]); localStorage.setItem("version", key); setVersion(key); const hash = window.location.hash; const match = hash.match(regex); window.location.hash = `#/${key}/${match[1]}`; }; const items: MenuProps["items"] = useMemo(() => themes.map((item) => ({ label: ( {translate("${" + item.label + "}")} ), key: item.value, })), [themes]); return ( {children ||