195 lines
6.7 KiB
TypeScript
195 lines
6.7 KiB
TypeScript
import { readonly, shallowRef } from 'vue'
|
|
import { fileApi } from '@/api/file'
|
|
/**
|
|
* Purpose: ShopVO supports useShopTheme.ts by holding or executing the shop vo page or API role.
|
|
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
import { shopApi, type ShopVO } from '@/api/shop'
|
|
import {
|
|
applyShopTheme,
|
|
DEFAULT_SHOP_THEME,
|
|
resolveShopTheme,
|
|
SHOP_THEME_CHANGED_EVENT,
|
|
type ResolvedShopTheme
|
|
} from '@/utils/shop-theme'
|
|
|
|
type ShopThemeMode = 'auto' | 'admin' | 'customer'
|
|
|
|
export interface RefreshShopThemeOptions {
|
|
mode?: ShopThemeMode
|
|
shopId?: string | number | null
|
|
}
|
|
|
|
/**
|
|
* Purpose: currentTheme supports useShopTheme.ts by holding or executing the current theme page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const currentTheme = shallowRef<ResolvedShopTheme>(DEFAULT_SHOP_THEME)
|
|
let cleanupThemeSync: (() => void) | undefined
|
|
/**
|
|
* Purpose: refreshSequence supports useShopTheme.ts by holding or executing the refresh sequence page or API role.
|
|
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
let refreshSequence = 0
|
|
|
|
applyShopTheme(DEFAULT_SHOP_THEME)
|
|
|
|
/**
|
|
* Purpose: useShopTheme supports useShopTheme.ts by holding or executing the use shop theme page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
export function useShopTheme() {
|
|
return {
|
|
currentTheme: readonly(currentTheme),
|
|
refreshShopTheme,
|
|
resetShopTheme,
|
|
setupShopThemeSync
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purpose: refreshShopTheme supports useShopTheme.ts by holding or executing the refresh shop theme page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
async function refreshShopTheme(options: RefreshShopThemeOptions = {}) {
|
|
const mode = resolveThemeMode(options.mode)
|
|
const token = readToken(mode)
|
|
const userInfo = readStoredUserInfo(mode)
|
|
const shopId = options.shopId === undefined ? userInfo?.shopId : options.shopId
|
|
|
|
if (!token || !shopId) {
|
|
resetShopTheme()
|
|
return
|
|
}
|
|
|
|
const sequence = ++refreshSequence
|
|
try {
|
|
const shop = await shopApi.getShopById(String(shopId))
|
|
if (sequence !== refreshSequence) {
|
|
return
|
|
}
|
|
await applyThemeFromShop(shop)
|
|
} catch {
|
|
if (sequence === refreshSequence) {
|
|
resetShopTheme()
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purpose: resetShopTheme supports useShopTheme.ts by holding or executing the reset shop theme page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function resetShopTheme() {
|
|
refreshSequence += 1
|
|
currentTheme.value = DEFAULT_SHOP_THEME
|
|
applyShopTheme(DEFAULT_SHOP_THEME)
|
|
}
|
|
|
|
/**
|
|
* Purpose: setupShopThemeSync supports useShopTheme.ts by holding or executing the setup shop theme sync page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function setupShopThemeSync() {
|
|
if (cleanupThemeSync || typeof window === 'undefined') {
|
|
return cleanupThemeSync || (() => {})
|
|
}
|
|
|
|
const handleThemeEvent = () => {
|
|
void refreshShopTheme()
|
|
}
|
|
const handleStorage = (event: StorageEvent) => {
|
|
if ([
|
|
'accessToken',
|
|
'userInfo',
|
|
'refreshToken',
|
|
'adminAccessToken',
|
|
'adminRefreshToken',
|
|
'adminUserInfo'
|
|
].includes(event.key || '')) {
|
|
void refreshShopTheme()
|
|
}
|
|
}
|
|
|
|
window.addEventListener(SHOP_THEME_CHANGED_EVENT, handleThemeEvent)
|
|
window.addEventListener('storage', handleStorage)
|
|
window.addEventListener('focus', handleThemeEvent)
|
|
|
|
cleanupThemeSync = () => {
|
|
window.removeEventListener(SHOP_THEME_CHANGED_EVENT, handleThemeEvent)
|
|
window.removeEventListener('storage', handleStorage)
|
|
window.removeEventListener('focus', handleThemeEvent)
|
|
cleanupThemeSync = undefined
|
|
}
|
|
return cleanupThemeSync
|
|
}
|
|
|
|
/**
|
|
* Purpose: applyThemeFromShop supports useShopTheme.ts by holding or executing the apply theme from shop page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
async function applyThemeFromShop(shop: ShopVO) {
|
|
const theme = resolveShopTheme(shop)
|
|
currentTheme.value = theme
|
|
const backgroundImageUrl = await resolveBackgroundImageUrl(theme.backgroundImageFileId)
|
|
applyShopTheme(theme, backgroundImageUrl)
|
|
}
|
|
|
|
/**
|
|
* Purpose: resolveBackgroundImageUrl supports useShopTheme.ts by holding or executing the resolve background image url page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
async function resolveBackgroundImageUrl(fileId?: string) {
|
|
if (!fileId) {
|
|
return null
|
|
}
|
|
try {
|
|
return await fileApi.getSignedUrl(fileId, 120) as unknown as string
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purpose: readToken supports useShopTheme.ts by holding or executing the read token page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function resolveThemeMode(mode: ShopThemeMode = 'auto'): Exclude<ShopThemeMode, 'auto'> {
|
|
if (mode !== 'auto') {
|
|
return mode
|
|
}
|
|
if (typeof window !== 'undefined' && window.location.pathname.startsWith('/admin')) {
|
|
return 'admin'
|
|
}
|
|
return 'customer'
|
|
}
|
|
|
|
function readToken(mode: Exclude<ShopThemeMode, 'auto'>) {
|
|
if (typeof window === 'undefined') {
|
|
return ''
|
|
}
|
|
if (mode === 'admin') {
|
|
return localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken') || ''
|
|
}
|
|
return localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken') || ''
|
|
}
|
|
|
|
/**
|
|
* Purpose: readStoredUserInfo supports useShopTheme.ts by holding or executing the read stored user info page or API role.
|
|
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
function readStoredUserInfo(mode: Exclude<ShopThemeMode, 'auto'>): { shopId?: string | number } | null {
|
|
if (typeof window === 'undefined') {
|
|
return null
|
|
}
|
|
const raw = localStorage.getItem(mode === 'admin' ? 'adminUserInfo' : 'userInfo')
|
|
if (!raw) {
|
|
return null
|
|
}
|
|
try {
|
|
return JSON.parse(raw) as { shopId?: string | number }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|