Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PermissionUpdateNotice.tsx

74 lines2.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import { sessionHasLegacyPermissions, type EnvironmentId } from "@t3tools/contracts";
3import { useNavigate } from "@tanstack/react-router";
4import * as Schema from "effect/Schema";
5import { useEffect } from "react";
6​
7import { getLocalStorageItem, setLocalStorageItem } from "../hooks/useLocalStorage";
8import { useEnvironments } from "../state/environments";
9import { environmentSession } from "../state/session";
10import { toastManager } from "./ui/toast";
11​
12// Keep an active toast across remounts, including Strict Mode effect replay.
13const shown = new Set<EnvironmentId>();
14​
15function EnvironmentPermissionNotice({
16 environmentId,
17 label,
18}: {
19 environmentId: EnvironmentId;
20 label: string;
21}) {
22 const session = useAtomValue(environmentSession.sessionStateAtom(environmentId));
23 const navigate = useNavigate();
24 useEffect(() => {
25 if (
26 session._tag !== "Success" ||
27 session.waiting ||
28 !sessionHasLegacyPermissions(session.value)
29 )
30 return;
31 if (shown.has(environmentId)) return;
32 const key = `t3code:permission-update:v1:${environmentId}`;
33 try {
34 if (getLocalStorageItem(key, Schema.Boolean)) return;
35 } catch {}
36 shown.add(environmentId);
37 const persistDismissal = () => {
38 try {
39 setLocalStorageItem(key, true, Schema.Boolean);
40 } catch {}
41 };
42 const dismiss = () => {
43 persistDismissal();
44 toastManager.close(id);
45 };
46 const id = toastManager.add({
47 title: `Permissions have changed for ${label}`,
48 description:
49 "This connection still uses the old permissions, so some actions may no longer be available. Pair again using a new link with the permissions you need.",
50 timeout: 0,
51 onClose: persistDismissal,
52 actionProps: {
53 children: "Open Connections",
54 onClick: () => {
55 dismiss();
56 void navigate({ to: "/settings/connections" });
57 },
58 },
59 data: {
60 actionLayout: "stacked-end",
61 secondaryActionProps: { children: "Dismiss", onClick: dismiss },
62 secondaryActionVariant: "ghost",
63 },
64 });
65 }, [environmentId, label, navigate, session]);
66 return null;
68​
69export function PermissionUpdateNotice() {
70 const { environments } = useEnvironments();
71 return environments.map(({ environmentId, label }) => (
72 <EnvironmentPermissionNotice key={environmentId} environmentId={environmentId} label={label} />
73 ));