Skip to content

Commit

Permalink
refactor: init.ts周りのeslintエラーと型の修正 (#10157)
Browse files Browse the repository at this point in the history
* refactor/miLocalStorageのメソッドに戻り値追加

* refactor/miLocalStorageのキーとしてdebugがconfig.tsに存在するので追加

* fix/JSON.parseにnullは入らないのでnullの時は分岐させてnullにする

* refactor/修正したファイルの型調整+記法の統一

* fix/型のためにlangがnullの時はhtmlの言語の設定をしない

* refactor/catchで何もしないと警告が出るので修正

* refactor/細かい点の修正

* refactor/変数の二重定義になっていた二重定義になっていたので修正

* refactor/importの整理(通常のimportは最初に処理されるので影響はない想定)

* fix/vueファイルに型を与えてインポート時の型エラーを防ぐ

* refactor/開発環境のみで利用するので,eslintの設定を変更する

* fix/vueの定義を最小限にする

* fallback language to 'en-US'

* remove accounts migration

* fix:vueの型定義ファイルを消す

---------

Co-authored-by: tamaina <[email protected]>
  • Loading branch information
k35o and tamaina authored Apr 13, 2023
1 parent dffefda commit 4634467
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
11 changes: 6 additions & 5 deletions packages/frontend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { miLocalStorage } from "./local-storage";
import { miLocalStorage } from './local-storage';

const address = new URL(location.href);
const siteName = (document.querySelector('meta[property="og:site_name"]') as HTMLMetaElement)?.content;
const siteName = document.querySelector<HTMLMetaElement>('meta[property="og:site_name"]')?.content;

export const host = address.host;
export const hostname = address.hostname;
export const url = address.origin;
export const apiUrl = url + '/api';
export const wsUrl = url.replace('http://', 'ws://').replace('https://', 'wss://') + '/streaming';
export const lang = miLocalStorage.getItem('lang');
export const lang = miLocalStorage.getItem('lang') ?? 'en-US';
export const langs = _LANGS_;
export let locale = JSON.parse(miLocalStorage.getItem('locale'));
const preParseLocale = miLocalStorage.getItem('locale');
export let locale = preParseLocale ? JSON.parse(preParseLocale) : null;
export const version = _VERSION_;
export const instanceName = siteName === 'Misskey' ? host : siteName;
export const ui = miLocalStorage.getItem('ui');
export const debug = miLocalStorage.getItem('debug') === 'true';

export function updateLocale(newLocale) {
export function updateLocale(newLocale): void {
locale = newLocale;
}
45 changes: 17 additions & 28 deletions packages/frontend/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ import 'vite/modulepreload-polyfill';

import '@/style.scss';

//#region account indexedDB migration
import { set } from '@/scripts/idb-proxy';

{
const accounts = miLocalStorage.getItem('accounts');
if (accounts) {
set('accounts', JSON.parse(accounts));
miLocalStorage.removeItem('accounts');
}
}
//#endregion

import { computed, createApp, watch, markRaw, version as vueVersion, defineAsyncComponent } from 'vue';
import { compareVersions } from 'compare-versions';
import JSON5 from 'json5';
Expand All @@ -42,11 +30,11 @@ import { reloadChannel } from '@/scripts/unison-reload';
import { reactionPicker } from '@/scripts/reaction-picker';
import { getUrlWithoutLoginId } from '@/scripts/login-id';
import { getAccountFromId } from '@/scripts/get-account-from-id';
import { deckStore } from './ui/deck/deck-store';
import { miLocalStorage } from './local-storage';
import { claimAchievement, claimedAchievements } from './scripts/achievements';
import { fetchCustomEmojis } from './custom-emojis';
import { mainRouter } from './router';
import { deckStore } from '@/ui/deck/deck-store';
import { miLocalStorage } from '@/local-storage';
import { claimAchievement, claimedAchievements } from '@/scripts/achievements';
import { fetchCustomEmojis } from '@/custom-emojis';
import { mainRouter } from '@/router';

console.info(`Misskey v${version}`);

Expand All @@ -55,7 +43,9 @@ if (_DEV_) {

console.info(`vue ${vueVersion}`);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).$i = $i;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).$store = defaultStore;

window.addEventListener('error', event => {
Expand Down Expand Up @@ -184,7 +174,7 @@ fetchInstanceMetaPromise.then(() => {

try {
await fetchCustomEmojis();
} catch (err) {}
} catch (err) { /* empty */ }

const app = createApp(
new URLSearchParams(window.location.search).has('zen') ? defineAsyncComponent(() => import('@/ui/zen.vue')) :
Expand Down Expand Up @@ -212,20 +202,20 @@ await deckStore.ready;

// https://github.com/misskey-dev/misskey/pull/8575#issuecomment-1114239210
// なぜかinit.tsの内容が2回実行されることがあるため、mountするdivを1つに制限する
const rootEl = (() => {
const rootEl = ((): HTMLElement => {
const MISSKEY_MOUNT_DIV_ID = 'misskey_app';

const currentEl = document.getElementById(MISSKEY_MOUNT_DIV_ID);
const currentRoot = document.getElementById(MISSKEY_MOUNT_DIV_ID);

if (currentEl) {
if (currentRoot) {
console.warn('multiple import detected');
return currentEl;
return currentRoot;
}

const rootEl = document.createElement('div');
rootEl.id = MISSKEY_MOUNT_DIV_ID;
document.body.appendChild(rootEl);
return rootEl;
const root = document.createElement('div');
root.id = MISSKEY_MOUNT_DIV_ID;
document.body.appendChild(root);
return root;
})();

app.mount(rootEl);
Expand Down Expand Up @@ -256,8 +246,7 @@ if (lastVersion !== version) {
popup(defineAsyncComponent(() => import('@/components/MkUpdated.vue')), {}, {}, 'closed');
}
}
} catch (err) {
}
} catch (err) { /* empty */ }
}

await defaultStore.ready;
Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Keys =
'customCss' |
'message_drafts' |
'scratchpad' |
'debug' |
`miux:${string}` |
`ui:folder:${string}` |
`themes:${string}` |
Expand All @@ -32,7 +33,7 @@ type Keys =
'emojis' // DEPRECATED, stored in indexeddb (13.9.0~);

export const miLocalStorage = {
getItem: (key: Keys) => window.localStorage.getItem(key),
setItem: (key: Keys, value: string) => window.localStorage.setItem(key, value),
removeItem: (key: Keys) => window.localStorage.removeItem(key),
getItem: (key: Keys): string | null => window.localStorage.getItem(key),
setItem: (key: Keys, value: string): void => window.localStorage.setItem(key, value),
removeItem: (key: Keys): void => window.localStorage.removeItem(key),
};

0 comments on commit 4634467

Please sign in to comment.