import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { PlusIcon } from "@heroicons/react/24/outline"; import { Tab } from "@headlessui/react"; // lib import { requiredAuth } from "lib/auth"; import { CyclesIcon } from "components/icons"; // services import cycleService from "services/cycles.service"; import projectService from "services/project.service"; import workspaceService from "services/workspace.service"; // layouts import AppLayout from "layouts/app-layout"; // components import CreateUpdateCycleModal from "components/project/cycles/create-update-cycle-modal"; import CycleStatsView from "components/project/cycles/stats-view"; // ui import { HeaderButton, EmptySpace, EmptySpaceItem, Loader } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons // types import { ICycle, SelectCycleType } from "types"; import type { NextPage, NextPageContext } from "next"; // fetching keys import { CYCLE_LIST, PROJECT_DETAILS, WORKSPACE_DETAILS } from "constants/fetch-keys"; const ProjectCycles: NextPage = () => { const [selectedCycle, setSelectedCycle] = useState(); const [createUpdateCycleModal, setCreateUpdateCycleModal] = useState(false); const { query: { workspaceSlug, projectId }, } = useRouter(); const { data: activeWorkspace } = useSWR( workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null, () => (workspaceSlug ? workspaceService.getWorkspace(workspaceSlug as string) : null) ); const { data: activeProject } = useSWR( activeWorkspace && projectId ? PROJECT_DETAILS(projectId as string) : null, activeWorkspace && projectId ? () => projectService.getProject(activeWorkspace.slug, projectId as string) : null ); const { data: cycles } = useSWR( activeWorkspace && projectId ? CYCLE_LIST(projectId as string) : null, activeWorkspace && projectId ? () => cycleService.getCycles(activeWorkspace.slug, projectId as string) : null ); const getCycleStatus = (startDate: string, endDate: string) => { const today = new Date(); if (today < new Date(startDate)) return "upcoming"; else if (today > new Date(endDate)) return "completed"; else return "current"; }; const currentCycles = cycles?.filter( (c) => getCycleStatus(c.start_date ?? "", c.end_date ?? "") === "current" ); const upcomingCycles = cycles?.filter( (c) => getCycleStatus(c.start_date ?? "", c.end_date ?? "") === "upcoming" ); const completedCycles = cycles?.filter( (c) => getCycleStatus(c.start_date ?? "", c.end_date ?? "") === "completed" ); useEffect(() => { if (createUpdateCycleModal) return; const timer = setTimeout(() => { setSelectedCycle(undefined); clearTimeout(timer); }, 500); }, [createUpdateCycleModal]); return ( } right={ { const e = new KeyboardEvent("keydown", { key: "q", }); document.dispatchEvent(e); }} /> } > {cycles ? ( cycles.length > 0 ? (

Current Cycle

`rounded-lg px-6 py-2 ${selected ? "bg-gray-300" : "hover:bg-gray-200"}` } > Upcoming `rounded-lg px-6 py-2 ${selected ? "bg-gray-300" : "hover:bg-gray-200"}` } > Completed
) : (
Use
Q
shortcut to create a new cycle } Icon={PlusIcon} action={() => { const e = new KeyboardEvent("keydown", { key: "q", }); document.dispatchEvent(e); }} />
) ) : ( )}
); }; 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 ProjectCycles;