refactor: indexedDbに移行
This commit is contained in:
parent
5cac3fe517
commit
0022e11dce
|
@ -2,9 +2,13 @@ import * as Misskey from 'misskey-js';
|
||||||
import type { PollEditorModelValue } from '@/components/MkPollEditor.vue';
|
import type { PollEditorModelValue } from '@/components/MkPollEditor.vue';
|
||||||
import type { DeleteScheduleEditorModelValue } from '@/components/MkDeleteScheduleEditor.vue';
|
import type { DeleteScheduleEditorModelValue } from '@/components/MkDeleteScheduleEditor.vue';
|
||||||
import { miLocalStorage } from '@/local-storage.js';
|
import { miLocalStorage } from '@/local-storage.js';
|
||||||
|
import { get as idbGet, set as idbSet } from '@/scripts/idb-proxy.js';
|
||||||
|
|
||||||
export type NoteDraft = {
|
export type NoteDraft = {
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
type: keyof NoteKeys;
|
||||||
|
uniqueId: string;
|
||||||
|
auxId: string | null;
|
||||||
data: {
|
data: {
|
||||||
text: string;
|
text: string;
|
||||||
useCw: boolean;
|
useCw: boolean;
|
||||||
|
@ -17,28 +21,83 @@ export type NoteDraft = {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getAll() {
|
type NoteKeys = {
|
||||||
const drafts = miLocalStorage.getItem('drafts');
|
note: () => unknown,
|
||||||
|
reply: (replyId: string) => unknown,
|
||||||
|
quote: (renoteId: string) => unknown,
|
||||||
|
channel: (channelId: string) => unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function migrate(userId: string) {
|
||||||
|
const raw = miLocalStorage.getItem('drafts');
|
||||||
|
if (!raw) return;
|
||||||
|
|
||||||
|
const drafts = JSON.parse(raw) as Record<string, NoteDraft>;
|
||||||
|
const newDrafts: Record<string, NoteDraft> = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < Object.keys(drafts).length; i++) {
|
||||||
|
const key = Object.keys(drafts)[i];
|
||||||
|
const [type, id] = key.split(':');
|
||||||
|
if (type === 'note' && id !== userId) continue;
|
||||||
|
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);
|
||||||
|
newDrafts[newKey] = {
|
||||||
|
...drafts[key],
|
||||||
|
uniqueId,
|
||||||
|
type: keyType,
|
||||||
|
auxId: keyId,
|
||||||
|
};
|
||||||
|
delete drafts[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
await idbSet(`drafts::${userId}`, JSON.stringify(newDrafts));
|
||||||
|
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 {
|
||||||
|
const id = uniqueId ?? Date.now();
|
||||||
|
let key = `${type}:${id}`;
|
||||||
|
for (const arg of args) {
|
||||||
|
if (arg != null) key += `:${arg}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (withUniqueId) {
|
||||||
|
return { uniqueId: id, key } as any;
|
||||||
|
} else {
|
||||||
|
return key as any;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAll(userId: string) {
|
||||||
|
const drafts = await idbGet(`drafts::${userId}`);
|
||||||
if (!drafts) return {};
|
if (!drafts) return {};
|
||||||
return JSON.parse(drafts) as Record<string, NoteDraft | undefined>;
|
return JSON.parse(drafts) as Record<string, NoteDraft | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get(key: string) {
|
export async function get<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, ...args: Parameters<NoteKeys[T]>) {
|
||||||
const draft = getAll()[key];
|
const key = getKey(type, uniqueId, false, ...args);
|
||||||
|
const draft = await getAll(userId)[key];
|
||||||
return draft ?? null;
|
return draft ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function set(key: string, draft: NoteDraft['data']) {
|
export async function set<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, draft: NoteDraft['data'], ...args: Parameters<NoteKeys[T]>) {
|
||||||
const drafts = getAll();
|
const drafts = await getAll(userId);
|
||||||
drafts[key] = {
|
const keys = getKey(type, uniqueId, true, ...args);
|
||||||
|
drafts[keys.key] = {
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
|
type,
|
||||||
|
uniqueId: uniqueId ?? keys.uniqueId,
|
||||||
|
auxId: args[0] ?? null,
|
||||||
data: draft,
|
data: draft,
|
||||||
};
|
};
|
||||||
miLocalStorage.setItem('drafts', JSON.stringify(drafts));
|
await idbSet(`drafts::${userId}`, JSON.stringify(drafts));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function remove(key: string) {
|
export async function remove<T extends keyof NoteKeys>(type: T, userId: string, uniqueId: string | null, ...args: Parameters<NoteKeys[T]>) {
|
||||||
const drafts = getAll();
|
const drafts = await getAll(userId);
|
||||||
|
const key = getKey(type, uniqueId, false, ...args);
|
||||||
delete drafts[key];
|
delete drafts[key];
|
||||||
miLocalStorage.setItem('drafts', JSON.stringify(drafts));
|
await idbSet(`drafts::${userId}`, JSON.stringify(drafts));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue