fix(ui): восстанавливать фильтры WireGuard из ссылки
This commit is contained in:
1 parent
c6702efcc4
commit
e51964b4d2
8 files changed
+103
-13
No files matched your search
@@ -0,0 +1,11 @@
|
||||
import { test } from "node:test"
|
||||
import assert from "node:assert/strict"
|
||||
import { readWireGuardUrl, patchWireGuardUrl } from "./wireguard-url-state"
|
||||
test("WireGuard URL keeps context and validates all owned filters", () => {
|
||||
assert.deepEqual(readWireGuardUrl(new URLSearchParams()), {tab:"interfaces",status:"all",search:"",error:null})
|
||||
const params = new URLSearchParams("server=2&interface=*%2F2&keep=1&tab=peers&status=down&q=key%2B1")
|
||||
assert.deepEqual(readWireGuardUrl(params), {tab:"peers",status:"down",search:"key+1",error:null})
|
||||
for (const raw of ["tab=bad", "status=bad", "q="+"a".repeat(201), "q=a&q=b", "tab=peers&tab=cli", "status=up&status=down"]) assert.ok(readWireGuardUrl(new URLSearchParams(raw)).error)
|
||||
const next=new URL(patchWireGuardUrl(params,{q:null,status:null,tab:null}),"https://test.local")
|
||||
assert.equal(next.searchParams.get("server"),"2");assert.equal(next.searchParams.get("interface"),"*/2");assert.equal(next.searchParams.get("keep"),"1");assert.equal(next.searchParams.has("q"),false)
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
export type WireGuardTab = "interfaces" | "peers" | "cli"
|
||||
export type WireGuardStatus = "all" | "up" | "down"
|
||||
const tabs = ["interfaces", "peers", "cli"]
|
||||
const statuses = ["all", "up", "down"]
|
||||
export function readWireGuardUrl(params: URLSearchParams) {
|
||||
const tab = params.get("tab") ?? "interfaces", status = params.get("status") ?? "all", search = params.get("q") ?? ""
|
||||
let error: string | null = null
|
||||
if (["tab", "status", "q"].some(key => params.getAll(key).length > 1)) error = "В ссылке повторяются фильтры WireGuard."
|
||||
else if (!tabs.includes(tab)) error = "В ссылке указана неизвестная вкладка WireGuard."
|
||||
else if (!statuses.includes(status)) error = "В ссылке указан неизвестный статус WireGuard."
|
||||
else if (search.length > 200) error = "Поиск WireGuard в ссылке слишком длинный."
|
||||
return { tab: (tabs.includes(tab) ? tab : "interfaces") as WireGuardTab, status: (statuses.includes(status) ? status : "all") as WireGuardStatus, search, error }
|
||||
}
|
||||
export function patchWireGuardUrl(params: URLSearchParams, changes: Record<string, string | null>) {
|
||||
const next = new URLSearchParams(params)
|
||||
for (const [key, value] of Object.entries(changes)) if (!value) next.delete(key); else next.set(key, value)
|
||||
return "/wireguard" + (next.size ? "?" + next.toString() : "")
|
||||
}
|
||||
Reference in new issue
Block a user