fosrl.pangolin/src/app/[orgId]/settings/layout.tsx

130 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-10-13 22:24:02 -04:00
import { Metadata } from "next";
import { TopbarNav } from "./components/TopbarNav";
2024-11-16 12:18:22 -05:00
import { Cog, Combine, Settings, Users, Waypoints } from "lucide-react";
2024-10-13 22:24:02 -04:00
import Header from "./components/Header";
2024-10-13 22:49:14 -04:00
import { verifySession } from "@app/lib/auth/verifySession";
import { redirect } from "next/navigation";
import { internal } from "@app/api";
import { AxiosResponse } from "axios";
2024-10-19 16:37:40 -04:00
import { GetOrgResponse, ListOrgsResponse } from "@server/routers/org";
import { authCookieHeader } from "@app/api/cookies";
import { cache } from "react";
2024-11-09 23:59:19 -05:00
import { GetOrgUserResponse } from "@server/routers/user";
export const dynamic = "force-dynamic";
2024-10-13 18:33:41 -04:00
export const metadata: Metadata = {
title: `Settings - Pangolin`,
2024-12-16 22:49:16 -05:00
description: ""
2024-10-13 22:24:02 -04:00
};
2024-10-13 18:33:41 -04:00
2024-10-13 22:24:02 -04:00
const topNavItems = [
2024-10-13 18:33:41 -04:00
{
title: "Sites",
href: "/{orgId}/settings/sites",
2024-12-16 22:49:16 -05:00
icon: <Combine className="h-4 w-4" />
2024-10-13 18:33:41 -04:00
},
{
title: "Resources",
href: "/{orgId}/settings/resources",
2024-12-16 22:49:16 -05:00
icon: <Waypoints className="h-4 w-4" />
2024-10-13 18:33:41 -04:00
},
2024-10-13 22:49:14 -04:00
{
title: "Access",
href: "/{orgId}/settings/access",
2024-12-16 22:49:16 -05:00
icon: <Users className="h-4 w-4" />
2024-10-13 22:49:14 -04:00
},
{
title: "General",
href: "/{orgId}/settings/general",
2024-12-16 22:49:16 -05:00
icon: <Settings className="h-4 w-4" />
}
2024-10-13 22:24:02 -04:00
];
2024-10-13 18:33:41 -04:00
interface SettingsLayoutProps {
2024-10-13 22:24:02 -04:00
children: React.ReactNode;
2024-10-23 13:30:23 -04:00
params: Promise<{ orgId: string }>;
2024-10-13 18:33:41 -04:00
}
export default async function SettingsLayout(props: SettingsLayoutProps) {
2024-10-23 13:30:23 -04:00
const params = await props.params;
const { children } = props;
const getUser = cache(verifySession);
const user = await getUser();
2024-10-13 22:49:14 -04:00
if (!user) {
2024-11-28 00:11:13 -05:00
redirect(`/?redirect=/${params.orgId}/`);
2024-10-13 22:49:14 -04:00
}
2024-10-23 13:30:23 -04:00
const cookie = await authCookieHeader();
try {
2024-11-09 23:59:19 -05:00
const getOrgUser = cache(() =>
internal.get<AxiosResponse<GetOrgUserResponse>>(
`/org/${params.orgId}/user/${user.userId}`,
2024-12-16 22:49:16 -05:00
cookie
)
);
2024-11-09 23:59:19 -05:00
const orgUser = await getOrgUser();
2024-11-14 00:13:37 -05:00
if (!orgUser.data.data.isAdmin && !orgUser.data.data.isOwner) {
2024-11-09 23:59:19 -05:00
throw new Error("User is not an admin or owner");
}
} catch {
2024-11-09 23:59:19 -05:00
redirect(`/${params.orgId}`);
}
2024-10-19 16:37:40 -04:00
let orgs: ListOrgsResponse["orgs"] = [];
2024-10-26 22:02:58 -04:00
try {
const getOrgs = cache(() =>
2024-12-16 22:49:16 -05:00
internal.get<AxiosResponse<ListOrgsResponse>>(`/orgs`, cookie)
2024-10-26 22:02:58 -04:00
);
const res = await getOrgs();
2024-10-26 22:02:58 -04:00
if (res && res.data.data.orgs) {
orgs = res.data.data.orgs;
}
} catch (e) {
console.error("Error fetching orgs", e);
}
2024-10-13 18:33:41 -04:00
return (
<>
<div className="w-full border-b bg-neutral-100 dark:bg-neutral-800 mb-6 select-none sm:px-0 px-3 pt-3">
2024-10-15 23:52:58 -04:00
<div className="container mx-auto flex flex-col content-between gap-4 ">
2024-10-19 16:37:40 -04:00
<Header
email={user.email}
orgId={params.orgId}
2024-10-19 16:37:40 -04:00
orgs={orgs}
/>
2024-10-14 00:02:55 -04:00
<TopbarNav items={topNavItems} orgId={params.orgId} />
2024-10-13 18:33:41 -04:00
</div>
</div>
2024-10-13 22:24:02 -04:00
2024-10-13 22:49:14 -04:00
<div className="container mx-auto sm:px-0 px-3">{children}</div>
2024-12-16 22:49:16 -05:00
<footer className="w-full mt-6 py-3">
<div className="container mx-auto flex justify-end items-center px-3 sm:px-0 text-sm text-muted space-x-3">
<div>Built by Fossorial</div>
<a
href="https://github.com/fosrl/pangolin"
target="_blank"
rel="noopener noreferrer"
aria-label="GitHub"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
className="w-5 h-5"
>
<path d="M12 0C5.37 0 0 5.373 0 12c0 5.303 3.438 9.8 8.207 11.385.6.11.82-.26.82-.577v-2.17c-3.338.726-4.042-1.61-4.042-1.61-.546-1.385-1.333-1.755-1.333-1.755-1.09-.744.082-.73.082-.73 1.205.085 1.84 1.24 1.84 1.24 1.07 1.835 2.807 1.305 3.492.997.107-.775.42-1.305.763-1.605-2.665-.305-5.467-1.335-5.467-5.93 0-1.31.468-2.382 1.236-3.22-.123-.303-.535-1.523.117-3.176 0 0 1.008-.322 3.3 1.23a11.52 11.52 0 013.006-.403c1.02.005 2.045.137 3.006.403 2.29-1.552 3.295-1.23 3.295-1.23.654 1.653.242 2.873.12 3.176.77.838 1.235 1.91 1.235 3.22 0 4.605-2.805 5.623-5.475 5.92.43.37.814 1.1.814 2.22v3.293c0 .32.217.693.825.576C20.565 21.795 24 17.298 24 12 24 5.373 18.627 0 12 0z" />
</svg>
</a>
</div>
</footer>
2024-10-13 18:33:41 -04:00
</>
2024-10-13 22:24:02 -04:00
);
2024-10-13 18:33:41 -04:00
}