Skip to content

Commit 61cb2a5

Browse files
juliusmarmingecodex
andcommitted
refactor(web): retain primary session state with atoms
Co-authored-by: codex <[email protected]>
1 parent f28bf2d commit 61cb2a5

3 files changed

Lines changed: 47 additions & 32 deletions

File tree

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ import { getPairingTokenFromUrl, setPairingTokenOnUrl } from "../../pairingUrl";
8787
import { readHostedPairingRequest } from "../../hostedPairing";
8888
import {
8989
createServerPairingCredential,
90-
fetchSessionState,
9190
revokeOtherServerClientSessions,
9291
revokeServerClientSession,
9392
revokeServerPairingLink,
9493
isLoopbackHostname,
94+
usePrimarySessionState,
9595
type ServerClientSessionRecord,
9696
type ServerPairingLinkRecord,
9797
} from "~/environments/primary";
@@ -1584,13 +1584,13 @@ const DesktopSshHostRow = memo(function DesktopSshHostRow({
15841584

15851585
export function ConnectionsSettings() {
15861586
const desktopBridge = window.desktopBridge;
1587-
const [currentSessionScopes, setCurrentSessionScopes] =
1588-
useState<ReadonlyArray<AuthEnvironmentScope> | null>(
1589-
desktopBridge ? AuthAdministrativeScopes : null,
1590-
);
1591-
const [currentAuthPolicy, setCurrentAuthPolicy] = useState<
1592-
"desktop-managed-local" | "loopback-browser" | "remote-reachable" | "unsafe-no-auth" | null
1593-
>(desktopBridge ? null : null);
1587+
const primarySessionState = usePrimarySessionState();
1588+
const currentSessionScopes = desktopBridge
1589+
? AuthAdministrativeScopes
1590+
: primarySessionState.data?.authenticated
1591+
? (primarySessionState.data.scopes ?? null)
1592+
: null;
1593+
const currentAuthPolicy = desktopBridge ? null : (primarySessionState.data?.auth.policy ?? null);
15941594
const savedEnvironmentsById = useSavedEnvironmentRegistryStore((state) => state.byId);
15951595
const savedEnvironmentIds = useMemo(
15961596
() =>
@@ -2113,30 +2113,6 @@ export function ConnectionsSettings() {
21132113
savedBackendMode,
21142114
]);
21152115

2116-
useEffect(() => {
2117-
if (desktopBridge) {
2118-
setCurrentSessionScopes(AuthAdministrativeScopes);
2119-
return;
2120-
}
2121-
2122-
let cancelled = false;
2123-
void fetchSessionState()
2124-
.then((session) => {
2125-
if (cancelled) return;
2126-
setCurrentSessionScopes(session.authenticated ? (session.scopes ?? null) : null);
2127-
setCurrentAuthPolicy(session.auth.policy);
2128-
})
2129-
.catch(() => {
2130-
if (cancelled) return;
2131-
setCurrentSessionScopes(null);
2132-
setCurrentAuthPolicy(null);
2133-
});
2134-
2135-
return () => {
2136-
cancelled = true;
2137-
};
2138-
}, [desktopBridge]);
2139-
21402116
useEffect(() => {
21412117
if (!canManageLocalBackend) return;
21422118

‎apps/web/src/environments/primary/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ export {
3232
__resetServerAuthBootstrapForTests,
3333
} from "./auth";
3434

35+
export { refreshPrimarySessionState, usePrimarySessionState } from "./sessionState";
36+
3537
export { resolvePrimaryEnvironmentHttpUrl, isLoopbackHostname } from "./target";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import * as Cause from "effect/Cause";
3+
import * as Effect from "effect/Effect";
4+
import * as Option from "effect/Option";
5+
import { AsyncResult, Atom } from "effect/unstable/reactivity";
6+
import { useCallback } from "react";
7+
8+
import { appAtomRegistry } from "../../rpc/atomRegistry";
9+
import { fetchSessionState } from "./auth";
10+
11+
const primarySessionStateAtom = Atom.make(Effect.promise(fetchSessionState)).pipe(
12+
Atom.swr({ staleTime: 5_000, revalidateOnMount: true }),
13+
Atom.setIdleTTL(5 * 60_000),
14+
Atom.withLabel("primary-environment:session"),
15+
);
16+
17+
export function refreshPrimarySessionState(): void {
18+
appAtomRegistry.refresh(primarySessionStateAtom);
19+
}
20+
21+
export function usePrimarySessionState() {
22+
const result = useAtomValue(primarySessionStateAtom);
23+
const refresh = useCallback(() => {
24+
refreshPrimarySessionState();
25+
}, []);
26+
let error: string | null = null;
27+
if (result._tag === "Failure") {
28+
const cause = Cause.squash(result.cause);
29+
error = cause instanceof Error ? cause.message : "Could not read environment session.";
30+
}
31+
return {
32+
data: Option.getOrNull(AsyncResult.value(result)),
33+
error,
34+
isPending: result.waiting,
35+
refresh,
36+
};
37+
}

0 commit comments

Comments
 (0)