mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-29 11:42:56 +01:00
Keep filters out of the web view's storage, and let a newer selection win over a late read
Filters can hold captured values, so neither the reload's places nor the launch's last filter go into web view storage: the engine holds the places in memory while the app runs, and proxy.db's last_filter row keeps the last filter. A read of a selected flow that answers after the window shows a newer copy no longer replaces it. The pane and stream harnesses select through goToPlace, so switching a tab no longer empties their card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c2d4c39c62
commit
cc83c7cd1e
12 files changed
+281
-50
No files matched your search
@@ -1,10 +1,10 @@
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useSetAtom, useStore } from "jotai";
|
||||
import { useEffect, useLayoutEffect, useState } from "react";
|
||||
import { AddColumnDialog } from "../components/AddColumnDialog";
|
||||
import { FlowDetail } from "../components/FlowDetail";
|
||||
import { FlowList } from "../components/FlowList";
|
||||
import { selectedFlowAtom } from "../lib/atoms";
|
||||
import { browserTabMemory, learnedTabKey, flowShape } from "../lib/defaultTab";
|
||||
import { goToPlace } from "../lib/navigation";
|
||||
import { flowWindowAtom } from "../lib/store";
|
||||
import { type FlowFixture, fixtures } from "./fixtures";
|
||||
|
||||
@@ -42,7 +42,7 @@ export function FlowPanesHarness() {
|
||||
|
||||
function FixtureCard({ fixture }: { fixture: FlowFixture }) {
|
||||
const params = new URLSearchParams(location.search);
|
||||
const setSelected = useSetAtom(selectedFlowAtom);
|
||||
const store = useStore();
|
||||
const setWindow = useSetAtom(flowWindowAtom);
|
||||
const [ready, setReady] = useState(false);
|
||||
useLayoutEffect(() => {
|
||||
@@ -52,7 +52,8 @@ function FixtureCard({ fixture }: { fixture: FlowFixture }) {
|
||||
browserTabMemory()?.setItem(learnedTabKey("response", flowShape(fixture.flow)), tab);
|
||||
}
|
||||
setWindow({ flows, hasOlder: false, total: flows.length });
|
||||
setSelected(flows.find((f) => f.id === fixture.flow.id) ?? fixture.flow);
|
||||
const shown = flows.find((f) => f.id === fixture.flow.id) ?? fixture.flow;
|
||||
goToPlace(store, "update", { selection: [shown.id], flow: shown });
|
||||
setReady(true);
|
||||
// oxlint-disable-next-line react-hooks/exhaustive-deps -- once per fixture
|
||||
}, [fixture]);
|
||||
|
||||
@@ -82,10 +82,11 @@ export function LockedHarness() {
|
||||
const [synced, setSynced] = useState(false);
|
||||
useEffect(() => {
|
||||
syncProxyState(store);
|
||||
syncNavigation(store);
|
||||
syncFlows(store);
|
||||
syncSidebarSelection(store);
|
||||
setSynced(true);
|
||||
void syncNavigation(store).then(() => {
|
||||
syncFlows(store);
|
||||
syncSidebarSelection(store);
|
||||
setSynced(true);
|
||||
});
|
||||
}, [store]);
|
||||
if (!synced) return null;
|
||||
return (
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type { Flow } from "@yaakapp-internal/proxy-lib";
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useSetAtom, useStore } from "jotai";
|
||||
import { useLayoutEffect, useMemo, useState } from "react";
|
||||
import { FlowDetail } from "../components/FlowDetail";
|
||||
import { FlowList } from "../components/FlowList";
|
||||
import { ProxyEditorHost } from "../components/ProxyEditorHost";
|
||||
import { MessageSourceContext } from "../hooks/useFlowMessages";
|
||||
import { selectedFlowAtom } from "../lib/atoms";
|
||||
import { goToPlace } from "../lib/navigation";
|
||||
import { flowWindowAtom } from "../lib/store";
|
||||
import { fixtureSource, type StreamFixture, streamFixtures } from "./streamFixtures";
|
||||
|
||||
@@ -51,15 +51,16 @@ function listedFlows(shown: Flow): Flow[] {
|
||||
|
||||
function StreamDetail({ fixture }: { fixture: StreamFixture }) {
|
||||
const source = useMemo(() => fixtureSource(fixture), [fixture]);
|
||||
const setSelected = useSetAtom(selectedFlowAtom);
|
||||
const store = useStore();
|
||||
const setWindow = useSetAtom(flowWindowAtom);
|
||||
const [flow, setFlow] = useState(fixture.flow);
|
||||
const withList = new URLSearchParams(location.search).has("list");
|
||||
useLayoutEffect(() => {
|
||||
const flows = listedFlows(flow);
|
||||
setWindow({ flows, hasOlder: false, total: flows.length });
|
||||
setSelected(flows.find((f) => f.id === flow.id) ?? flow);
|
||||
}, [flow, setSelected, setWindow]);
|
||||
const shown = flows.find((f) => f.id === flow.id) ?? flow;
|
||||
goToPlace(store, "update", { selection: [shown.id], flow: shown });
|
||||
}, [flow, store, setWindow]);
|
||||
// The count on the flow follows its messages, as the engine writes it
|
||||
useLayoutEffect(
|
||||
() => source.subscribe(() => setFlow((f) => ({ ...f, messageCount: f.messageCount + 1 }))),
|
||||
|
||||
@@ -72,6 +72,7 @@ function emit(event: string, payload: unknown = {}) {
|
||||
/** Filters applied, newest first, and saved searches, kept across reloads as the database is */
|
||||
const HISTORY_KEY = "harness_filter_history";
|
||||
const SAVED_KEY = "harness_saved_queries";
|
||||
const LAST_FILTER_KEY = "harness_last_filter";
|
||||
|
||||
function stored<T>(key: string): T[] {
|
||||
try {
|
||||
@@ -146,6 +147,17 @@ function answer(cmd: string, payload: unknown): unknown {
|
||||
store(HISTORY_KEY, []);
|
||||
emit("filter_history_changed");
|
||||
return 0;
|
||||
// Held by the engine for as long as the app runs; the tab's session here
|
||||
case "get_window_places":
|
||||
return sessionStorage.getItem("harness_window_places");
|
||||
case "set_window_places":
|
||||
sessionStorage.setItem("harness_window_places", String(req.places));
|
||||
return true;
|
||||
case "get_last_filter":
|
||||
return localStorage.getItem(LAST_FILTER_KEY) ?? "";
|
||||
case "set_last_filter":
|
||||
localStorage.setItem(LAST_FILTER_KEY, String(req.filter));
|
||||
return true;
|
||||
case "list_saved_queries":
|
||||
return stored<SavedQuery>(SAVED_KEY);
|
||||
case "create_saved_query": {
|
||||
|
||||
@@ -14,6 +14,8 @@ import { EMPTY_PLACE, stackOf } from "./places";
|
||||
|
||||
const existing = new Set<string>();
|
||||
const recorded: string[] = [];
|
||||
let lastFilter = "";
|
||||
let windowPlaces: string | null = null;
|
||||
|
||||
vi.mock("./rpc", () => ({
|
||||
rpc: vi.fn(async (cmd: string, payload: { ids?: string[]; filter?: string }) => {
|
||||
@@ -21,6 +23,10 @@ vi.mock("./rpc", () => ({
|
||||
return (payload.ids ?? []).filter((id) => existing.has(id)).map((id) => flow(id));
|
||||
}
|
||||
if (cmd === "record_filter") recorded.push(payload.filter ?? "");
|
||||
if (cmd === "get_last_filter") return lastFilter;
|
||||
if (cmd === "get_window_places") return windowPlaces;
|
||||
if (cmd === "set_window_places") windowPlaces = (payload as { places: string }).places;
|
||||
if (cmd === "set_last_filter") lastFilter = payload.filter ?? "";
|
||||
return true;
|
||||
}),
|
||||
listen: vi.fn(() => () => {}),
|
||||
@@ -29,7 +35,7 @@ vi.mock("./tauri", () => ({ command: vi.fn(async () => {}), subscribe: vi.fn(()
|
||||
// The package's index reaches for the window as it loads; the sidebar's selection is all used.
|
||||
vi.mock("@yaakapp-internal/ui", () => ({ selectedIdsFamily: () => atom<string[]>([]) }));
|
||||
|
||||
const { applyFilter, goToPlace, searchDraftAtom, searchDraftVersionAtom } =
|
||||
const { applyFilter, goToPlace, searchDraftAtom, searchDraftVersionAtom, syncNavigation } =
|
||||
await import("./navigation");
|
||||
|
||||
function flow(id: string): Flow {
|
||||
@@ -47,6 +53,65 @@ const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
beforeEach(() => {
|
||||
existing.clear();
|
||||
recorded.length = 0;
|
||||
lastFilter = "";
|
||||
windowPlaces = null;
|
||||
});
|
||||
|
||||
describe("syncNavigation", () => {
|
||||
test("a launch restores the last filter applied, and keeps each one applied after", async () => {
|
||||
lastFilter = "@host:*.github.com";
|
||||
const store = createStore();
|
||||
await syncNavigation(store);
|
||||
await settle();
|
||||
expect(store.get(appliedFilterAtom)).toBe("@host:*.github.com");
|
||||
expect(store.get(searchDraftAtom)).toBe("@host:*.github.com");
|
||||
expect(store.get(canGoBackAtom)).toBe(false);
|
||||
|
||||
applyFilter(store, "stripe");
|
||||
await settle();
|
||||
expect(lastFilter).toBe("stripe");
|
||||
});
|
||||
|
||||
test("a launch the person has already moved on from keeps where they are", async () => {
|
||||
lastFilter = "old";
|
||||
const store = createStore();
|
||||
await syncNavigation(store);
|
||||
applyFilter(store, "new");
|
||||
await settle();
|
||||
expect(store.get(appliedFilterAtom)).toBe("new");
|
||||
});
|
||||
|
||||
test("a reload takes back the places, the tabs and the selection, not the last filter", async () => {
|
||||
existing.add("B");
|
||||
lastFilter = "elsewhere";
|
||||
const before = createStore();
|
||||
await syncNavigation(before);
|
||||
applyFilter(before, "x");
|
||||
goToPlace(before, "add", { selection: ["B"], flow: flow("B") });
|
||||
goToPlace(before, "update", { tabs: { response: "headers" } });
|
||||
await settle();
|
||||
|
||||
const after = createStore();
|
||||
await syncNavigation(after);
|
||||
await settle();
|
||||
expect(after.get(appliedFilterAtom)).toBe("x");
|
||||
expect(after.get(placeAtom).tabs).toEqual({ response: "headers" });
|
||||
expect(after.get(selectedFlowAtom)?.id).toBe("B");
|
||||
expect(goToPlace(after, "back")).toBe(true);
|
||||
});
|
||||
|
||||
test("a late read of the selected flow leaves a newer one alone", async () => {
|
||||
existing.add("A");
|
||||
const store = newStore();
|
||||
goToPlace(store, "add", { selection: ["A"], flow: flow("A") });
|
||||
goToPlace(store, "add", { selection: ["B"], flow: flow("B") });
|
||||
store.set(selectedFlowAtom, null);
|
||||
goToPlace(store, "back");
|
||||
const newer = { ...flow("A"), status: 200 } as Flow;
|
||||
goToPlace(store, "update", { selection: ["A"], flow: newer });
|
||||
await settle();
|
||||
expect(store.get(selectedFlowAtom)).toBe(newer);
|
||||
});
|
||||
});
|
||||
|
||||
describe("goToPlace", () => {
|
||||
|
||||
@@ -163,49 +163,61 @@ function readSelected(store: Store, id: string) {
|
||||
store.set(placeStackAtom, (s) => stackWithout(s, new Set([id])));
|
||||
return;
|
||||
}
|
||||
// The window has moved on meanwhile.
|
||||
if (currentPlace(store.get(placeStackAtom)).selection.has(id)) {
|
||||
store.set(selectedFlowAtom, found);
|
||||
}
|
||||
// The window has moved on meanwhile, or shows the flow already, as it was since.
|
||||
const current = currentPlace(store.get(placeStackAtom)).selection.has(id);
|
||||
if (current && store.get(selectedFlowAtom) == null) store.set(selectedFlowAtom, found);
|
||||
})
|
||||
.catch((err) => console.warn("Failed to read the selected flow", err));
|
||||
}
|
||||
|
||||
/** Where the window's places are kept for a reload, for this session */
|
||||
const PLACES_KEY = "proxy_places";
|
||||
/** Where the filter last applied is kept for the next launch */
|
||||
const LAST_FILTER_KEY = "proxy_last_filter";
|
||||
|
||||
function read(storage: () => Storage, key: string): string | null {
|
||||
try {
|
||||
return storage().getItem(key);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function write(storage: () => Storage, key: string, value: string) {
|
||||
try {
|
||||
storage().setItem(key, value);
|
||||
} catch {
|
||||
// Not kept: the next reload starts from the last filter.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores where the window was, before anything reads the filter: its places after a reload,
|
||||
* or the last filter applied at launch. Keeps them as they change.
|
||||
* Restores where the window was, before the flows load for its filter: its places after a
|
||||
* reload, which the engine holds in memory while the app runs, or at launch the last filter
|
||||
* applied, which the encrypted database keeps. Filters can hold captured values, so neither is
|
||||
* kept in the web view's storage. Keeps them as they change.
|
||||
*/
|
||||
export function syncNavigation(store: Store) {
|
||||
const kept = stackFromJson(read(() => sessionStorage, PLACES_KEY));
|
||||
const lastFilter = read(() => localStorage, LAST_FILTER_KEY) ?? "";
|
||||
const stack = kept ?? stackOf({ ...EMPTY_PLACE, filter: lastFilter });
|
||||
export async function syncNavigation(store: Store) {
|
||||
const kept = stackFromJson(await rpc("get_window_places", {}).catch(() => null));
|
||||
const stack = kept ?? stackOf(EMPTY_PLACE);
|
||||
store.set(placeStackAtom, stack);
|
||||
showPlace(store, currentPlace(stack), { filter: true, reveal: true });
|
||||
if (kept == null) {
|
||||
// Unread while traffic is locked, as the database is.
|
||||
rpc("get_last_filter", {})
|
||||
.then((filter) => {
|
||||
// Only over the window as it opened, not where it has been taken since.
|
||||
const now = store.get(placeStackAtom);
|
||||
if (filter === "" || now.places.length > 1 || currentPlace(now).filter !== "") return;
|
||||
goToPlace(store, "update", { filter });
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
// One write at a time, the latest places after it, so they arrive in order.
|
||||
let pending: string | null = null;
|
||||
let writing = false;
|
||||
const write = () => {
|
||||
if (writing || pending == null) return;
|
||||
const places = pending;
|
||||
pending = null;
|
||||
writing = true;
|
||||
rpc("set_window_places", { places })
|
||||
.catch(() => {})
|
||||
.finally(() => {
|
||||
writing = false;
|
||||
write();
|
||||
});
|
||||
};
|
||||
store.sub(placeStackAtom, () => {
|
||||
const now = store.get(placeStackAtom);
|
||||
write(() => sessionStorage, PLACES_KEY, stackToJson(now));
|
||||
write(() => localStorage, LAST_FILTER_KEY, currentPlace(now).filter);
|
||||
pending = stackToJson(store.get(placeStackAtom));
|
||||
write();
|
||||
});
|
||||
let lastFilter = store.get(appliedFilterAtom);
|
||||
store.sub(appliedFilterAtom, () => {
|
||||
const filter = store.get(appliedFilterAtom);
|
||||
if (filter === lastFilter) return;
|
||||
lastFilter = filter;
|
||||
rpc("set_last_filter", { filter }).catch(() => {});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ const jotaiStore = createStore();
|
||||
|
||||
syncProxyState(jotaiStore);
|
||||
// Before the flows load, so they load for the filter it restores
|
||||
syncNavigation(jotaiStore);
|
||||
await syncNavigation(jotaiStore);
|
||||
syncFlows(jotaiStore);
|
||||
syncSidebarSelection(jotaiStore);
|
||||
|
||||
|
||||
Reference in new issue
Block a user