mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: chatgpt prompts
This commit is contained in:
35
src/hooks/useData.ts
vendored
Normal file
35
src/hooks/useData.ts
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
const safeKey = Symbol('chat-id');
|
||||
|
||||
export default function useData(oData: any[]) {
|
||||
const [opData, setData] = useState<any[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const nData = oData.map(i => ({ [safeKey]: v4(), ...i }));
|
||||
setData(nData);
|
||||
}, [oData])
|
||||
|
||||
const opAdd = (val: any) => {
|
||||
const v = [val, ...opData];
|
||||
setData(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
const opRemove = (id: string) => {
|
||||
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);
|
||||
nData[idx] = data;
|
||||
setData(nData);
|
||||
return nData;
|
||||
};
|
||||
|
||||
return { opSafeKey: safeKey, opReplace, opAdd, opRemove, opData };
|
||||
}
|
||||
Reference in New Issue
Block a user