setting-clash.tsx 2.9 KB

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