mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-17 02:16:38 +00:00
86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { internal } from "@app/lib/api";
|
|
import { authCookieHeader } from "@app/lib/api/cookies";
|
|
import ProfileIcon from "@app/components/ProfileIcon";
|
|
import { verifySession } from "@app/lib/auth/verifySession";
|
|
import UserProvider from "@app/providers/UserProvider";
|
|
import { GetOrgResponse } from "@server/routers/org";
|
|
import { GetOrgUserResponse } from "@server/routers/user";
|
|
import { AxiosResponse } from "axios";
|
|
import { redirect } from "next/navigation";
|
|
import { cache } from "react";
|
|
import SetLastOrgCookie from "@app/components/SetLastOrgCookie";
|
|
import PrivateSubscriptionStatusProvider from "@app/providers/SubscriptionStatusProvider";
|
|
import { GetOrgSubscriptionResponse } from "#private/routers/billing/getOrgSubscription";
|
|
import { pullEnv } from "@app/lib/pullEnv";
|
|
import { build } from "@server/build";
|
|
|
|
export default async function OrgLayout(props: {
|
|
children: React.ReactNode;
|
|
params: Promise<{ orgId: string }>;
|
|
}) {
|
|
const cookie = await authCookieHeader();
|
|
const params = await props.params;
|
|
const orgId = params.orgId;
|
|
const env = pullEnv();
|
|
|
|
if (!orgId) {
|
|
redirect(`/`);
|
|
}
|
|
|
|
const getUser = cache(verifySession);
|
|
const user = await getUser();
|
|
|
|
if (!user) {
|
|
redirect(`/`);
|
|
}
|
|
|
|
try {
|
|
const getOrgUser = cache(() =>
|
|
internal.get<AxiosResponse<GetOrgUserResponse>>(
|
|
`/org/${orgId}/user/${user.userId}`,
|
|
cookie
|
|
)
|
|
);
|
|
const orgUser = await getOrgUser();
|
|
} catch {
|
|
redirect(`/`);
|
|
}
|
|
|
|
try {
|
|
const getOrg = cache(() =>
|
|
internal.get<AxiosResponse<GetOrgResponse>>(`/org/${orgId}`, cookie)
|
|
);
|
|
await getOrg();
|
|
} catch {
|
|
redirect(`/`);
|
|
}
|
|
|
|
let subscriptionStatus = null;
|
|
if (build != "oss") {
|
|
try {
|
|
const getSubscription = cache(() =>
|
|
internal.get<AxiosResponse<GetOrgSubscriptionResponse>>(
|
|
`/org/${orgId}/billing/subscription`,
|
|
cookie
|
|
)
|
|
);
|
|
const subRes = await getSubscription();
|
|
subscriptionStatus = subRes.data.data;
|
|
} catch (error) {
|
|
// If subscription fetch fails, keep subscriptionStatus as null
|
|
console.error("Failed to fetch subscription status:", error);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<PrivateSubscriptionStatusProvider
|
|
subscriptionStatus={subscriptionStatus}
|
|
env={env.app.environment}
|
|
sandbox_mode={env.app.sandbox_mode}
|
|
>
|
|
{props.children}
|
|
<SetLastOrgCookie orgId={orgId} />
|
|
</PrivateSubscriptionStatusProvider>
|
|
);
|
|
}
|