test.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { useEffect, useRef } from "react";
  2. import { useVerge } from "@/hooks/use-verge";
  3. import { Box, Button, Grid } from "@mui/material";
  4. import {
  5. DndContext,
  6. closestCenter,
  7. KeyboardSensor,
  8. PointerSensor,
  9. useSensor,
  10. useSensors,
  11. DragEndEvent,
  12. } from "@dnd-kit/core";
  13. import {
  14. SortableContext,
  15. sortableKeyboardCoordinates,
  16. } from "@dnd-kit/sortable";
  17. import { useTranslation } from "react-i18next";
  18. import { BasePage } from "@/components/base";
  19. import { TestViewer, TestViewerRef } from "@/components/test/test-viewer";
  20. import { TestItem } from "@/components/test/test-item";
  21. import { emit } from "@tauri-apps/api/event";
  22. const TestPage = () => {
  23. const { t } = useTranslation();
  24. const sensors = useSensors(
  25. useSensor(PointerSensor),
  26. useSensor(KeyboardSensor, {
  27. coordinateGetter: sortableKeyboardCoordinates,
  28. })
  29. );
  30. const { verge, mutateVerge, patchVerge } = useVerge();
  31. // test list
  32. const testList = verge?.test_list ?? [
  33. {
  34. uid: crypto.randomUUID(),
  35. name: "Apple",
  36. url: "https://www.apple.com",
  37. icon: "https://www.apple.com/favicon.ico",
  38. },
  39. {
  40. uid: crypto.randomUUID(),
  41. name: "GitHub",
  42. url: "https://www.github.com",
  43. icon: `<svg width="98" height="96" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" fill="#000000"/></svg>`,
  44. },
  45. {
  46. uid: crypto.randomUUID(),
  47. name: "Google",
  48. url: "https://www.google.com",
  49. icon: `<svg enable-background="new 0 0 48 48" height="48" viewBox="0 0 48 48" width="48" xmlns="http://www.w3.org/2000/svg"><path d="m43.611 20.083h-1.611v-.083h-18v8h11.303c-1.649 4.657-6.08 8-11.303 8-6.627 0-12-5.373-12-12s5.373-12 12-12c3.059 0 5.842 1.154 7.961 3.039l5.657-5.657c-3.572-3.329-8.35-5.382-13.618-5.382-11.045 0-20 8.955-20 20s8.955 20 20 20 20-8.955 20-20c0-1.341-.138-2.65-.389-3.917z" fill="#ffc107"/><path d="m6.306 14.691 6.571 4.819c1.778-4.402 6.084-7.51 11.123-7.51 3.059 0 5.842 1.154 7.961 3.039l5.657-5.657c-3.572-3.329-8.35-5.382-13.618-5.382-7.682 0-14.344 4.337-17.694 10.691z" fill="#ff3d00"/><path d="m24 44c5.166 0 9.86-1.977 13.409-5.192l-6.19-5.238c-2.008 1.521-4.504 2.43-7.219 2.43-5.202 0-9.619-3.317-11.283-7.946l-6.522 5.025c3.31 6.477 10.032 10.921 17.805 10.921z" fill="#4caf50"/><path d="m43.611 20.083h-1.611v-.083h-18v8h11.303c-.792 2.237-2.231 4.166-4.087 5.571.001-.001.002-.001.003-.002l6.19 5.238c-.438.398 6.591-4.807 6.591-14.807 0-1.341-.138-2.65-.389-3.917z" fill="#1976d2"/></svg>`,
  50. },
  51. ];
  52. const onTestListItemChange = (
  53. uid: string,
  54. patch?: Partial<IVergeTestItem>
  55. ) => {
  56. if (patch) {
  57. const newList = testList.map((x) => {
  58. if (x.uid === uid) {
  59. return { ...x, ...patch };
  60. }
  61. return x;
  62. });
  63. mutateVerge({ ...verge, test_list: newList }, false);
  64. } else {
  65. mutateVerge();
  66. }
  67. };
  68. const onDeleteTestListItem = (uid: string) => {
  69. const newList = testList.filter((x) => x.uid !== uid);
  70. patchVerge({ test_list: newList });
  71. mutateVerge({ ...verge, test_list: newList }, false);
  72. };
  73. const reorder = (list: any[], startIndex: number, endIndex: number) => {
  74. const result = Array.from(list);
  75. const [removed] = result.splice(startIndex, 1);
  76. result.splice(endIndex, 0, removed);
  77. return result;
  78. };
  79. const onDragEnd = async (event: DragEndEvent) => {
  80. const { active, over } = event;
  81. if (over) {
  82. if (active.id !== over.id) {
  83. let old_index = testList.findIndex((x) => x.uid === active.id);
  84. let new_index = testList.findIndex((x) => x.uid === over.id);
  85. if (old_index < 0 || new_index < 0) {
  86. return;
  87. }
  88. let newList = reorder(testList, old_index, new_index);
  89. await mutateVerge({ ...verge, test_list: newList }, false);
  90. await patchVerge({ test_list: newList });
  91. }
  92. }
  93. };
  94. useEffect(() => {
  95. if (!verge) return;
  96. if (!verge?.test_list) {
  97. patchVerge({ test_list: testList });
  98. }
  99. }, [verge]);
  100. const viewerRef = useRef<TestViewerRef>(null);
  101. return (
  102. <BasePage
  103. title={t("Test")}
  104. header={
  105. <Box sx={{ mt: 1, display: "flex", alignItems: "center", gap: 1 }}>
  106. <Button
  107. variant="contained"
  108. size="small"
  109. onClick={() => emit("verge://test-all")}
  110. >
  111. {t("Test All")}
  112. </Button>
  113. <Button
  114. variant="contained"
  115. size="small"
  116. onClick={() => viewerRef.current?.create()}
  117. >
  118. {t("New")}
  119. </Button>
  120. </Box>
  121. }
  122. >
  123. <DndContext
  124. sensors={sensors}
  125. collisionDetection={closestCenter}
  126. onDragEnd={onDragEnd}
  127. >
  128. <Box sx={{ mb: 4.5 }}>
  129. <Grid container spacing={{ xs: 1, lg: 1 }}>
  130. <SortableContext
  131. items={testList.map((x) => {
  132. return x.uid;
  133. })}
  134. >
  135. {testList.map((item) => (
  136. <Grid item xs={6} sm={4} md={3} lg={2} key={item.uid}>
  137. <TestItem
  138. id={item.uid}
  139. itemData={item}
  140. onEdit={() => viewerRef.current?.edit(item)}
  141. onDelete={onDeleteTestListItem}
  142. />
  143. </Grid>
  144. ))}
  145. </SortableContext>
  146. </Grid>
  147. </Box>
  148. </DndContext>
  149. <TestViewer ref={viewerRef} onChange={onTestListItemChange} />
  150. </BasePage>
  151. );
  152. };
  153. export default TestPage;