mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 23:56:51 +00:00
Put tags and search of changelogs and versions in URL
This commit is contained in:
20
src/app/hooks/useTags.ts
Normal file
20
src/app/hooks/useTags.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useMemo } from 'preact/hooks'
|
||||
import { useSearchParam } from './useSearchParam'
|
||||
|
||||
const TAG_KEY = 'tags'
|
||||
const TAG_SEP = '|'
|
||||
|
||||
export function useTags(): [string[], (tag: string, force?: boolean) => void] {
|
||||
const [tags, setTags] = useSearchParam(TAG_KEY)
|
||||
const activeTags = useMemo(() => tags?.split(TAG_SEP) ?? [], [tags])
|
||||
|
||||
const toggleTag = (tag: string, force?: boolean) => {
|
||||
if (force === false || (activeTags.includes(tag) && force !== true)) {
|
||||
setTags(activeTags.filter(t => t !== tag).join(TAG_SEP), true)
|
||||
} else {
|
||||
setTags([...activeTags, tag].sort().join(TAG_SEP), true)
|
||||
}
|
||||
}
|
||||
|
||||
return [activeTags, toggleTag]
|
||||
}
|
||||
Reference in New Issue
Block a user