states.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { atom } from "recoil";
  2. export const atomThemeMode = atom<"light" | "dark">({
  3. key: "atomThemeMode",
  4. default: "light",
  5. });
  6. export const atomClashPort = atom<number>({
  7. key: "atomClashPort",
  8. default: 0,
  9. });
  10. export const atomLogData = atom<ApiType.LogItem[]>({
  11. key: "atomLogData",
  12. default: [],
  13. });
  14. export const atomEnableLog = atom<boolean>({
  15. key: "atomEnableLog",
  16. effects: [
  17. ({ setSelf, onSet }) => {
  18. const key = "enable-log";
  19. try {
  20. setSelf(localStorage.getItem(key) !== "false");
  21. } catch {}
  22. onSet((newValue, _, isReset) => {
  23. try {
  24. if (isReset) {
  25. localStorage.removeItem(key);
  26. } else {
  27. localStorage.setItem(key, newValue.toString());
  28. }
  29. } catch {}
  30. });
  31. },
  32. ],
  33. });
  34. interface IConnectionSetting {
  35. layout: "table" | "list";
  36. }
  37. export const atomConnectionSetting = atom<IConnectionSetting>({
  38. key: "atomConnectionSetting",
  39. effects: [
  40. ({ setSelf, onSet }) => {
  41. const key = "connections-setting";
  42. try {
  43. const value = localStorage.getItem(key);
  44. const data = value == null ? { layout: "list" } : JSON.parse(value);
  45. setSelf(data);
  46. } catch {
  47. setSelf({ layout: "list" });
  48. }
  49. onSet((newValue) => {
  50. try {
  51. localStorage.setItem(key, JSON.stringify(newValue));
  52. } catch {}
  53. });
  54. },
  55. ],
  56. });
  57. // save the state of each profile item loading
  58. export const atomLoadingCache = atom<Record<string, boolean>>({
  59. key: "atomLoadingCache",
  60. default: {},
  61. });
  62. // save update state
  63. export const atomUpdateState = atom<boolean>({
  64. key: "atomUpdateState",
  65. default: false,
  66. });
  67. // current profile uid
  68. export const atomCurrentProfile = atom<string>({
  69. key: "atomCurrentProfile",
  70. default: "",
  71. });