Put tags and search of changelogs and versions in URL

This commit is contained in:
Misode
2022-05-21 02:45:32 +02:00
parent 2ebba08d4b
commit 781d545962
4 changed files with 38 additions and 29 deletions

20
src/app/hooks/useTags.ts Normal file
View 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]
}