b349676eae
On page reload the sidebar tree rendered nothing until every root page
was fetched (paginated), and children of expanded branches arrived even
later (breadcrumbs effect / socket connect) — the tree visibly jumped a
couple of seconds after load.
- treeDataAtom is now a facade over atomFamily(atomWithStorage) keyed
treeData:v1:{workspaceId}:{userId} with getOnInit: true — the cached
tree hydrates synchronously and paints on the very first render,
together with the already-persisted open-branches map. Public atom
interface unchanged (value or functional updater), all call sites
untouched.
- Custom sync storage: debounced writes (500ms, coalesced, size guard,
beforeunload flush), defensive reads (corrupted JSON -> []), no
cross-tab subscribe (localStorage is a boot cache only).
- SpaceTree renders on cached data immediately; "No pages yet" still
waits for the server. Once server roots merge, open loaded branches
are re-fetched fresh and reconciled once per space (shared
refreshOpenBranches, also used by the socket reconnect handler).
- Logout hygiene: clearPersistedTreeCaches() purges treeData:v1:* and
openTreeNodes:* by prefix and disables further persistence (kill
switch closes the websocket-write-vs-beforeunload-flush resurrection
race). Wired into both handleLogout and the 401 redirectToLogin path,
since cached trees contain page titles.
- Tests: tree-data-atom.test.ts (hydration, debounce round-trip,
corrupted JSON, scope isolation, logout purge, persistence kill
switch); expand-all suite adapted. 144 tree tests / full client suite
green, tsc clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import axios, { AxiosInstance } from "axios";
|
|
import APP_ROUTE from "@/lib/app-route.ts";
|
|
import { isCloud } from "@/lib/config.ts";
|
|
import { clearPersistedTreeCaches } from "@/features/page/tree/atoms/tree-data-atom";
|
|
|
|
const api: AxiosInstance = axios.create({
|
|
baseURL: "/api",
|
|
withCredentials: true,
|
|
});
|
|
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
// we need the response headers for these endpoints
|
|
const exemptEndpoints = ["/api/pages/export", "/api/spaces/export"];
|
|
if (response.request.responseURL) {
|
|
const path = new URL(response.request.responseURL)?.pathname;
|
|
if (path && exemptEndpoints.includes(path)) {
|
|
return response;
|
|
}
|
|
}
|
|
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
if (error.response) {
|
|
switch (error.response.status) {
|
|
case 401: {
|
|
const url = new URL(error.request.responseURL)?.pathname;
|
|
if (url === "/api/auth/collab-token") return;
|
|
if (window.location.pathname.startsWith("/share/")) return;
|
|
|
|
// Handle unauthorized error
|
|
redirectToLogin();
|
|
break;
|
|
}
|
|
case 403:
|
|
// Handle forbidden error
|
|
break;
|
|
case 404:
|
|
// Handle not found error
|
|
if (
|
|
error.response.data.message
|
|
.toLowerCase()
|
|
.includes("workspace not found")
|
|
) {
|
|
console.log("workspace not found");
|
|
if (
|
|
!isCloud() &&
|
|
window.location.pathname != APP_ROUTE.AUTH.SETUP
|
|
) {
|
|
window.location.href = APP_ROUTE.AUTH.SETUP;
|
|
}
|
|
}
|
|
break;
|
|
case 500:
|
|
// Handle internal server error
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return Promise.reject(error);
|
|
},
|
|
);
|
|
|
|
function redirectToLogin() {
|
|
const exemptPaths = [
|
|
APP_ROUTE.AUTH.LOGIN,
|
|
APP_ROUTE.AUTH.SIGNUP,
|
|
APP_ROUTE.AUTH.FORGOT_PASSWORD,
|
|
APP_ROUTE.AUTH.PASSWORD_RESET,
|
|
"/invites",
|
|
];
|
|
if (!exemptPaths.some((path) => window.location.pathname.startsWith(path))) {
|
|
// Forced logout (401 / expired session) must purge the persisted sidebar
|
|
// tree caches too: they contain page titles, and on a shared machine most
|
|
// sessions end via cookie expiry — not the logout button — so this is the
|
|
// only cleanup that runs on that path. It also disables further cache
|
|
// persistence until the full page load below.
|
|
clearPersistedTreeCaches();
|
|
const redirectTo = window.location.pathname;
|
|
if (redirectTo === APP_ROUTE.HOME) {
|
|
window.location.href = APP_ROUTE.AUTH.LOGIN;
|
|
} else {
|
|
const params = new URLSearchParams({ redirect: redirectTo });
|
|
window.location.href = `${APP_ROUTE.AUTH.LOGIN}?${params.toString()}`;
|
|
}
|
|
}
|
|
}
|
|
|
|
export default api;
|