enhanced.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import useSWR from "swr";
  2. import { useLockFn } from "ahooks";
  3. import { Box, Grid } from "@mui/material";
  4. import {
  5. getProfiles,
  6. deleteProfile,
  7. enhanceProfiles,
  8. changeProfileChain,
  9. } from "../../services/cmds";
  10. import { CmdType } from "../../services/types";
  11. import Notice from "../base/base-notice";
  12. import ProfileMore from "./profile-more";
  13. interface Props {
  14. items: CmdType.ProfileItem[];
  15. chain: string[];
  16. }
  17. const EnhancedMode = (props: Props) => {
  18. const { items, chain } = props;
  19. const { mutate } = useSWR("getProfiles", getProfiles);
  20. // handler
  21. const onEnhance = useLockFn(enhanceProfiles);
  22. const onEnhanceEnable = useLockFn(async (uid: string) => {
  23. if (chain.includes(uid)) return;
  24. const newChain = [...chain, uid];
  25. await changeProfileChain(newChain);
  26. mutate((conf = {}) => ({ ...conf, chain: newChain }), true);
  27. });
  28. const onEnhanceDisable = useLockFn(async (uid: string) => {
  29. if (!chain.includes(uid)) return;
  30. const newChain = chain.filter((i) => i !== uid);
  31. await changeProfileChain(newChain);
  32. mutate((conf = {}) => ({ ...conf, chain: newChain }), true);
  33. });
  34. const onEnhanceDelete = useLockFn(async (uid: string) => {
  35. try {
  36. await onEnhanceDisable(uid);
  37. await deleteProfile(uid);
  38. mutate();
  39. } catch (err: any) {
  40. Notice.error(err?.message || err.toString());
  41. }
  42. });
  43. const onMoveTop = useLockFn(async (uid: string) => {
  44. if (!chain.includes(uid)) return;
  45. const newChain = [uid].concat(chain.filter((i) => i !== uid));
  46. await changeProfileChain(newChain);
  47. mutate((conf = {}) => ({ ...conf, chain: newChain }), true);
  48. });
  49. const onMoveEnd = useLockFn(async (uid: string) => {
  50. if (!chain.includes(uid)) return;
  51. const newChain = chain.filter((i) => i !== uid).concat([uid]);
  52. await changeProfileChain(newChain);
  53. mutate((conf = {}) => ({ ...conf, chain: newChain }), true);
  54. });
  55. return (
  56. <Box sx={{ mt: 4 }}>
  57. <Grid container spacing={2}>
  58. {items.map((item) => (
  59. <Grid item xs={12} sm={6} key={item.file}>
  60. <ProfileMore
  61. selected={!!chain.includes(item.uid)}
  62. itemData={item}
  63. onEnable={() => onEnhanceEnable(item.uid)}
  64. onDisable={() => onEnhanceDisable(item.uid)}
  65. onDelete={() => onEnhanceDelete(item.uid)}
  66. onMoveTop={() => onMoveTop(item.uid)}
  67. onMoveEnd={() => onMoveEnd(item.uid)}
  68. onEnhance={onEnhance}
  69. />
  70. </Grid>
  71. ))}
  72. </Grid>
  73. </Box>
  74. );
  75. };
  76. export default EnhancedMode;