From 0b784b8f32bbf1f714255185f000b7893aa8e819 Mon Sep 17 00:00:00 2001 From: yukineko <27853966+hideki0403@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:10:41 +0900 Subject: [PATCH] =?UTF-8?q?change:=20uniqueId=E3=81=AE=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E3=82=92=E5=BF=85=E9=A0=88=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/note-drafts.ts | 42 +++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/packages/frontend/src/scripts/note-drafts.ts b/packages/frontend/src/scripts/note-drafts.ts index c5f1376deb..958b9ae5dc 100644 --- a/packages/frontend/src/scripts/note-drafts.ts +++ b/packages/frontend/src/scripts/note-drafts.ts @@ -42,7 +42,7 @@ export async function migrate(userId: string) { const keyType = type === 'renote' ? 'quote' : type as keyof NoteKeys; const keyId = type === 'note' ? null : id; 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] = { ...drafts[key], uniqueId, @@ -52,52 +52,46 @@ export async function migrate(userId: string) { delete drafts[key]; } - await idbSet(`drafts::${userId}`, JSON.stringify(newDrafts)); + await idbSet(`drafts::${userId}`, newDrafts); miLocalStorage.setItem('drafts', JSON.stringify(drafts)); } -function getKey(type: T, uniqueId: string | null, withUniqueId: U, ...args: Parameters): U extends true ? { uniqueId: string, key: string } : string { - const id = uniqueId ?? Date.now(); - let key = `${type}:${id}`; +function getKey(type: T, uniqueId: string, ...args: Parameters) { + let key = `${type}:${uniqueId}`; for (const arg of args) { if (arg != null) key += `:${arg}`; } - - if (withUniqueId) { - return { uniqueId: id, key } as any; - } else { - return key as any; - } + return key; } export async function getAll(userId: string) { const drafts = await idbGet(`drafts::${userId}`); - if (!drafts) return {}; - return JSON.parse(drafts) as Record; + return (drafts ?? {}) as Record; } -export async function get(type: T, userId: string, uniqueId: string | null, ...args: Parameters) { - const key = getKey(type, uniqueId, false, ...args); +export async function get(type: T, userId: string, uniqueId: string, ...args: Parameters) { + const key = getKey(type, uniqueId, ...args); const draft = await getAll(userId)[key]; return draft ?? null; } -export async function set(type: T, userId: string, uniqueId: string | null, draft: NoteDraft['data'], ...args: Parameters) { +export async function set(type: T, userId: string, uniqueId: string, draft: NoteDraft['data'], ...args: Parameters) { const drafts = await getAll(userId); - const keys = getKey(type, uniqueId, true, ...args); - drafts[keys.key] = { + const key = getKey(type, uniqueId, ...args); + drafts[key] = { updatedAt: new Date(), type, - uniqueId: uniqueId ?? keys.uniqueId, + uniqueId, 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(type: T, userId: string, uniqueId: string | null, ...args: Parameters) { +export async function remove(type: T, userId: string, uniqueId: string, ...args: Parameters) { const drafts = await getAll(userId); - const key = getKey(type, uniqueId, false, ...args); + const key = getKey(type, uniqueId, ...args); delete drafts[key]; - await idbSet(`drafts::${userId}`, JSON.stringify(drafts)); + await idbSet(`drafts::${userId}`, drafts); }