mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-26 00:16:51 +00:00
Improve versions in title
This commit is contained in:
@@ -9,20 +9,20 @@ const VERSIONS_IN_TITLE = 3
|
||||
|
||||
interface Title {
|
||||
title: string,
|
||||
changeTitle: (title: string, versions?: VersionId[]) => unknown,
|
||||
changeTitle: (title: string, versions?: VersionId[], suffix?: string) => unknown,
|
||||
}
|
||||
const Title = createContext<Title>({
|
||||
title: '',
|
||||
changeTitle: () => {},
|
||||
})
|
||||
|
||||
export function useTitle(title?: string, versions?: VersionId[]) {
|
||||
export function useTitle(title?: string, versions?: VersionId[], suffix?: string) {
|
||||
const context = useContext(Title)
|
||||
useEffect(() => {
|
||||
if (title) {
|
||||
context.changeTitle(title, versions)
|
||||
context.changeTitle(title, versions, suffix)
|
||||
}
|
||||
}, [title, versions])
|
||||
}, [title, versions, suffix])
|
||||
return context
|
||||
}
|
||||
|
||||
@@ -30,10 +30,20 @@ export function TitleProvider({ children }: { children: ComponentChildren }) {
|
||||
const { locale } = useLocale()
|
||||
const [title, setTitle] = useState<string>(locale('title.home'))
|
||||
|
||||
const changeTitle = useCallback((title: string, versions?: VersionId[]) => {
|
||||
versions ??= config.versions.map(v => v.id as VersionId)
|
||||
const titleVersions = versions.slice(-VERSIONS_IN_TITLE)
|
||||
document.title = `${title} Minecraft ${titleVersions.join(', ')}`
|
||||
const changeTitle = useCallback((title: string, versionIds?: VersionId[], suffix?: string) => {
|
||||
let versions = config.versions
|
||||
if (versionIds !== undefined) {
|
||||
versions = config.versions.filter(v => versionIds?.includes(v.id as VersionId))
|
||||
}
|
||||
let titleSuffix = ''
|
||||
if (suffix) {
|
||||
titleSuffix = ` - ${suffix}`
|
||||
}
|
||||
if (!(versionIds?.length === 0)) {
|
||||
const titleVersions = versions.map(v => v.id).slice(-VERSIONS_IN_TITLE)
|
||||
titleSuffix = ` - Minecraft ${titleVersions.join(', ')}`
|
||||
}
|
||||
document.title = title + titleSuffix
|
||||
setTitle(title)
|
||||
}, [])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user