change: uniqueIdの指定を必須に

This commit is contained in:
yukineko 2024-04-18 17:10:41 +09:00 committed by mattyatea
parent 0022e11dce
commit 0b784b8f32

View file

@ -42,7 +42,7 @@ export async function migrate(userId: string) {
const keyType = type === 'renote' ? 'quote' : type as keyof NoteKeys; const keyType = type === 'renote' ? 'quote' : type as keyof NoteKeys;
const keyId = type === 'note' ? null : id; const keyId = type === 'note' ? null : id;
const uniqueId = Date.now().toString() + i.toString(); const uniqueId = Date.now().toString() + i.toString();
const newKey = getKey(keyType, uniqueId, false, keyId as string); const newKey = getKey(keyType, uniqueId, keyId as string);
newDrafts[newKey] = { newDrafts[newKey] = {
...drafts[key], ...drafts[key],
uniqueId, uniqueId,
@ -52,52 +52,46 @@ export async function migrate(userId: string) {
delete drafts[key]; delete drafts[key];
} }
await idbSet(`drafts::${userId}`, JSON.stringify(newDrafts)); await idbSet(`drafts::${userId}`, newDrafts);
miLocalStorage.setItem('drafts', JSON.stringify(drafts)); miLocalStorage.setItem('drafts', JSON.stringify(drafts));
} }
function getKey<T extends keyof NoteKeys, U extends boolean>(type: T, uniqueId: string | null, withUniqueId: U, ...args: Parameters<NoteKeys[T]>): U extends true ? { uniqueId: string, key: string } : string { function getKey<T extends keyof NoteKeys>(type: T, uniqueId: string, ...args: Parameters<NoteKeys[T]>) {
const id = uniqueId ?? Date.now(); let key = `${type}:${uniqueId}`;
let key = `${type}:${id}`;
for (const arg of args) { for (const arg of args) {
if (arg != null) key += `:${arg}`; if (arg != null) key += `:${arg}`;
} }
return key;
if (withUniqueId) {
return { uniqueId: id, key } as any;
} else {
return key as any;
}
} }
export async function getAll(userId: string) { export async function getAll(userId: string) {
const drafts = await idbGet(`drafts::${userId}`); const drafts = await idbGet(`drafts::${userId}`);
if (!drafts) return {}; return (drafts ?? {}) as Record<string, NoteDraft | undefined>;
return JSON.parse(drafts) as Record<string, NoteDraft | undefined>;
} }
export async function get<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, ...args: Parameters<NoteKeys[T]>) { export async function get<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string, ...args: Parameters<NoteKeys[T]>) {
const key = getKey(type, uniqueId, false, ...args); const key = getKey(type, uniqueId, ...args);
const draft = await getAll(userId)[key]; const draft = await getAll(userId)[key];
return draft ?? null; return draft ?? null;
} }
export async function set<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, draft: NoteDraft['data'], ...args: Parameters<NoteKeys[T]>) { export async function set<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string, draft: NoteDraft['data'], ...args: Parameters<NoteKeys[T]>) {
const drafts = await getAll(userId); const drafts = await getAll(userId);
const keys = getKey(type, uniqueId, true, ...args); const key = getKey(type, uniqueId, ...args);
drafts[keys.key] = { drafts[key] = {
updatedAt: new Date(), updatedAt: new Date(),
type, type,
uniqueId: uniqueId ?? keys.uniqueId, uniqueId,
auxId: args[0] ?? null, auxId: args[0] ?? null,
data: draft, data: JSON.parse(JSON.stringify(draft)) as NoteDraft['data'],
}; };
await idbSet(`drafts::${userId}`, JSON.stringify(drafts)); console.log(drafts);
await idbSet(`drafts::${userId}`, drafts);
} }
export async function remove<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, ...args: Parameters<NoteKeys[T]>) { export async function remove<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string, ...args: Parameters<NoteKeys[T]>) {
const drafts = await getAll(userId); const drafts = await getAll(userId);
const key = getKey(type, uniqueId, false, ...args); const key = getKey(type, uniqueId, ...args);
delete drafts[key]; delete drafts[key];
await idbSet(`drafts::${userId}`, JSON.stringify(drafts)); await idbSet(`drafts::${userId}`, drafts);
} }