Skip to content

Commit cd288ab

Browse files
committed
feat: add app setup composables
1 parent 7a09b97 commit cd288ab

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

lib/composables/App.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { type HttpOptions } from '../http/Http'
2+
import { useSetupHttp } from './Http'
3+
import { type HasLang, useSetupLang } from './Lang'
4+
import { type HasTheme, useSetupTheme } from './Theme'
5+
6+
export interface SetupAppUser extends HasLang, HasTheme {}
7+
8+
export interface SetupAppOptions {
9+
http?: HttpOptions
10+
}
11+
12+
export function useSetupApp(): (user?: SetupAppUser | null, options?: SetupAppOptions) => void {
13+
const setupLang = useSetupLang()
14+
const setupTheme = useSetupTheme()
15+
const setupHttp = useSetupHttp()
16+
17+
return (user, options) => {
18+
setupLang(user)
19+
setupTheme(user)
20+
setupHttp(user, options?.http)
21+
}
22+
}

lib/composables/Http.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { type Lang, useBrowserLang } from 'sefirot/composables/Lang'
2+
import { Http, type HttpOptions } from 'sefirot/http/Http'
3+
4+
export interface HasLang {
5+
lang: Lang
6+
}
7+
8+
export function useSetupHttp(): (user?: HasLang | null, options?: HttpOptions) => void {
9+
const browserLang = useBrowserLang()
10+
11+
return (user, options = {}) => {
12+
Http.config({
13+
lang: user?.lang ?? browserLang,
14+
...options
15+
})
16+
}
17+
}

lib/composables/Lang.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@ export interface TransMessages<T> {
1111
ja: T
1212
}
1313

14+
export interface HasLang {
15+
lang: Lang
16+
}
17+
1418
export const SefirotLangKey = 'sefirot-lang-key'
1519

20+
export function useSetupLang(): (user?: HasLang | null) => void {
21+
const browserLang = useBrowserLang()
22+
23+
return (user) => {
24+
provideLang(user?.lang ?? browserLang)
25+
}
26+
}
27+
1628
export function provideLang(lang: Lang) {
1729
provide(SefirotLangKey, lang)
1830
}
@@ -23,6 +35,12 @@ export function useLang(): Lang {
2335
return inject(SefirotLangKey, 'en') || 'en'
2436
}
2537

38+
export function useBrowserLang(): Lang {
39+
const lang = navigator.language
40+
41+
return lang.split('-')[0] === 'ja' ? 'ja' : 'en'
42+
}
43+
2644
export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
2745
const lang = useLang()
2846

@@ -32,9 +50,3 @@ export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
3250
t
3351
}
3452
}
35-
36-
export function useBrowserLang(): Lang {
37-
const lang = navigator.language
38-
39-
return lang.split('-')[0] === 'ja' ? 'ja' : 'en'
40-
}

lib/composables/Theme.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useDark } from '@vueuse/core'
2+
import { type WritableComputedRef, computed } from 'vue'
3+
4+
export type Theme = 'light' | 'dark'
5+
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+
18+
export function useTheme(): WritableComputedRef<Theme> {
19+
const _isDark = useDark()
20+
21+
return computed({
22+
get: () => _isDark.value ? 'dark' : 'light',
23+
set: (v) => { _isDark.value = v === 'dark' }
24+
})
25+
}

0 commit comments

Comments
 (0)