Skip to content

Commit 0cb1b38

Browse files
brc-ddkiaking
andauthored
fix: reduce unnecessary modularity, make useSetupApp SSR compatible, handle fallbacks properly, don't use aliased imports in distributed code, rename useBrowserLang to getBrowserLang (#646)
Co-authored-by: Kia King Ishii <[email protected]>
1 parent e0def5f commit 0cb1b38

File tree

6 files changed

+22
-58
lines changed

6 files changed

+22
-58
lines changed

lib/components/SDescText.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
2-
import { useLinkifyIt } from 'sefirot/composables/Markdown'
32
import { computed } from 'vue'
3+
import { useLinkifyIt } from '../composables/Markdown'
44
import { useHasSlotContent } from '../composables/Utils'
55
import SDescEmpty from './SDescEmpty.vue'
66

lib/composables/Api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useHttpConfig } from 'sefirot/stores/HttpConfig'
21
import { type Ref, type WatchSource, ref, watch } from 'vue'
32
import { Http } from '../http/Http'
3+
import { useHttpConfig } from '../stores/HttpConfig'
44
import { tryOnMounted } from './Utils'
55

66
export interface Query<Data = any> {

lib/composables/App.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import { type HttpOptions } from 'sefirot/stores/HttpConfig'
2-
import { useSetupHttp } from './Http'
3-
import { type HasLang, useSetupLang } from './Lang'
4-
import { type HasTheme, useSetupTheme } from './Theme'
1+
import { type HttpOptions, useHttpConfig } from '../stores/HttpConfig'
2+
import { type Lang, getBrowserLang, provideLang } from './Lang'
3+
import { type Theme, useTheme } from './Theme'
54

6-
export interface SetupAppUser extends HasLang, HasTheme {}
5+
export interface SetupAppUser {
6+
lang?: Lang
7+
theme?: Theme
8+
}
79

810
export interface SetupAppOptions {
911
http?: HttpOptions
1012
}
1113

1214
export function useSetupApp(): (user?: SetupAppUser | null, options?: SetupAppOptions) => void {
13-
const setupLang = useSetupLang()
14-
const setupTheme = useSetupTheme()
15-
const setupHttp = useSetupHttp()
15+
const theme = useTheme()
16+
const httpConfig = useHttpConfig()
1617

1718
return (user, options) => {
18-
setupLang(user)
19-
setupTheme(user)
20-
setupHttp(user, options?.http)
19+
const lang = user?.lang ?? getBrowserLang()
20+
provideLang(lang)
21+
if (user?.theme) { theme.value = user.theme }
22+
httpConfig.apply({ lang, ...options?.http })
2123
}
2224
}

lib/composables/Http.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/composables/Lang.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,8 @@ export interface TransMessages<T> {
1111
ja: T
1212
}
1313

14-
export interface HasLang {
15-
lang: Lang
16-
}
17-
1814
export const SefirotLangKey: InjectionKey<Lang> = Symbol.for('sefirot-lang-key')
1915

20-
export function useSetupLang(): (user?: HasLang | null) => void {
21-
const browserLang = useBrowserLang()
22-
23-
return (user) => {
24-
provideLang(user?.lang ?? browserLang)
25-
}
26-
}
27-
2816
export function provideLang(lang: Lang) {
2917
getCurrentInstance()!.appContext.app._context.provides[SefirotLangKey] = lang
3018
}
@@ -33,12 +21,16 @@ export function useLang(): Lang {
3321
return inject(SefirotLangKey, 'en')
3422
}
3523

36-
export function useBrowserLang(): Lang {
37-
const lang = navigator.language
38-
24+
export function getBrowserLang(): Lang {
25+
const lang = String(globalThis.navigator?.language || 'en')
3926
return lang.split('-')[0] === 'ja' ? 'ja' : 'en'
4027
}
4128

29+
/** @deprecated use `getBrowserLang` instead */
30+
export function useBrowserLang(): Lang {
31+
return getBrowserLang()
32+
}
33+
4234
export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
4335
const lang = useLang()
4436

lib/composables/Theme.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ import { type WritableComputedRef, computed } from 'vue'
33

44
export type Theme = 'light' | 'dark'
55

6-
export interface HasTheme {
7-
theme: Theme
8-
}
9-
10-
export function useSetupTheme(): (user?: HasTheme | null) => void {
11-
const theme = useTheme()
12-
13-
return (user) => {
14-
theme.value = user?.theme ?? 'light'
15-
}
16-
}
17-
186
export function useTheme(): WritableComputedRef<Theme> {
197
const _isDark = useDark()
208

0 commit comments

Comments
 (0)