From a2fcfa3b89acbbeccb16267d43075c39fc81ec9d Mon Sep 17 00:00:00 2001 From: lencx Date: Fri, 13 Jan 2023 23:59:38 +0800 Subject: [PATCH] chore: export --- src-tauri/src/app/cmd.rs | 33 +++++++++++++---------- src-tauri/src/main.rs | 3 ++- src/hooks/useJson.ts | 17 ++++++++++++ src/utils.ts | 1 + src/view/download/config.tsx | 29 +++++++++++++------- src/view/download/index.tsx | 51 +++++++++++++++++++++++++++++------- 6 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 src/hooks/useJson.ts diff --git a/src-tauri/src/app/cmd.rs b/src-tauri/src/app/cmd.rs index b90fad3..c27fd56 100644 --- a/src-tauri/src/app/cmd.rs +++ b/src-tauri/src/app/cmd.rs @@ -185,24 +185,30 @@ pub struct FileMetadata { pub id: String, } -#[command] -pub fn download_list(filename: Option, id: Option) { - info!("download_list"); - let download_path = chat_root().join("chat.download.json"); +#[tauri::command] +pub fn get_download_list(pathname: &str) -> (Vec, PathBuf) { + info!("get_download_list: {}", pathname); + let download_path = chat_root().join(PathBuf::from(pathname)); let content = fs::read_to_string(&download_path).unwrap_or_else(|err| { info!("download_list_error: {}", err); fs::write(&download_path, "[]").unwrap(); "[]".to_string() }); - let mut list = serde_json::from_str::>(&content) - .unwrap_or_else(|err| { - info!("download_list_parse_error: {}", err); - vec![] - }); + let list = serde_json::from_str::>(&content).unwrap_or_else(|err| { + info!("download_list_parse_error: {}", err); + vec![] + }); - let list2 = &list; + (list, download_path) +} + +#[command] +pub fn download_list(pathname: &str, filename: Option, id: Option) { + info!("download_list: {}", pathname); + let data = get_download_list(pathname); + let mut list = vec![]; let mut my_hashmap = HashMap::new(); - utils::vec_to_hashmap(list2.clone().into_iter(), "id", &mut my_hashmap); + utils::vec_to_hashmap(data.0.into_iter(), "id", &mut my_hashmap); for entry in WalkDir::new(utils::chat_root().join("download")) .into_iter() @@ -236,15 +242,14 @@ pub fn download_list(filename: Option, id: Option) { } } - dbg!(&list); - + // dbg!(&list); list.sort_by(|a, b| { let a1 = a.get("created").unwrap().as_u64().unwrap(); let b1 = b.get("created").unwrap().as_u64().unwrap(); a1.cmp(&b1).reverse() }); - fs::write(download_path, serde_json::to_string_pretty(&list).unwrap()).unwrap(); + fs::write(data.1, serde_json::to_string_pretty(&list).unwrap()).unwrap(); } #[command] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 08ddcf3..8c84394 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -30,7 +30,7 @@ async fn main() { trace: Color::Cyan, }; - cmd::download_list(None, None); + cmd::download_list("chat.download.json", None, None); let chat_conf = ChatConfJson::get_chat_conf(); @@ -76,6 +76,7 @@ async fn main() { cmd::dalle2_window, cmd::cmd_list, cmd::download_list, + cmd::get_download_list, fs_extra::metadata, ]) .setup(setup::init) diff --git a/src/hooks/useJson.ts b/src/hooks/useJson.ts new file mode 100644 index 0000000..a06873a --- /dev/null +++ b/src/hooks/useJson.ts @@ -0,0 +1,17 @@ +import { useState } from 'react'; + +import { readJSON } from '@/utils'; +import useInit from '@/hooks/useInit'; + +export default function useJson(file: string) { + const [json, setData] = useState(); + + const refreshJson = async () => { + const data = await readJSON(file); + setData(data); + }; + + useInit(refreshJson); + + return { json, refreshJson }; +} diff --git a/src/utils.ts b/src/utils.ts index e983604..09a0648 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,6 +4,7 @@ import dayjs from 'dayjs'; export const CHAT_MODEL_JSON = 'chat.model.json'; export const CHAT_MODEL_CMD_JSON = 'chat.model.cmd.json'; +export const CHAT_DOWNLOAD_JSON = 'chat.download.json'; export const CHAT_PROMPTS_CSV = 'chat.prompts.csv'; export const GITHUB_PROMPTS_CSV_URL = 'https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv'; export const DISABLE_AUTO_COMPLETE = { diff --git a/src/view/download/config.tsx b/src/view/download/config.tsx index c164b11..dc915f4 100644 --- a/src/view/download/config.tsx +++ b/src/view/download/config.tsx @@ -1,28 +1,37 @@ -import { Switch, Tag, Tooltip, Space, Popconfirm } from 'antd'; +import { Tag, Space, Popconfirm } from 'antd'; + +import { fmtDate } from '@/utils'; + +const colorMap: any = { + pdf: 'blue', + png: 'orange', +} export const syncColumns = () => [ { title: 'Name', dataIndex: 'name', fixed: 'left', - // width: 120, key: 'name', }, { - title: 'Type', - dataIndex: 'type', - key: 'type', - render: () => { - return {}; - } - // width: 200, + title: 'Extension', + dataIndex: 'ext', + key: 'ext', + render: (v: string) => {v}, + }, + { + title: 'Created', + dataIndex: 'created', + key: 'created', + render: fmtDate, }, { title: 'Action', render: (_: any, row: any, actions: any) => { return ( - View + actions.setRecord(row, 'view')}>View actions.setRecord(row, 'delete')} diff --git a/src/view/download/index.tsx b/src/view/download/index.tsx index 03ce0d2..324cc98 100644 --- a/src/view/download/index.tsx +++ b/src/view/download/index.tsx @@ -1,27 +1,52 @@ -import { useState } from 'react'; -import { Table } from 'antd'; -import { path, shell } from '@tauri-apps/api'; +import { useEffect, useState } from 'react'; +import { Table, Modal } from 'antd'; +import { path, shell, fs } from '@tauri-apps/api'; import useInit from '@/hooks/useInit'; +import useJson from '@/hooks/useJson'; import useColumns from '@/hooks/useColumns'; import useTable, { TABLE_PAGINATION } from '@/hooks/useTable'; -import { chatRoot, readJSON } from '@/utils'; +import { chatRoot, CHAT_DOWNLOAD_JSON } from '@/utils'; import { syncColumns } from './config'; import './index.scss'; +function renderFile(buff: Uint8Array, type: string) { + const renderType = { + pdf: 'application/pdf', + png: 'image/png', + }[type]; + return URL.createObjectURL(new Blob([buff], { type: renderType })); +} + export default function SyncPrompts() { const { rowSelection, selectedRowIDs } = useTable(); - const [downloadPath, setDownloadPath] = useState(''); - const [downloadData, setDownloadData] = useState([]); const { columns, ...opInfo } = useColumns(syncColumns()); + const [downloadPath, setDownloadPath] = useState(''); + const { json } = useJson(CHAT_DOWNLOAD_JSON); + const [source, setSource] = useState(''); + const [isVisible, setVisible] = useState(false); useInit(async () => { const file = await path.join(await chatRoot(), 'chat.download.json'); setDownloadPath(file); - const data = await readJSON(file, { isRoot: true, isList: true }); - setDownloadData(data); }); + useEffect(() => { + if (!opInfo.opType) return; + (async () => { + const record = opInfo?.opRecord; + const isImg = ['png'].includes(record?.ext); + const file = await path.join(await chatRoot(), 'download', isImg ? 'img' : record?.ext, `${record?.id}.${record?.ext}`); + if (opInfo.opType === 'view') { + const data = await fs.readBinaryFile(file); + const sourceData = renderFile(data, record?.ext); + setSource(sourceData); + setVisible(true); + } + opInfo.resetRecord(); + })() + }, [opInfo.opType]) + return (
@@ -33,10 +58,18 @@ export default function SyncPrompts() { rowKey="name" columns={columns} scroll={{ x: 'auto' }} - dataSource={downloadData} + dataSource={json} rowSelection={rowSelection} pagination={TABLE_PAGINATION} /> + setVisible(false)} + footer={false} + destroyOnClose + > + +
) } \ No newline at end of file