fix(ui): сохранять фильтры ресурсов в URL
This commit is contained in:
1 parent
fd7a08d5dc
commit
0734679fd6
7 files changed
+85
-6
No files matched your search
@@ -15,3 +15,12 @@ test("сброс оставляет пробу, вкладку и чужие п
|
||||
const result = patchUptimeUrl(new URLSearchParams('probe=p%2F2&keep=1&tab=speed&q=a&status=down&servers=%5B%221%22%5D'), { q: null, status: null, servers: null })
|
||||
assert.equal(result, '/uptime?probe=p%2F2&keep=1&tab=speed')
|
||||
})
|
||||
|
||||
test("фильтры Resources независимы от Ping и валидируются", async () => {
|
||||
const { readResourceUrl } = await import("./uptime-url-state")
|
||||
const params = new URLSearchParams('tab=resources&q=ping&status=down&rq=CHR&rtype=exit-node')
|
||||
assert.deepEqual(readResourceUrl(params), { search: 'CHR', type: 'exit-node', error: null })
|
||||
assert.equal(readUptimeUrl(params).search, 'ping')
|
||||
for (const raw of ['rtype=unknown', 'rtype=exit-node&rtype=jump-host', 'rq=a&rq=b']) assert.ok(readResourceUrl(new URLSearchParams(raw)).error)
|
||||
assert.equal(patchUptimeUrl(params, { rq: null, rtype: null }), '/uptime?tab=resources&q=ping&status=down')
|
||||
})
|
||||
@@ -35,3 +35,12 @@ export function patchUptimeUrl(params: URLSearchParams, changes: Record<string,
|
||||
}
|
||||
|
||||
export type UrlSetter<T> = Dispatch<SetStateAction<T>>
|
||||
|
||||
export type ResourceTypeFilter = "all" | "jump-host" | "exit-node" | "home-router"
|
||||
export function readResourceUrl(params: URLSearchParams) {
|
||||
const raw = params.get("rtype")
|
||||
const valid = raw === null || ["jump-host", "exit-node", "home-router"].includes(raw)
|
||||
const error = !valid || ["rq", "rtype"].some(key => params.getAll(key).length > 1)
|
||||
? "В ссылке указаны неверные или повторяющиеся фильтры ресурсов." : null
|
||||
return { search: params.get("rq") ?? "", type: (valid && raw !== null ? raw : "all") as ResourceTypeFilter, error }
|
||||
}
|
||||
Reference in new issue
Block a user