import React from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import type { NextPage, NextPageContext } from "next"; import useSWR from "swr"; // hooks import useUser from "lib/hooks/useUser"; // lib import { requiredAuth } from "lib/auth"; // services import userService from "lib/services/user.service"; import projectService from "lib/services/project.service"; import workspaceService from "lib/services/workspace.service"; // layouts import AppLayout from "layouts/app-layout"; // ui import { Loader, Spinner } from "ui"; // icons import { ArrowRightIcon, CalendarDaysIcon, ClipboardDocumentListIcon, } from "@heroicons/react/24/outline"; // types import type { IIssue } from "types"; // fetch-keys import { PROJECTS_LIST, USER_ISSUE, WORKSPACE_DETAILS } from "constants/fetch-keys"; // common import { addSpaceIfCamelCase, findHowManyDaysLeft, groupBy, renderShortNumericDateFormat, } from "constants/common"; import { LayerDiagonalIcon } from "ui/icons"; const Workspace: NextPage = () => { const { user } = useUser(); const router = useRouter(); const { workspaceSlug } = router.query; const { data: myIssues } = useSWR( workspaceSlug ? USER_ISSUE(workspaceSlug as string) : null, workspaceSlug ? () => userService.userIssues(workspaceSlug as string) : null ); const { error: workspaceDetailError } = useSWR( workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null, workspaceSlug ? () => workspaceService.getWorkspace(workspaceSlug as string) : null ); const groupedIssues = { backlog: [], unstarted: [], started: [], cancelled: [], completed: [], ...groupBy(myIssues ?? [], "state_detail.group"), }; const { data: projects } = useSWR( workspaceSlug ? PROJECTS_LIST(workspaceSlug as string) : null, workspaceSlug ? () => projectService.getProjects(workspaceSlug as string) : null ); const hours = new Date().getHours(); const cards = [ { title: "Issues completed", number: groupedIssues.completed.length, }, { title: "Issues pending", number: myIssues?.length ?? 0 - groupedIssues.completed.length, }, { title: "Projects", number: projects?.length ?? 0, }, ]; if (workspaceDetailError?.status === 404) { router.push("/404"); return null; } else if (workspaceDetailError) { router.push("/error"); return null; } return (
{user ? (
Good{" "} {hours >= 4 && hours < 12 ? "Morning" : hours >= 12 && hours < 17 ? "Afternoon" : "Evening"} , {user.first_name}!
) : ( )}
{cards.map((card, index) => (

{card.title}

{card.number}

))}
{myIssues ? ( myIssues.length > 0 ? (

My Issues

{myIssues.length}

{myIssues.map((issue) => (
{issue.priority ?? "None"}
{addSpaceIfCamelCase(issue.state_detail.name)}
{issue.target_date ? renderShortNumericDateFormat(issue.target_date) : "N/A"}
Target date
{renderShortNumericDateFormat(issue.target_date ?? "")}
{issue.target_date && (issue.target_date < new Date().toISOString() ? `Target date has passed by ${findHowManyDaysLeft( issue.target_date )} days` : findHowManyDaysLeft(issue.target_date) <= 3 ? `Target date is in ${findHowManyDaysLeft( issue.target_date )} days` : "Target date")}
))}
) : (

No issues found. Create a new issue with{" "}
Ctrl/Command + I
.

) ) : (
)}

Recent Projects

{projects && workspaceSlug ? ( projects.length > 0 ? ( projects .sort((a, b) => Date.parse(`${a.updated_at}`) - Date.parse(`${b.updated_at}`)) .map( (project, index) => index < 3 && (
{project.icon ? ( {String.fromCodePoint(parseInt(project.icon))} ) : ( {project?.name.charAt(0)} )}

{project.name}

) ) ) : (

No projects has been create for this workspace.

) ) : (
)}
); }; export const getServerSideProps = async (ctx: NextPageContext) => { const user = await requiredAuth(ctx.req?.headers.cookie); const redirectAfterSignIn = ctx.req?.url; if (!user) { return { redirect: { destination: `/signin?next=${redirectAfterSignIn}`, permanent: false, }, }; } return { props: { user, }, }; }; export default Workspace;