2023-01-26 18:12:20 +00:00
|
|
|
import React, { FC, useState } from "react";
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-02-08 04:43:07 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
import useSWR from "swr";
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// services
|
|
|
|
import projectService from "services/project.service";
|
|
|
|
// hooks
|
|
|
|
import useUser from "hooks/use-user";
|
|
|
|
// ui
|
2023-02-08 04:43:07 +00:00
|
|
|
import { Button, Spinner } from "components/ui";
|
2023-01-26 18:12:20 +00:00
|
|
|
// components
|
2023-02-08 04:43:07 +00:00
|
|
|
import { NotAuthorizedView } from "components/core";
|
2023-02-10 12:32:18 +00:00
|
|
|
import { CommandPalette } from "components/command-palette";
|
2023-01-26 18:12:20 +00:00
|
|
|
import { JoinProject } from "components/project";
|
|
|
|
// local components
|
|
|
|
import Container from "layouts/container";
|
2023-02-08 04:43:07 +00:00
|
|
|
import AppSidebar from "layouts/app-layout/app-sidebar";
|
|
|
|
import AppHeader from "layouts/app-layout/app-header";
|
|
|
|
// types
|
|
|
|
import { UserAuth } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { PROJECT_MEMBERS } from "constants/fetch-keys";
|
2023-03-03 07:59:36 +00:00
|
|
|
import SettingsNavbar from "layouts/settings-navbar";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
2023-02-08 04:43:07 +00:00
|
|
|
type Meta = {
|
2023-01-26 18:12:20 +00:00
|
|
|
title?: string | null;
|
|
|
|
description?: string | null;
|
|
|
|
image?: string | null;
|
|
|
|
url?: string | null;
|
|
|
|
};
|
|
|
|
|
2023-02-08 04:43:07 +00:00
|
|
|
type AppLayoutProps = {
|
2023-01-26 18:12:20 +00:00
|
|
|
meta?: Meta;
|
|
|
|
children: React.ReactNode;
|
|
|
|
noPadding?: boolean;
|
|
|
|
bg?: "primary" | "secondary";
|
|
|
|
noHeader?: boolean;
|
|
|
|
breadcrumbs?: JSX.Element;
|
|
|
|
left?: JSX.Element;
|
|
|
|
right?: JSX.Element;
|
2023-03-03 07:59:36 +00:00
|
|
|
settingsLayout?: boolean;
|
2023-02-08 04:43:07 +00:00
|
|
|
memberType?: UserAuth;
|
|
|
|
};
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const AppLayout: FC<AppLayoutProps> = ({
|
|
|
|
meta,
|
|
|
|
children,
|
|
|
|
noPadding = false,
|
|
|
|
bg = "primary",
|
|
|
|
noHeader = false,
|
|
|
|
breadcrumbs,
|
|
|
|
left,
|
|
|
|
right,
|
2023-03-03 07:59:36 +00:00
|
|
|
settingsLayout = false,
|
2023-02-08 04:43:07 +00:00
|
|
|
memberType,
|
2023-01-26 18:12:20 +00:00
|
|
|
}) => {
|
|
|
|
// states
|
|
|
|
const [toggleSidebar, setToggleSidebar] = useState(false);
|
|
|
|
const [isJoiningProject, setIsJoiningProject] = useState(false);
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const { user } = useUser();
|
2023-02-08 04:43:07 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const { data: projectMembers, mutate: projectMembersMutate } = useSWR(
|
|
|
|
workspaceSlug && projectId ? PROJECT_MEMBERS(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectService.projectMembers(workspaceSlug as string, projectId as string)
|
|
|
|
: null,
|
|
|
|
{
|
|
|
|
shouldRetryOnError: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// flags
|
|
|
|
const isMember = projectMembers?.find((member) => member.member.id === user?.id) || !projectId;
|
|
|
|
|
|
|
|
const handleJoin = () => {
|
|
|
|
setIsJoiningProject(true);
|
|
|
|
projectService
|
|
|
|
.joinProject(workspaceSlug as string, {
|
|
|
|
project_ids: [projectId as string],
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
setIsJoiningProject(false);
|
|
|
|
projectMembersMutate();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container meta={meta}>
|
|
|
|
<CommandPalette />
|
|
|
|
<div className="flex h-screen w-full overflow-x-hidden">
|
|
|
|
<AppSidebar toggleSidebar={toggleSidebar} setToggleSidebar={setToggleSidebar} />
|
2023-02-08 04:43:07 +00:00
|
|
|
{settingsLayout && (memberType?.isGuest || memberType?.isViewer) ? (
|
|
|
|
<NotAuthorizedView
|
|
|
|
actionButton={
|
|
|
|
(memberType?.isViewer || memberType?.isGuest) && projectId ? (
|
|
|
|
<Link href={`/${workspaceSlug}/projects/${projectId}/issues`}>
|
|
|
|
<Button size="sm" theme="secondary">
|
|
|
|
Go to Issues
|
|
|
|
</Button>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
(memberType?.isViewer || memberType?.isGuest) &&
|
|
|
|
workspaceSlug && (
|
|
|
|
<Link href={`/${workspaceSlug}`}>
|
|
|
|
<Button size="sm" theme="secondary">
|
|
|
|
Go to workspace
|
|
|
|
</Button>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
2023-03-03 07:59:36 +00:00
|
|
|
<main className="flex h-screen w-full min-w-0 flex-col overflow-y-auto">
|
|
|
|
{!noHeader && (
|
|
|
|
<AppHeader
|
|
|
|
breadcrumbs={breadcrumbs}
|
|
|
|
left={left}
|
|
|
|
right={right}
|
|
|
|
setToggleSidebar={setToggleSidebar}
|
2023-02-08 04:43:07 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-03-03 07:59:36 +00:00
|
|
|
{projectId && !projectMembers ? (
|
|
|
|
<div className="flex h-full w-full items-center justify-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
) : isMember ? (
|
|
|
|
<div
|
2023-03-04 12:17:03 +00:00
|
|
|
className={`flex w-full flex-grow flex-col ${
|
2023-03-03 07:59:36 +00:00
|
|
|
noPadding ? "" : settingsLayout ? "p-9 lg:px-32 lg:pt-9" : "p-9"
|
|
|
|
} ${
|
|
|
|
bg === "primary"
|
|
|
|
? "bg-primary"
|
|
|
|
: bg === "secondary"
|
|
|
|
? "bg-secondary"
|
|
|
|
: "bg-primary"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{settingsLayout && (
|
|
|
|
<div className="mb-12 space-y-6">
|
|
|
|
<div>
|
|
|
|
<h3 className="text-3xl font-semibold">
|
|
|
|
{projectId ? "Project" : "Workspace"} Settings
|
|
|
|
</h3>
|
|
|
|
<p className="mt-1 text-gray-600">
|
|
|
|
This information will be displayed to every member of the project.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<SettingsNavbar />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<JoinProject isJoiningProject={isJoiningProject} handleJoin={handleJoin} />
|
|
|
|
)}
|
|
|
|
</main>
|
2023-02-08 04:43:07 +00:00
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AppLayout;
|