DuplicateFile.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import styles from "./DuplicateFile.module.less";
  2. // import { invoke } from "@tauri-apps/api/tauri";
  3. import { open } from "@tauri-apps/api/dialog";
  4. import { Col, Row, Button, message, Table, Select, Space } from "antd";
  5. import { appDataDir } from "@tauri-apps/api/path";
  6. import File from "@/plugins/tauri-plugin-file/file";
  7. import { useEffect, useState } from "react";
  8. // import { SQLite } from "@/plugins/tauri-plugin-sqlite";
  9. // import {select_history_init} from '@/databases/index'
  10. const { Option } = Select;
  11. import {
  12. insertSeletedFileHistory,
  13. insertSearchFiles,
  14. get_info_by_path,
  15. get_all_history,
  16. get_list_by_sourceid,
  17. } from "@/services";
  18. import { insertSearchFilesPasamsType, historyListType } from "@/types/files";
  19. export default function DuplicateFile() {
  20. const [usePath, setUsePath] = useState<string>(
  21. "/Users/sysadmin/code/rust_project/tauri-app/diff_source"
  22. );
  23. const [fileList, setFileList] = useState<insertSearchFilesPasamsType[]>([]);
  24. const [historyList, setHistoryList] = useState<historyListType[]>([]);
  25. const columns = [
  26. {
  27. title: "编号",
  28. dataIndex: "id",
  29. key: "id",
  30. },
  31. {
  32. title: "路径",
  33. dataIndex: "name",
  34. key: "name",
  35. },
  36. {
  37. title: "哈希值",
  38. dataIndex: "hash",
  39. key: "hash",
  40. },
  41. ];
  42. async function sort() {
  43. // 打开本地的系统目录,暂时不支持多选
  44. const selected = await open({
  45. directory: true,
  46. multiple: false,
  47. defaultPath: await appDataDir(),
  48. });
  49. if (selected && selected.length) {
  50. setUsePath(`${selected}`);
  51. // 最多记录 100 条用户操作的历史数据
  52. const files = await File.getAllList(`${selected}`);
  53. console.log(20202020, files);
  54. }
  55. // await invoke("file_sort", { path: selected });
  56. // setFile([...fileStr, await invoke("file_sort", { path: selected })]);
  57. }
  58. // 存储用户的历史选择记录
  59. async function opens() {
  60. const res = await insertSeletedFileHistory(usePath);
  61. fileHistoryListInit();
  62. if (res) {
  63. // return message.error(`${res}`)
  64. }
  65. const [info, msg] = await get_info_by_path(`${usePath}`);
  66. if (!info) {
  67. return message.error(msg);
  68. }
  69. // 最多记录 100 条用户操作的历史数据
  70. const files = await File.getAllList(usePath);
  71. if (files.length) {
  72. files.forEach(async (elm) => {
  73. const [res, msg] = await insertSearchFiles({
  74. // 组装数据
  75. sourceId: (info as any).id,
  76. path: elm,
  77. type: await File.getType(elm),
  78. name: elm,
  79. hash: await File.getHash(elm),
  80. });
  81. // console.log(67, res, msg);
  82. });
  83. }
  84. getProcessedQueryData();
  85. }
  86. useEffect(() => {
  87. fileHistoryListInit();
  88. getProcessedQueryData();
  89. }, []);
  90. // 查询用户历史纪录
  91. async function fileHistoryListInit() {
  92. const res = await get_all_history();
  93. setHistoryList(res);
  94. }
  95. const historyHandleChange = (value: string) => {
  96. // console.log(`selected ${value}`);
  97. setUsePath(value);
  98. };
  99. // 获取处理好的查询数据数据,根据
  100. async function getProcessedQueryData() {
  101. console.log(102, usePath);
  102. let [info, msg1] = await get_info_by_path(`${usePath}`);
  103. if (!info) return;
  104. console.log(104, info);
  105. const [res, msg2] = await get_list_by_sourceid((info as any).id);
  106. console.log(109, res);
  107. if (!res) return;
  108. setFileList(res);
  109. // const res = await get_all_history();
  110. // setHistoryList(res);
  111. }
  112. return (
  113. <div className={styles.DuplicateFileBox}>
  114. <Row>
  115. <Col>
  116. <Button onClick={() => sort()}>选择文件路径</Button>
  117. </Col>
  118. <Col>设置文件路径</Col>
  119. </Row>
  120. <Row>已选择路径:{usePath}</Row>
  121. <Row>
  122. <Select style={{ width: "100%" }} onChange={historyHandleChange}>
  123. {historyList.length > 0 && historyList.map((elm, index) => (
  124. <Option key={index}>{elm.path}</Option>
  125. ))}
  126. </Select>
  127. </Row>
  128. {usePath && (
  129. <Row>
  130. <Button onClick={() => opens()}>开始</Button>
  131. </Row>
  132. )}
  133. {fileList.length > 0 && (
  134. <div>
  135. <br />
  136. <Row>
  137. <Space>
  138. <Button type="link">Link Button</Button>
  139. <Button type="link">Link Button</Button>
  140. </Space>
  141. </Row>
  142. <br />
  143. <Row>
  144. <Table rowKey={"id"} dataSource={fileList} columns={columns} />;
  145. </Row>
  146. </div>
  147. )}
  148. </div>
  149. );
  150. }