Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add app setup composables #581

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/composables/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type HttpOptions } from '../http/Http'
import { useSetupHttp } from './Http'
import { type HasLang, useSetupLang } from './Lang'
import { type HasTheme, useSetupTheme } from './Theme'

export interface SetupAppUser extends HasLang, HasTheme {}

export interface SetupAppOptions {
http?: HttpOptions
}

export function useSetupApp(): (user?: SetupAppUser | null, options?: SetupAppOptions) => void {
const setupLang = useSetupLang()
const setupTheme = useSetupTheme()
const setupHttp = useSetupHttp()

return (user, options) => {
setupLang(user)
setupTheme(user)
setupHttp(user, options?.http)
}
}
17 changes: 17 additions & 0 deletions lib/composables/Http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type Lang, useBrowserLang } from 'sefirot/composables/Lang'
import { Http, type HttpOptions } from 'sefirot/http/Http'

export interface HasLang {
lang: Lang
}

export function useSetupHttp(): (user?: HasLang | null, options?: HttpOptions) => void {
const browserLang = useBrowserLang()

return (user, options = {}) => {
Http.config({
lang: user?.lang ?? browserLang,
...options
})
}
}
24 changes: 18 additions & 6 deletions lib/composables/Lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ export interface TransMessages<T> {
ja: T
}

export interface HasLang {
lang: Lang
}

export const SefirotLangKey = 'sefirot-lang-key'

export function useSetupLang(): (user?: HasLang | null) => void {
const browserLang = useBrowserLang()

return (user) => {
provideLang(user?.lang ?? browserLang)
}
}

export function provideLang(lang: Lang) {
provide(SefirotLangKey, lang)
}
Expand All @@ -23,6 +35,12 @@ export function useLang(): Lang {
return inject(SefirotLangKey, 'en') || 'en'
}

export function useBrowserLang(): Lang {
const lang = navigator.language

return lang.split('-')[0] === 'ja' ? 'ja' : 'en'
}

export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
const lang = useLang()

Expand All @@ -32,9 +50,3 @@ export function useTrans<T>(messages: TransMessages<T>): Trans<T> {
t
}
}

export function useBrowserLang(): Lang {
const lang = navigator.language

return lang.split('-')[0] === 'ja' ? 'ja' : 'en'
}
25 changes: 25 additions & 0 deletions lib/composables/Theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useDark } from '@vueuse/core'
import { type WritableComputedRef, computed } from 'vue'

export type Theme = 'light' | 'dark'

export interface HasTheme {
theme: Theme
}

export function useSetupTheme(): (user?: HasTheme | null) => void {
const theme = useTheme()

return (user) => {
theme.value = user?.theme ?? 'light'
}
}

export function useTheme(): WritableComputedRef<Theme> {
const _isDark = useDark()

return computed({
get: () => _isDark.value ? 'dark' : 'light',
set: (v) => { _isDark.value = v === 'dark' }
})
}
Loading