1- import { Outlet , useLocation , useOutletContext } from "react-router" ;
1+ import {
2+ Outlet ,
3+ useLocation ,
4+ useLoaderData ,
5+ useOutletContext ,
6+ } from "react-router" ;
27import { NewLink , Tabs } from "@thunderstore/cyberstorm" ;
38import { PageHeader } from "~/commonComponents/PageHeader/PageHeader" ;
4-
59import { type OutletContextShape } from "../../root" ;
610import "./Settings.css" ;
711import { 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
3646export 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