2024-02-14 14:09:07 +01:00
|
|
|
import { useThemeOverrideState } from '@/composables/theming'
|
|
|
|
|
2023-11-25 20:14:12 +01:00
|
|
|
// AUTH
|
|
|
|
|
2023-07-28 17:30:56 +02:00
|
|
|
export const useIsLoggedIn = () => {
|
|
|
|
const isLoggedIn = useState<boolean>(
|
|
|
|
'isLoggedIn',
|
|
|
|
() => localStorage.getItem('isLoggedIn') === 'true',
|
|
|
|
)
|
|
|
|
|
|
|
|
return computed({
|
|
|
|
get: () => isLoggedIn.value,
|
|
|
|
set: (value) => {
|
|
|
|
isLoggedIn.value = value
|
|
|
|
localStorage.setItem('isLoggedIn', value.toString())
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-25 20:14:12 +01:00
|
|
|
// LOCALE
|
|
|
|
|
|
|
|
const usePreferedLocaleState = () =>
|
|
|
|
useState<string>(
|
|
|
|
'preferedLocale',
|
|
|
|
() => localStorage.getItem('preferedLocale') || 'auto',
|
|
|
|
)
|
|
|
|
|
|
|
|
export const usePreferedLocale = () => {
|
|
|
|
const preferedLocale = usePreferedLocaleState()
|
|
|
|
const { locale, getBrowserLocale, defaultLocale } = useNuxtApp().$i18n
|
|
|
|
|
|
|
|
return computed({
|
|
|
|
get: () => preferedLocale.value,
|
|
|
|
set: (value) => {
|
|
|
|
preferedLocale.value = value
|
|
|
|
localStorage.setItem('preferedLocale', value)
|
|
|
|
|
|
|
|
if (value === 'auto') {
|
|
|
|
const browserLocale = getBrowserLocale()
|
|
|
|
value = browserLocale || defaultLocale
|
|
|
|
}
|
|
|
|
|
|
|
|
locale.value = value
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-25 21:28:41 +01:00
|
|
|
// THEME
|
|
|
|
|
|
|
|
const usePreferedThemeState = () =>
|
|
|
|
useState<string>(
|
|
|
|
'preferedTheme',
|
|
|
|
() => localStorage.getItem('preferedTheme') || 'auto',
|
|
|
|
)
|
|
|
|
|
|
|
|
export const usePreferedTheme = async () => {
|
|
|
|
const preferedTheme = usePreferedThemeState()
|
|
|
|
const colorMode = useColorMode()
|
|
|
|
const settings = await useSettings()
|
|
|
|
|
|
|
|
return computed({
|
|
|
|
get: () => preferedTheme.value,
|
|
|
|
set: (value) => {
|
|
|
|
preferedTheme.value = value
|
|
|
|
localStorage.setItem('preferedTheme', value)
|
|
|
|
|
|
|
|
if (value === 'auto') {
|
|
|
|
value = settings.value.portal_theme
|
|
|
|
}
|
|
|
|
|
|
|
|
colorMode.preference = value
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:24:04 +02:00
|
|
|
export const useRedirectUrl = () =>
|
|
|
|
useState<string | null>('redirectUrl', () => null)
|
2023-09-04 16:38:54 +02:00
|
|
|
|
2023-11-30 15:59:41 +01:00
|
|
|
export const useQueryMsg = () => useState<string | null>('queryMsg', () => null)
|
|
|
|
|
2023-09-04 16:38:54 +02:00
|
|
|
// SETTINGS
|
|
|
|
|
2024-02-09 15:28:40 +01:00
|
|
|
type AppsSettings = Record<
|
|
|
|
string,
|
|
|
|
{
|
|
|
|
label: string
|
|
|
|
url: string
|
|
|
|
description?: Record<string, string>
|
|
|
|
logo?: string
|
|
|
|
}
|
|
|
|
>
|
|
|
|
|
2023-09-04 16:38:54 +02:00
|
|
|
export interface Settings {
|
|
|
|
domain: string
|
|
|
|
public: boolean
|
2024-02-09 15:28:40 +01:00
|
|
|
portal_logo?: { is: 'img' | 'svg'; src: string }
|
2023-09-04 16:38:54 +02:00
|
|
|
portal_theme: string
|
2024-01-20 14:37:35 +01:00
|
|
|
portal_title?: string
|
2024-01-20 14:38:38 +01:00
|
|
|
search_engine?: string
|
|
|
|
search_engine_name?: string
|
2023-09-04 16:38:54 +02:00
|
|
|
show_other_domains_apps: 0 | 1
|
2023-11-23 16:01:49 +01:00
|
|
|
portal_user_intro: string
|
|
|
|
portal_public_intro: string
|
2024-02-09 15:28:40 +01:00
|
|
|
apps: AppsSettings
|
2023-09-04 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const useSettingsState = () => useState<Settings>('settings')
|
|
|
|
|
|
|
|
export const useSettings = async () => {
|
|
|
|
const settings = useSettingsState()
|
|
|
|
|
|
|
|
if (!settings.value) {
|
|
|
|
const { data } = await useApi<Settings>('/public')
|
2024-02-09 02:22:23 +01:00
|
|
|
|
2024-02-09 15:28:40 +01:00
|
|
|
const logo = data.value!.portal_logo as unknown as string | undefined
|
2024-02-09 02:22:23 +01:00
|
|
|
if (logo) {
|
|
|
|
const src = `https://${data.value.domain}/yunohost/sso/customassets/${logo}`
|
2024-02-09 15:28:40 +01:00
|
|
|
const is = logo.slice(-4) === '.svg' ? 'svg' : 'img'
|
|
|
|
data.value!.portal_logo = { is, src: is === 'svg' ? '' : src }
|
2024-02-09 02:22:23 +01:00
|
|
|
|
|
|
|
if (is === 'svg') {
|
|
|
|
// Query file content to inject inline SVG so that CSS "currentColor" can cascade to it
|
2024-02-09 15:28:40 +01:00
|
|
|
$fetch<Blob>(src)
|
|
|
|
.then((blob) => blob.text())
|
|
|
|
.then((text) => {
|
|
|
|
data.value!.portal_logo!.src = text.replace(
|
|
|
|
'<?xml version="1.0" encoding="utf-8"?>',
|
|
|
|
'',
|
|
|
|
)
|
|
|
|
})
|
2024-02-09 02:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 16:38:54 +02:00
|
|
|
settings.value = data.value as Settings
|
|
|
|
|
|
|
|
const colorMode = useColorMode()
|
|
|
|
colorMode.preference = settings.value.portal_theme
|
2024-02-14 14:09:07 +01:00
|
|
|
|
|
|
|
const themeOverride = useThemeOverrideState()
|
|
|
|
if (themeOverride.value) {
|
|
|
|
useThemeOverride().init(themeOverride.value)
|
|
|
|
}
|
2023-09-04 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
// USER
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
username: string
|
|
|
|
fullname: string
|
|
|
|
mail: string
|
|
|
|
mailalias: string[]
|
|
|
|
mailforward: string[]
|
|
|
|
groups: string[]
|
2024-02-09 15:28:40 +01:00
|
|
|
apps: AppsSettings
|
2023-09-04 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useUserState = () => useState<User | null>('user', () => null)
|
|
|
|
|
|
|
|
export const useUser = async <T extends User | null = User>() => {
|
|
|
|
const user = useUserState()
|
|
|
|
const isLoggedIn = useIsLoggedIn()
|
|
|
|
|
|
|
|
if (!user.value && isLoggedIn.value) {
|
|
|
|
const { data } = await useApi<T>('/me')
|
|
|
|
user.value = data.value
|
|
|
|
}
|
|
|
|
|
|
|
|
return user as Ref<T>
|
2023-09-04 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// APP
|
|
|
|
|
|
|
|
export const useApps = async () => {
|
|
|
|
const isLoggedIn = useIsLoggedIn()
|
|
|
|
const settings = await useSettings()
|
|
|
|
const user = await useUser()
|
|
|
|
|
|
|
|
return computed(() => {
|
|
|
|
return isLoggedIn.value ? user.value?.apps : settings.value.apps
|
|
|
|
})
|
|
|
|
}
|