chore: sync

This commit is contained in:
lencx
2022-12-21 14:00:42 +08:00
parent 878bb6c265
commit d513a50e27
11 changed files with 186 additions and 23 deletions

19
src/hooks/useData.ts vendored
View File

@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react';
import { v4 } from 'uuid';
const safeKey = Symbol('chat-id');
export const safeKey = Symbol('chat-id');
export default function useData(oData: any[]) {
const [opData, setData] = useState<any[]>([]);
@@ -35,5 +35,20 @@ export default function useData(oData: any[]) {
return nData;
};
return { opSafeKey: safeKey, opInit, opReplace, opAdd, opRemove, opData };
const opReplaceItems = (ids: string[], data: any) => {
const nData = [...opData];
let count = 0;
for (let i = 0; i < nData.length; i++) {
const v = nData[i];
if (ids.includes(v[safeKey])) {
count++;
nData[i] = { ...v, ...data };
}
if (count === ids.length) break;
}
setData(nData);
return nData;
};
return { opSafeKey: safeKey, opInit, opReplace, opAdd, opRemove, opData, opReplaceItems };
}