import { useRef, useEffect, useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { Table, Modal, Popconfirm, Button, Tooltip, Tag, message } from 'antd'; import { QuestionCircleOutlined } from '@ant-design/icons'; import { invoke } from '@tauri-apps/api'; import useJson from '@/hooks/useJson'; import useData from '@/hooks/useData'; import useColumns from '@/hooks/useColumns'; import FilePath from '@/components/FilePath'; import { CHAT_AWESOME_JSON } from '@/utils'; import { useTableRowSelection, TABLE_PAGINATION } from '@/hooks/useTable'; import { awesomeColumns } from './config'; import AwesomeForm from './Form'; export default function Awesome() { const formRef = useRef(null); const [isVisible, setVisible] = useState(false); const { opData, opInit, opAdd, opReplace, opReplaceItems, opRemove, opRemoveItems, opSafeKey } = useData([]); const { columns, ...opInfo } = useColumns(awesomeColumns()); const { rowSelection, selectedRowIDs, rowReset } = useTableRowSelection(); const { json, updateJson } = useJson(CHAT_AWESOME_JSON); const selectedItems = rowSelection.selectedRowKeys || []; useEffect(() => { if (!json || json.length <= 0) return; opInit(json); }, [json?.length]); useEffect(() => { if (!opInfo.opType) return; if (['edit', 'new'].includes(opInfo.opType)) { setVisible(true); } if (['delete'].includes(opInfo.opType)) { const data = opRemove(opInfo?.opRecord?.[opSafeKey]); updateJson(data); opInfo.resetRecord(); } }, [opInfo.opType, formRef]); const hide = () => { setVisible(false); opInfo.resetRecord(); }; useEffect(() => { if (opInfo.opType === 'enable') { const data = opReplace(opInfo?.opRecord?.[opSafeKey], opInfo?.opRecord); updateJson(data); } }, [opInfo.opTime]); const handleDelete = () => { const data = opRemoveItems(selectedRowIDs); updateJson(data); rowReset(); message.success('All selected URLs have been deleted'); }; const handleOk = () => { formRef.current?.form?.validateFields().then(async (vals: Record) => { let idx = opData.findIndex((i) => i.url === vals.url); if (vals.url === opInfo?.opRecord?.url) { idx = -1; } if (idx === -1) { if (opInfo.opType === 'new') { const data = opAdd(vals); await updateJson(data); opInit(data); message.success('Data added successfully'); } if (opInfo.opType === 'edit') { const data = opReplace(opInfo?.opRecord?.[opSafeKey], vals); await updateJson(data); message.success('Data updated successfully'); } hide(); } else { const data = opData[idx]; message.error(
{data.title}: {data.url}
This URL already exists, please edit it and try again.
, ); } }); }; const handleEnable = (isEnable: boolean) => { const data = opReplaceItems(selectedRowIDs, { enable: isEnable }); updateJson(data); }; const handlePreview = () => { invoke('wa_window', { label: 'awesome_preview', url: 'index.html?type=preview', title: 'Preview Dashboard', }); }; const modalTitle = `${{ new: 'Create', edit: 'Edit' }[opInfo.opType]} URL`; return (
{selectedItems.length > 0 && ( <> Selected {selectedItems.length} items )}
); } const PreviewTip = () => { const go = useNavigate(); const handleGo = (v: string) => { go(`/settings?type=${v}`); }; return ( Click the button to preview, and in Settings you can set a single URL or Dashboard as the default window for the app.
handleGo('main_window')} color="blue"> Main Window {'or '} handleGo('tray_window')} color="blue"> SystemTray Window } >
); };