nomadro/apps/miniprogram/tt/src/app.tsx
eric 8c5eb388c1 Scrub BMI medical copy and live-refresh Douyin home A/B + portals.
Rename titles to BMI指数, soften audit-sensitive tips, and subscribe remote config so homepage variant and entrance toggles apply within a few seconds.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-04 19:17:21 +08:00

78 lines
2.1 KiB
TypeScript

import { useEffect } from 'react'
import type { PropsWithChildren } from 'react'
import Taro, { useDidHide, useDidShow, useLaunch } from '@tarojs/taro'
import { flush, trackAppLaunch } from './utils/track'
import {
loadRemoteConfig,
softRefreshRemoteConfig,
startRemoteConfigLiveSync,
subscribeRemoteConfig,
} from './services/remoteConfig'
import {
resolveTtHomeVariantFromConfig,
type NomadroRemoteConfig,
} from './data/remoteConfig.defaults'
import { startH5RemLock } from './utils/h5RemLock'
import { applyH5Theme, getStoredH5Theme } from './utils/h5Theme'
// Taro multi-end: theme.h5.ts -> community.scss; theme.ts -> h5-theme.scss
import './styles/theme'
import './styles/ebook.scss'
import './app.scss'
/** 后台改 homeVariant 后,若当前停在启动页/门户页则立刻跳转 */
function applyHomeVariantRoute(config: NomadroRemoteConfig) {
const variant = resolveTtHomeVariantFromConfig(config)
try {
const pages = Taro.getCurrentPages()
const cur = pages[pages.length - 1] as { route?: string } | undefined
const route = (cur?.route || '').replace(/^\//, '')
const onIndex = route === 'pages/index/index'
const onHub = route === 'pages/hub/index'
if (variant === 'hub' && onIndex) {
void Taro.reLaunch({ url: '/pages/hub/index' })
} else if (variant === 'bmi' && onHub) {
void Taro.reLaunch({ url: '/pages/index/index' })
}
} catch {
/* ignore */
}
}
function App({ children }: PropsWithChildren) {
useLaunch(() => {
trackAppLaunch()
void loadRemoteConfig(true)
if (process.env.TARO_ENV === 'h5') {
applyH5Theme(getStoredH5Theme())
}
})
useDidShow(() => {
softRefreshRemoteConfig()
})
useDidHide(() => {
void flush()
})
useEffect(() => {
const stopRem = startH5RemLock()
const stopLive = startRemoteConfigLiveSync()
const unsub = subscribeRemoteConfig((r) => {
applyHomeVariantRoute(r.config)
})
if (process.env.TARO_ENV === 'h5') {
applyH5Theme(getStoredH5Theme())
}
return () => {
stopRem()
stopLive()
unsub()
}
}, [])
return children
}
export default App