Files
outline/app/hooks/useLastVisitedPath.tsx
T
Tom MoorandClaude Opus 5 4b4c7d3281 chore: React 19 upgrade (#13562)
* React 19 upgrade

Bumps react and react-dom to 19.2.8, with the dependency changes React 19
requires: react-is 19, @types/react 19, @types/react-dom 19,
react-helmet-async 3 and react-avatar-editor 15 (which now ships its own
types). framer-motion moves to 13 — its v6 types reference React.ReactHTML,
removed from @types/react 19.

The type changes fall into a few classes:

- The global JSX namespace is gone, so JSX.Element becomes React.JSX.Element
  and the custom element declarations move into a module augmentation.
- useRef() now requires an initial value.
- A ref created with a null initial value is typed nullable, so consumers
  that accept one are widened to RefObject<T | null>.
- React.ReactChild was removed; tooltips take React.ReactNode.
- Ref callbacks may only return a cleanup function. react-dnd connectors
  return the connected node, so useDragRef and useDropRef wrap useDrag and
  useDrop and hand back a ref callback.

react-i18next stays on v12: i18next 23 drops compatibilityJSON "v3", which
would rewrite all 64 _plural keys across every locale.

outline-icons, oy-vey and react-waypoint have no release that lists React 19
as a peer. All three render correctly under it; the install reports them as
unmet peers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Remove react-helmet and forwardRef

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 22:22:50 -04:00

116 lines
3.3 KiB
TypeScript

import { useCallback, useRef } from "react";
import { getCookie, removeCookie, setCookie } from "tiny-cookie";
import usePersistedState, {
setPersistedState,
} from "~/hooks/usePersistedState";
import Logger from "~/utils/Logger";
import history from "~/utils/history";
import { isAllowedLoginRedirect } from "~/utils/urls";
/**
* Hook to set locally and return the document or collection that the user last visited. This is
* used to redirect the user back to the last page they were on, if preferred.
*
* @returns A tuple of the last visited path and a method to set it.
*/
export function useLastVisitedPath(): [string, (path: string) => void] {
const [lastVisitedPath, setLastVisitedPath] = usePersistedState<string>(
"lastVisitedPath",
"/",
{ listen: false }
);
const setPathAsLastVisitedPath = useCallback(
(path: string) => {
if (isAllowedLoginRedirect(path) && path !== lastVisitedPath) {
setLastVisitedPath(path);
}
},
[lastVisitedPath, setLastVisitedPath]
);
return [lastVisitedPath, setPathAsLastVisitedPath] as const;
}
/**
* Hook that automatically tracks the current path as the last visited path.
* This uses a ref to track the previous path and updates localStorage directly
* without using useEffect to avoid React Doctor warnings.
*
* @param currentPath The current path to track.
*/
export function useTrackLastVisitedPath(currentPath: string): void {
const prevPathRef = useRef<string | undefined>(undefined);
// Update localStorage directly if path has changed
if (
prevPathRef.current !== currentPath &&
isAllowedLoginRedirect(currentPath)
) {
prevPathRef.current = currentPath;
setPersistedState("lastVisitedPath", currentPath);
}
}
/**
* Sets the path that the user visited before being asked to login.
*
* @param path The path to set as the post login path.
*/
export function setPostLoginPath(path: string) {
const key = "postLoginRedirectPath";
if (isAllowedLoginRedirect(path)) {
setCookie(key, path, { expires: 1 });
try {
sessionStorage.setItem(key, path);
} catch (_err) {
// If the session storage is full or inaccessible, we can't do anything about it.
}
}
}
/**
* Hook to set locally and return the path that the user visited before being asked
* to login.
*
* @returns A tuple of getter and setter for the post login path.
*/
export function usePostLoginPath() {
const key = "postLoginRedirectPath";
const getter = useCallback(() => {
let path;
try {
path = sessionStorage.getItem(key) || getCookie(key);
} catch (_err) {
// Expected error if the session storage is full or inaccessible.
}
if (path) {
Logger.info("lifecycle", "Spending post login path", { path });
// Remove the cookie once the app has been navigated to the post login path. We dont
// do this immediately as React StrictMode will render multiple times.
const cleanup = history.listen(() => {
try {
sessionStorage.removeItem(key);
} catch (_err) {
// Expected error if the session storage is full or inaccessible.
}
removeCookie(key);
cleanup?.();
});
if (isAllowedLoginRedirect(path)) {
return path;
}
}
return undefined;
}, []);
return [getter, setPostLoginPath] as const;
}