fix(ui): сохранять фильтры ресурсов в URL
This commit is contained in:
1 parent
fd7a08d5dc
commit
0734679fd6
7 files changed
+85
-6
No files matched your search
@@ -2,11 +2,12 @@
|
||||
|
||||
import { useMemo } from "react"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { readUptimeUrl, patchUptimeUrl, type UptimeTab, type ProbeStatusFilter, type UrlSetter } from "@/lib/data-filters/uptime-url-state"
|
||||
import { readUptimeUrl, readResourceUrl, patchUptimeUrl, type ResourceTypeFilter, type UptimeTab, type ProbeStatusFilter, type UrlSetter } from "@/lib/data-filters/uptime-url-state"
|
||||
|
||||
export function useUptimeUrlState() {
|
||||
const raw = useSearchParams().toString()
|
||||
const state = useMemo(() => readUptimeUrl(new URLSearchParams(raw)), [raw])
|
||||
const resource = useMemo(() => readResourceUrl(new URLSearchParams(raw)), [raw])
|
||||
function write(changes: Record<string, string | null>, replace = false) {
|
||||
const href = patchUptimeUrl(new URLSearchParams(window.location.search), changes) + window.location.hash
|
||||
if (href === window.location.pathname + window.location.search + window.location.hash) return
|
||||
@@ -24,6 +25,10 @@ export function useUptimeUrlState() {
|
||||
write({ servers: next.length ? JSON.stringify(next) : null })
|
||||
}
|
||||
return {
|
||||
resource,
|
||||
setResourceSearch: (rq: string) => write({ rq }, true),
|
||||
setResourceType: (type: ResourceTypeFilter) => write({ rtype: type === "all" ? null : type }),
|
||||
resetResources: () => write({ rq: null, rtype: null }),
|
||||
tab: state.tab, search: state.search, statusFilter: state.status, serverFilter: state.servers, error: state.error,
|
||||
setTab: (tab: UptimeTab) => write({ tab: tab === "probes" ? null : tab }),
|
||||
setSearch: (q: string) => write({ q }, true), setStatusFilter, setServerFilter,
|
||||
|
||||
Reference in new issue
Block a user