Skip to content

Commit 1ca471f

Browse files
committed
refactor(frontend): deviceKindの循環参照を除去
1 parent a93b6c3 commit 1ca471f

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

packages/frontend/src/boot/common.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { updateI18n } from '@/i18n.js';
1515
import { $i, refreshAccount, login } from '@/account.js';
1616
import { defaultStore, ColdDeviceStorage } from '@/store.js';
1717
import { fetchInstance, instance } from '@/instance.js';
18-
import { deviceKind } from '@/scripts/device-kind.js';
18+
import { deviceKind, updateDeviceKind } from '@/scripts/device-kind.js';
1919
import { reloadChannel } from '@/scripts/unison-reload.js';
2020
import { getUrlWithoutLoginId } from '@/scripts/login-id.js';
2121
import { getAccountFromId } from '@/scripts/get-account-from-id.js';
@@ -203,6 +203,10 @@ export async function common(createVue: () => App<Element>) {
203203
}
204204
});
205205

206+
watch(defaultStore.reactiveState.overridedDeviceKind, (kind) => {
207+
updateDeviceKind(kind);
208+
}, { immediate: true });
209+
206210
watch(defaultStore.reactiveState.useBlurEffectForModal, v => {
207211
document.documentElement.style.setProperty('--MI-modalBgFilter', v ? 'blur(4px)' : 'none');
208212
}, { immediate: true });

packages/frontend/src/scripts/device-kind.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
* SPDX-License-Identifier: AGPL-3.0-only
44
*/
55

6-
import { defaultStore } from '@/store.js';
7-
8-
await defaultStore.ready;
6+
export type DeviceKind = 'smartphone' | 'tablet' | 'desktop';
97

108
const ua = navigator.userAgent.toLowerCase();
119
const isTablet = /ipad/.test(ua) || (/mobile|iphone|android/.test(ua) && window.innerWidth > 700);
1210
const isSmartphone = !isTablet && /mobile|iphone|android/.test(ua);
1311

14-
const isIPhone = /iphone|ipod/gi.test(ua) && navigator.maxTouchPoints > 1;
15-
// navigator.platform may be deprecated but this check is still required
16-
const isIPadOS = navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1;
17-
const isIos = /ipad|iphone|ipod/gi.test(ua) && navigator.maxTouchPoints > 1;
12+
export const DEFAULT_DEVICE_KIND: DeviceKind = (
13+
isSmartphone
14+
? 'smartphone'
15+
: isTablet
16+
? 'tablet'
17+
: 'desktop'
18+
);
1819

19-
export const isFullscreenNotSupported = isIPhone || isIos;
20+
export let deviceKind: DeviceKind = DEFAULT_DEVICE_KIND;
2021

21-
export const deviceKind: 'smartphone' | 'tablet' | 'desktop' = defaultStore.state.overridedDeviceKind ? defaultStore.state.overridedDeviceKind
22-
: isSmartphone ? 'smartphone'
23-
: isTablet ? 'tablet'
24-
: 'desktop';
22+
export function updateDeviceKind(kind: DeviceKind | null) {
23+
deviceKind = kind ?? DEFAULT_DEVICE_KIND;
24+
}

packages/frontend/src/store.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import * as Misskey from 'misskey-js';
88
import { hemisphere } from '@@/js/intl-const.js';
99
import lightTheme from '@@/themes/l-light.json5';
1010
import darkTheme from '@@/themes/d-green-lime.json5';
11-
import { miLocalStorage } from './local-storage.js';
1211
import type { SoundType } from '@/scripts/sound.js';
12+
import { DEFAULT_DEVICE_KIND, type DeviceKind } from '@/scripts/device-kind.js';
13+
import { miLocalStorage } from '@/local-storage.js';
1314
import { Storage } from '@/pizzax.js';
1415

1516
interface PostFormAction {
@@ -206,7 +207,7 @@ export const defaultStore = markRaw(new Storage('base', {
206207

207208
overridedDeviceKind: {
208209
where: 'device',
209-
default: null as null | 'smartphone' | 'tablet' | 'desktop',
210+
default: null as DeviceKind | null,
210211
},
211212
serverDisconnectedBehavior: {
212213
where: 'device',
@@ -262,11 +263,11 @@ export const defaultStore = markRaw(new Storage('base', {
262263
},
263264
useBlurEffectForModal: {
264265
where: 'device',
265-
default: !/mobile|iphone|android/.test(navigator.userAgent.toLowerCase()), // 循環参照するのでdevice-kind.tsは参照できない
266+
default: DEFAULT_DEVICE_KIND === 'desktop',
266267
},
267268
useBlurEffect: {
268269
where: 'device',
269-
default: !/mobile|iphone|android/.test(navigator.userAgent.toLowerCase()), // 循環参照するのでdevice-kind.tsは参照できない
270+
default: DEFAULT_DEVICE_KIND === 'desktop',
270271
},
271272
showFixedPostForm: {
272273
where: 'device',

0 commit comments

Comments
 (0)