ThemeSelect.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Button, Dropdown, type MenuProps } from "antd"
  2. import { useEffect, useState } from "react";
  3. import { useIntl } from "react-intl";
  4. import { ThemeOutlinedIcon } from "../../assets/icon";
  5. import { refresh } from "../../reducers/theme";
  6. import store from "../../store";
  7. const ThemeSelectWidget = () => {
  8. const intl = useIntl();
  9. const [theme, setTheme] = useState<string>("ant");
  10. const items: MenuProps["items"] = [
  11. {
  12. key: "ant",
  13. label: "默认",
  14. },
  15. {
  16. key: "dark",
  17. label: "夜间",
  18. },
  19. ];
  20. const onClick: MenuProps["onClick"] = ({ key }) => {
  21. store.dispatch(refresh(key));
  22. localStorage.setItem("theme", key);
  23. };
  24. useEffect(() => {
  25. const currTheme = localStorage.getItem("theme");
  26. if (currTheme) {
  27. setTheme(currTheme);
  28. } else {
  29. setTheme("ant");
  30. }
  31. }, []);
  32. return (
  33. <Dropdown menu={{ items, onClick }} placement="bottomRight">
  34. <Button
  35. ghost
  36. style={{ border: "unset" }}
  37. icon={<ThemeOutlinedIcon style={{ color: "white" }} />}
  38. >
  39. {intl.formatMessage({
  40. id: `buttons.theme.${theme}`,
  41. })}
  42. </Button>
  43. </Dropdown>
  44. );
  45. };
  46. export default ThemeSelectWidget;