Skip to content

Commit ca3e8dd

Browse files
committed
Use loader in Settings.tsx
Use loader to always fetch fresh user data on initial load. Refs. TS-2758
1 parent 7693308 commit ca3e8dd

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

apps/cyberstorm-remix/app/settings/user/Connections/Connections.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ export default function Connections() {
4545

4646
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4747
const onSubmitSuccess = (_r: unknown) => {
48-
if (!outletContext.currentUser || !outletContext.currentUser.username)
48+
const username = outletContext.currentUser?.username;
49+
const user = outletContext.currentUser;
50+
51+
if (!username || !user) {
4952
throw new Error("User not logged in");
53+
}
54+
5055
toast.addToast({
5156
csVariant: "success",
52-
children: (
53-
<>
54-
User {outletContext.currentUser.username} was disconnected from TODO
55-
</>
56-
),
57+
children: <>User {username} was disconnected from</>,
5758
duration: 30000,
5859
});
5960
};

apps/cyberstorm-remix/app/settings/user/Settings.tsx

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
1-
import { Outlet, useLocation, useOutletContext } from "react-router";
1+
import {
2+
Outlet,
3+
useLocation,
4+
useLoaderData,
5+
useOutletContext,
6+
} from "react-router";
27
import { NewLink, Tabs } from "@thunderstore/cyberstorm";
38
import { PageHeader } from "~/commonComponents/PageHeader/PageHeader";
4-
59
import { type OutletContextShape } from "../../root";
610
import "./Settings.css";
711
import { NotLoggedIn } from "~/commonComponents/NotLoggedIn/NotLoggedIn";
12+
import { getPublicEnvVariables } from "cyberstorm/security/publicEnvVariables";
13+
import { DapperTs } from "@thunderstore/dapper-ts";
14+
15+
function getSessionIdFromCookie(cookieHeader: string): string | null {
16+
const match = cookieHeader
17+
.split("; ")
18+
.find((c) => c.startsWith("sessionid="));
19+
return match ? match.split("=")[1] : null;
20+
}
821

9-
// export async function clientLoader() {
10-
// const _storage = new NamespacedStorageManager(SESSION_STORAGE_KEY);
11-
// const currentUser = getSessionCurrentUser(_storage, true, undefined, () => {
12-
// clearSession(_storage);
13-
// throw new Response("Your session has expired, please log in again", {
14-
// status: 401,
15-
// });
16-
// // redirect("/");
17-
// });
22+
function tabClass(isActive: boolean) {
23+
return `tabs-item${isActive ? " tabs-item--current" : ""}`;
24+
}
25+
26+
export async function loader({ request }) {
27+
const cookieHeader = request.headers.get("cookie") || "";
28+
const sessionId = getSessionIdFromCookie(cookieHeader);
29+
const publicEnvVariables = getPublicEnvVariables(["VITE_API_URL"]);
1830

19-
// if (
20-
// !currentUser.username ||
21-
// (currentUser.username && currentUser.username === "")
22-
// ) {
23-
// clearSession(_storage);
24-
// throw new Response("Not logged in.", { status: 401 });
25-
// } else {
26-
// return {
27-
// currentUser: currentUser as typeof currentUserSchema._type,
28-
// };
29-
// }
30-
// }
31+
const dapper = new DapperTs(() => {
32+
return {
33+
apiHost: publicEnvVariables.VITE_API_URL,
34+
sessionId: sessionId || undefined,
35+
};
36+
});
3137

32-
// export function HydrateFallback() {
33-
// return <div style={{ padding: "32px" }}>Loading...</div>;
34-
// }
38+
const currentUser = await dapper.getCurrentUser();
39+
return { currentUser };
40+
}
41+
42+
export function HydrateFallback() {
43+
return <div style={{ padding: "32px" }}>Loading...</div>;
44+
}
3545

3646
export default function Community() {
37-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
38-
// const { currentUser } = useLoaderData<typeof clientLoader>();
3947
const location = useLocation();
40-
const outletContext = useOutletContext() as OutletContextShape;
48+
const rootContext = useOutletContext<OutletContextShape>();
49+
const { currentUser } = useLoaderData();
4150

42-
if (!outletContext.currentUser || !outletContext.currentUser.username)
51+
if (!currentUser || !currentUser.username) {
4352
return <NotLoggedIn />;
53+
}
54+
55+
const outletContext: OutletContextShape = {
56+
...rootContext,
57+
currentUser,
58+
};
4459

4560
const currentTab = location.pathname.endsWith("/account/")
4661
? "account"
@@ -51,16 +66,15 @@ export default function Community() {
5166
<PageHeader headingLevel="1" headingSize="2">
5267
Settings
5368
</PageHeader>
69+
5470
<div className="settings-user">
5571
<Tabs>
5672
<NewLink
5773
key="settings"
5874
primitiveType="cyberstormLink"
5975
linkId="Settings"
6076
aria-current={currentTab === "settings"}
61-
rootClasses={`tabs-item${
62-
currentTab === "settings" ? " tabs-item--current" : ""
63-
}`}
77+
rootClasses={tabClass(currentTab === "settings")}
6478
>
6579
Settings
6680
</NewLink>
@@ -69,13 +83,12 @@ export default function Community() {
6983
primitiveType="cyberstormLink"
7084
linkId="SettingsAccount"
7185
aria-current={currentTab === "account"}
72-
rootClasses={`tabs-item${
73-
currentTab === "account" ? " tabs-item--current" : ""
74-
}`}
86+
rootClasses={tabClass(currentTab === "account")}
7587
>
7688
Account
7789
</NewLink>
7890
</Tabs>
91+
7992
<section className="settings-user__body">
8093
<Outlet context={outletContext} />
8194
</section>

0 commit comments

Comments
 (0)