mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Add guides (#224)
* Add guides * Add versioning to guides * Guides: special variables and nested expressions * Add guides page to vite build * Add search and hash hooks, guide tags and headings * Improve guides list and filtering * Add 1.19 download link
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
import type { ComponentChildren } from 'preact'
|
||||
import { createContext } from 'preact'
|
||||
import { getCurrentUrl } from 'preact-router'
|
||||
import { useCallback, useContext, useEffect, useState } from 'preact/hooks'
|
||||
import { Analytics } from '../Analytics'
|
||||
import { useSearchParam } from '../hooks'
|
||||
import type { VersionId } from '../services'
|
||||
import { VersionIds } from '../services'
|
||||
import { Store } from '../Store'
|
||||
import { getSearchParams, setSeachParams } from '../Utils'
|
||||
|
||||
const VERSION_PARAM = 'version'
|
||||
|
||||
interface Version {
|
||||
version: VersionId,
|
||||
changeVersion: (version: VersionId, store?: boolean) => unknown,
|
||||
changeVersion: (version: VersionId, store?: boolean, updateSearch?: boolean) => unknown,
|
||||
changeTargetVersion: (version: VersionId, replace?: boolean) => unknown,
|
||||
}
|
||||
const Version = createContext<Version>({
|
||||
version: '1.18.2',
|
||||
changeVersion: () => {},
|
||||
changeTargetVersion: () => {},
|
||||
})
|
||||
|
||||
export function useVersion() {
|
||||
@@ -26,28 +27,29 @@ export function useVersion() {
|
||||
export function VersionProvider({ children }: { children: ComponentChildren }) {
|
||||
const [version, setVersion] = useState<VersionId>(Store.getVersion())
|
||||
|
||||
const searchParams = getSearchParams(getCurrentUrl())
|
||||
const targetVersion = searchParams.get(VERSION_PARAM)
|
||||
const [targetVersion, changeTargetVersion] = useSearchParam(VERSION_PARAM)
|
||||
|
||||
useEffect(() => {
|
||||
if (VersionIds.includes(targetVersion as VersionId) && version !== targetVersion) {
|
||||
setVersion(targetVersion as VersionId)
|
||||
}
|
||||
}, [version, targetVersion])
|
||||
|
||||
const changeVersion = useCallback((version: VersionId, store = true) => {
|
||||
if (getSearchParams(getCurrentUrl()).has(VERSION_PARAM)) {
|
||||
setSeachParams({ version })
|
||||
const changeVersion = useCallback((newVersion: VersionId, store = true, updateSearch = false) => {
|
||||
if (updateSearch || targetVersion) {
|
||||
changeTargetVersion(newVersion, true)
|
||||
}
|
||||
if (store) {
|
||||
Analytics.setVersion(version)
|
||||
Store.setVersion(version)
|
||||
Analytics.setVersion(newVersion)
|
||||
Store.setVersion(newVersion)
|
||||
}
|
||||
setVersion(version)
|
||||
}, [])
|
||||
setVersion(newVersion)
|
||||
}, [targetVersion])
|
||||
|
||||
const value: Version = {
|
||||
version,
|
||||
changeVersion,
|
||||
changeTargetVersion,
|
||||
}
|
||||
|
||||
return <Version.Provider value={value}>
|
||||
|
||||
Reference in New Issue
Block a user