setting-clash.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import useSWR, { useSWRConfig } from "swr";
  2. import {
  3. ListItemText,
  4. TextField,
  5. Switch,
  6. Select,
  7. MenuItem,
  8. } from "@mui/material";
  9. import { getClashConfig, updateConfigs } from "../../services/api";
  10. import { SettingList, SettingItem } from "./setting";
  11. import { patchClashConfig } from "../../services/cmds";
  12. import { ApiType } from "../../services/types";
  13. import GuardState from "./guard-state";
  14. interface Props {
  15. onError?: (err: Error) => void;
  16. }
  17. const SettingClash = ({ onError }: Props) => {
  18. const { mutate } = useSWRConfig();
  19. const { data: clashConfig } = useSWR("getClashConfig", getClashConfig);
  20. const {
  21. ipv6 = false,
  22. "allow-lan": allowLan = false,
  23. "log-level": logLevel = "silent",
  24. "mixed-port": mixedPort = 7890,
  25. } = clashConfig ?? {};
  26. const onSwitchFormat = (_e: any, value: boolean) => value;
  27. const onChangeData = (patch: Partial<ApiType.ConfigData>) => {
  28. mutate("getClashConfig", { ...clashConfig, ...patch }, false);
  29. };
  30. const onUpdateData = async (patch: Partial<ApiType.ConfigData>) => {
  31. await updateConfigs(patch);
  32. await patchClashConfig(patch);
  33. };
  34. return (
  35. <SettingList title="Clash Setting">
  36. <SettingItem>
  37. <ListItemText primary="Allow Lan" />
  38. <GuardState
  39. value={allowLan}
  40. valueProps="checked"
  41. onCatch={onError}
  42. onFormat={onSwitchFormat}
  43. onChange={(e) => onChangeData({ "allow-lan": e })}
  44. onGuard={(e) => onUpdateData({ "allow-lan": e })}
  45. >
  46. <Switch edge="end" />
  47. </GuardState>
  48. </SettingItem>
  49. <SettingItem>
  50. <ListItemText primary="IPv6" />
  51. <GuardState
  52. value={ipv6}
  53. valueProps="checked"
  54. onCatch={onError}
  55. onFormat={onSwitchFormat}
  56. onChange={(e) => onChangeData({ ipv6: e })}
  57. onGuard={(e) => onUpdateData({ ipv6: e })}
  58. >
  59. <Switch edge="end" />
  60. </GuardState>
  61. </SettingItem>
  62. <SettingItem>
  63. <ListItemText primary="Log Level" />
  64. <GuardState
  65. value={logLevel}
  66. onCatch={onError}
  67. onFormat={(e: any) => e.target.value}
  68. onChange={(e) => onChangeData({ "log-level": e })}
  69. onGuard={(e) => onUpdateData({ "log-level": e })}
  70. >
  71. <Select size="small" sx={{ width: 120 }}>
  72. <MenuItem value="debug">Debug</MenuItem>
  73. <MenuItem value="info">Info</MenuItem>
  74. <MenuItem value="warning">Warning</MenuItem>
  75. <MenuItem value="error">Error</MenuItem>
  76. <MenuItem value="silent">Silent</MenuItem>
  77. </Select>
  78. </GuardState>
  79. </SettingItem>
  80. <SettingItem>
  81. <ListItemText primary="Mixed Port" />
  82. <TextField
  83. size="small"
  84. value={mixedPort!}
  85. sx={{ width: 120 }}
  86. disabled
  87. />
  88. </SettingItem>
  89. </SettingList>
  90. );
  91. };
  92. export default SettingClash;