mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Projects (#192)
* Add file save UI and drafts project * Fix build * Create SearchList component as abstraction * Add project page and file tree view * Create Locale context * Create Theme context * Create Version context * Create Title context * Create Project context * Store current file in project context * Fix issues when renaming file and implement deleting * Style improvements * Make all project strings translatable * Fix z-index
This commit is contained in:
54
src/app/contexts/Version.tsx
Normal file
54
src/app/contexts/Version.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
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 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) => unknown,
|
||||
}
|
||||
const Version = createContext<Version>({
|
||||
version: '1.18',
|
||||
changeVersion: () => {},
|
||||
})
|
||||
|
||||
export function useVersion() {
|
||||
return useContext(Version)
|
||||
}
|
||||
|
||||
export function VersionProvider({ children }: { children: ComponentChildren }) {
|
||||
const [version, setVersion] = useState<VersionId>(Store.getVersion())
|
||||
|
||||
const searchParams = getSearchParams(getCurrentUrl())
|
||||
const targetVersion = searchParams.get(VERSION_PARAM)
|
||||
useEffect(() => {
|
||||
if (VersionIds.includes(targetVersion as VersionId) && version !== targetVersion) {
|
||||
setVersion(targetVersion as VersionId)
|
||||
}
|
||||
}, [version, targetVersion])
|
||||
|
||||
const changeVersion = useCallback((version: VersionId) => {
|
||||
if (getSearchParams(getCurrentUrl()).has(VERSION_PARAM)) {
|
||||
setSeachParams({ version })
|
||||
}
|
||||
Analytics.setVersion(version)
|
||||
Store.setVersion(version)
|
||||
setVersion(version)
|
||||
}, [])
|
||||
|
||||
const value: Version = {
|
||||
version,
|
||||
changeVersion,
|
||||
}
|
||||
|
||||
return <Version.Provider value={value}>
|
||||
{children}
|
||||
</Version.Provider>
|
||||
}
|
||||
Reference in New Issue
Block a user