chore: add pretty

This commit is contained in:
lencx
2023-01-22 18:18:36 +08:00
parent 1ba356a91f
commit bc39dcdd72
56 changed files with 776 additions and 535 deletions

View File

@@ -15,12 +15,12 @@ export default function useChatModel(key: string, file = CHAT_MODEL_JSON) {
setModelJson(data);
});
const modelSet = async (data: Record<string, any>[]|Record<string, any>) => {
const modelSet = async (data: Record<string, any>[] | Record<string, any>) => {
const oData = clone(modelJson);
oData[key] = data;
await writeJSON(file, oData);
setModelJson(oData);
}
};
return { modelJson, modelSet, modelData: modelJson?.[key] || [] };
}
@@ -40,15 +40,19 @@ export function useCacheModel(file = '') {
await writeJSON(newFile ? newFile : file, data, { isRoot: true });
setModelCacheJson(data);
await modelCacheCmd();
}
};
const modelCacheCmd = async () => {
// Generate the `chat.model.cmd.json` file and refresh the page for the slash command to take effect.
const list = await invoke('cmd_list');
await writeJSON(CHAT_MODEL_CMD_JSON, { name: 'ChatGPT CMD', last_updated: Date.now(), data: list });
await writeJSON(CHAT_MODEL_CMD_JSON, {
name: 'ChatGPT CMD',
last_updated: Date.now(),
data: list,
});
await invoke('window_reload', { label: 'core' });
await invoke('window_reload', { label: 'tray' });
};
return { modelCacheJson, modelCacheSet, modelCacheCmd };
}
}

View File

@@ -5,7 +5,7 @@ import { DISABLE_AUTO_COMPLETE } from '@/utils';
export default function useColumns(columns: any[] = []) {
const [opType, setOpType] = useState('');
const [opRecord, setRecord] = useState<Record<string|symbol, any> | null>(null);
const [opRecord, setRecord] = useState<Record<string | symbol, any> | null>(null);
const [opTime, setNow] = useState<number | null>(null);
const [opExtra, setExtra] = useState<any>(null);
@@ -58,26 +58,26 @@ export const EditRow: FC<EditRowProps> = ({ rowKey, row, actions }) => {
setEdit(true);
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setVal(e.target.value)
setVal(e.target.value);
};
const handleSave = () => {
setEdit(false);
row[rowKey] = val?.trim();
actions?.setRecord(row, 'rowedit')
actions?.setRecord(row, 'rowedit');
};
return isEdit
? (
<Input
value={val}
autoFocus
onChange={handleChange}
{...DISABLE_AUTO_COMPLETE}
onPressEnter={handleSave}
/>
)
: (
<div className='rowedit' onClick={handleEdit}>{val}</div>
);
return isEdit ? (
<Input
value={val}
autoFocus
onChange={handleChange}
{...DISABLE_AUTO_COMPLETE}
onPressEnter={handleSave}
/>
) : (
<div className="rowedit" onClick={handleEdit}>
{val}
</div>
);
};

10
src/hooks/useData.ts vendored
View File

@@ -8,7 +8,7 @@ export default function useData(oData: any[]) {
useEffect(() => {
opInit(oData);
}, [])
}, []);
const opAdd = (val: any) => {
const v = [val, ...opData];
@@ -18,19 +18,19 @@ export default function useData(oData: any[]) {
const opInit = (val: any[] = []) => {
if (!val || !Array.isArray(val)) return;
const nData = val.map(i => ({ [safeKey]: v4(), ...i }));
const nData = val.map((i) => ({ [safeKey]: v4(), ...i }));
setData(nData);
};
const opRemove = (id: string) => {
const nData = opData.filter(i => i[safeKey] !== id);
const nData = opData.filter((i) => i[safeKey] !== id);
setData(nData);
return nData;
};
const opReplace = (id: string, data: any) => {
const nData = [...opData];
const idx = opData.findIndex(v => v[safeKey] === id);
const idx = opData.findIndex((v) => v[safeKey] === id);
nData[idx] = data;
setData(nData);
return nData;
@@ -52,4 +52,4 @@ export default function useData(oData: any[]) {
};
return { opSafeKey: safeKey, opInit, opReplace, opAdd, opRemove, opData, opReplaceItems };
}
}

View File

@@ -8,5 +8,5 @@ export default function useInit(callback: () => void) {
callback();
isInit.current = false;
}
})
}
});
}

View File

@@ -7,14 +7,17 @@ import { safeKey } from '@/hooks/useData';
type rowSelectionOptions = {
key: 'id' | string;
rowType: 'id' | 'row' | 'all';
}
};
export function useTableRowSelection(options: Partial<rowSelectionOptions> = {}) {
const { key = 'id', rowType = 'id' } = options;
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
const [selectedRowIDs, setSelectedRowIDs] = useState<string[]>([]);
const [selectedRows, setSelectedRows] = useState<Record<string|symbol, any>[]>([]);
const [selectedRows, setSelectedRows] = useState<Record<string | symbol, any>[]>([]);
const onSelectChange = (newSelectedRowKeys: React.Key[], newSelectedRows: Record<string|symbol, any>[]) => {
const onSelectChange = (
newSelectedRowKeys: React.Key[],
newSelectedRows: Record<string | symbol, any>[],
) => {
const keys = newSelectedRows.map((i: any) => i[safeKey] || i[key]);
setSelectedRowKeys(newSelectedRowKeys);
if (rowType === 'id') {
@@ -38,11 +41,7 @@ export function useTableRowSelection(options: Partial<rowSelectionOptions> = {})
const rowSelection: TableRowSelection<Record<string, any>> = {
selectedRowKeys,
onChange: onSelectChange,
selections: [
Table.SELECTION_ALL,
Table.SELECTION_INVERT,
Table.SELECTION_NONE,
],
selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT, Table.SELECTION_NONE],
};
return { rowSelection, selectedRowIDs, selectedRows, rowReset };
@@ -55,4 +54,4 @@ export const TABLE_PAGINATION = {
defaultPageSize: 10,
pageSizeOptions: [5, 10, 15, 20],
showTotal: (total: number) => <span>Total {total} items</span>,
};
};