From 91503405b4e58cb9d39c0441d86ff540a27931e6 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 7 Jan 2023 10:13:02 +0900 Subject: [PATCH] refactor(client): typed localStorage --- packages/frontend/src/account.ts | 9 +++--- .../src/components/MkAutocomplete.vue | 3 +- packages/frontend/src/components/MkFolder.vue | 7 +++-- .../frontend/src/components/MkPostForm.vue | 17 +++++----- packages/frontend/src/config.ts | 10 +++--- packages/frontend/src/init.ts | 24 ++++++++------ packages/frontend/src/instance.ts | 5 +-- packages/frontend/src/local-storage.ts | 31 +++++++++++++++++++ packages/frontend/src/navbar.ts | 7 +++-- packages/frontend/src/pages/_error_.vue | 3 +- .../pages/messaging/messaging-room.form.vue | 11 ++++--- packages/frontend/src/pages/scratchpad.vue | 5 +-- .../src/pages/settings/custom-css.vue | 5 +-- .../frontend/src/pages/settings/general.vue | 19 ++++++------ .../frontend/src/pages/settings/index.vue | 5 +-- .../pages/settings/preferences-backups.vue | 19 ++++++------ .../frontend/src/pages/settings/theme.vue | 7 +++-- packages/frontend/src/scripts/aiscript/api.ts | 5 +-- .../frontend/src/scripts/get-note-menu.ts | 3 +- packages/frontend/src/scripts/idb-proxy.ts | 6 ++-- packages/frontend/src/scripts/theme.ts | 5 +-- packages/frontend/src/store.ts | 17 +++++++--- packages/frontend/src/theme-store.ts | 14 ++++++--- packages/frontend/src/ui/classic.vue | 5 +-- packages/frontend/src/ui/universal.vue | 3 +- 25 files changed, 157 insertions(+), 88 deletions(-) create mode 100644 packages/frontend/src/local-storage.ts diff --git a/packages/frontend/src/account.ts b/packages/frontend/src/account.ts index 0e991cdfb..93916ccf2 100644 --- a/packages/frontend/src/account.ts +++ b/packages/frontend/src/account.ts @@ -6,12 +6,13 @@ import { del, get, set } from '@/scripts/idb-proxy'; import { apiUrl } from '@/config'; import { waiting, api, popup, popupMenu, success, alert } from '@/os'; import { unisonReload, reloadChannel } from '@/scripts/unison-reload'; +import { miLocalStorage } from './local-storage'; // TODO: 他のタブと永続化されたstateを同期 type Account = misskey.entities.MeDetailed; -const accountData = localStorage.getItem('account'); +const accountData = miLocalStorage.getItem('account'); // TODO: 外部からはreadonlyに export const $i = accountData ? reactive(JSON.parse(accountData) as Account) : null; @@ -21,7 +22,7 @@ export const iAmAdmin = $i != null && $i.isAdmin; export async function signout() { waiting(); - localStorage.removeItem('account'); + miLocalStorage.removeItem('account'); await removeAccount($i.id); @@ -119,7 +120,7 @@ export function updateAccount(accountData) { for (const [key, value] of Object.entries(accountData)) { $i[key] = value; } - localStorage.setItem('account', JSON.stringify($i)); + miLocalStorage.setItem('account', JSON.stringify($i)); } export function refreshAccount() { @@ -130,7 +131,7 @@ export async function login(token: Account['token'], redirect?: string) { waiting(); if (_DEV_) console.log('logging as token ', token); const me = await fetchAccount(token); - localStorage.setItem('account', JSON.stringify(me)); + miLocalStorage.setItem('account', JSON.stringify(me)); document.cookie = `token=${token}; path=/; max-age=31536000`; // bull dashboardの認証とかで使う await addAccount(me.id, token); diff --git a/packages/frontend/src/components/MkAutocomplete.vue b/packages/frontend/src/components/MkAutocomplete.vue index 08e2c29de..8ed60bc5d 100644 --- a/packages/frontend/src/components/MkAutocomplete.vue +++ b/packages/frontend/src/components/MkAutocomplete.vue @@ -46,6 +46,7 @@ import { defaultStore } from '@/store'; import { emojilist } from '@/scripts/emojilist'; import { instance } from '@/instance'; import { i18n } from '@/i18n'; +import { miLocalStorage } from '@/local-storage'; type EmojiDef = { emoji: string; @@ -208,7 +209,7 @@ function exec() { } } else if (props.type === 'hashtag') { if (!props.q || props.q === '') { - hashtags.value = JSON.parse(localStorage.getItem('hashtags') || '[]'); + hashtags.value = JSON.parse(miLocalStorage.getItem('hashtags') || '[]'); fetching.value = false; } else { const cacheKey = `autocomplete:hashtag:${props.q}`; diff --git a/packages/frontend/src/components/MkFolder.vue b/packages/frontend/src/components/MkFolder.vue index 5a406c863..dc10c7d3f 100644 --- a/packages/frontend/src/components/MkFolder.vue +++ b/packages/frontend/src/components/MkFolder.vue @@ -25,8 +25,9 @@