2023-01-26 18:12:20 +00:00
|
|
|
import React, { useState, FC } from "react";
|
2023-03-06 13:08:01 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-03-06 13:08:01 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
import useTheme from "hooks/use-theme";
|
2023-06-06 16:06:00 +00:00
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-07-13 14:14:53 +00:00
|
|
|
import useProjects from "hooks/use-projects";
|
2023-01-26 18:12:20 +00:00
|
|
|
// components
|
2023-07-13 14:14:53 +00:00
|
|
|
import { DeleteProjectModal, SingleSidebarProject } from "components/project";
|
|
|
|
// icons
|
|
|
|
import { PlusIcon } from "@heroicons/react/24/outline";
|
2023-01-26 18:12:20 +00:00
|
|
|
// helpers
|
2023-03-06 13:08:01 +00:00
|
|
|
import { copyTextToClipboard } from "helpers/string.helper";
|
|
|
|
// types
|
2023-07-13 14:14:53 +00:00
|
|
|
import { IProject } from "types";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export const ProjectSidebarList: FC = () => {
|
2023-03-06 13:08:01 +00:00
|
|
|
const [deleteProjectModal, setDeleteProjectModal] = useState(false);
|
|
|
|
const [projectToDelete, setProjectToDelete] = useState<IProject | null>(null);
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// router
|
|
|
|
const router = useRouter();
|
2023-03-06 13:08:01 +00:00
|
|
|
const { workspaceSlug } = router.query;
|
2023-06-06 16:06:00 +00:00
|
|
|
|
|
|
|
const { user } = useUserAuth();
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const { collapsed: sidebarCollapse } = useTheme();
|
|
|
|
const { setToastAlert } = useToast();
|
2023-03-04 12:17:03 +00:00
|
|
|
|
2023-07-13 14:14:53 +00:00
|
|
|
const { projects: allProjects } = useProjects();
|
2023-07-14 09:38:07 +00:00
|
|
|
const favoriteProjects = allProjects?.filter((p) => p.is_favorite);
|
2023-03-06 13:08:01 +00:00
|
|
|
|
|
|
|
const handleDeleteProject = (project: IProject) => {
|
|
|
|
setProjectToDelete(project);
|
|
|
|
setDeleteProjectModal(true);
|
|
|
|
};
|
|
|
|
|
2023-02-23 05:24:54 +00:00
|
|
|
const handleCopyText = (projectId: string) => {
|
|
|
|
const originURL =
|
|
|
|
typeof window !== "undefined" && window.location.origin ? window.location.origin : "";
|
|
|
|
copyTextToClipboard(`${originURL}/${workspaceSlug}/projects/${projectId}/issues`).then(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Link Copied!",
|
|
|
|
message: "Project link copied to clipboard.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-03-06 13:08:01 +00:00
|
|
|
<DeleteProjectModal
|
|
|
|
isOpen={deleteProjectModal}
|
|
|
|
onClose={() => setDeleteProjectModal(false)}
|
|
|
|
data={projectToDelete}
|
2023-06-06 16:06:00 +00:00
|
|
|
user={user}
|
2023-03-06 13:08:01 +00:00
|
|
|
/>
|
2023-07-13 14:14:53 +00:00
|
|
|
<div className="h-full overflow-y-auto px-4">
|
2023-03-04 12:17:03 +00:00
|
|
|
{favoriteProjects && favoriteProjects.length > 0 && (
|
2023-07-13 14:14:53 +00:00
|
|
|
<div className="flex flex-col space-y-2 mt-5">
|
2023-07-10 07:17:00 +00:00
|
|
|
{!sidebarCollapse && (
|
2023-07-13 13:05:43 +00:00
|
|
|
<h5 className="text-sm font-medium text-custom-sidebar-text-200">Favorites</h5>
|
2023-07-10 07:17:00 +00:00
|
|
|
)}
|
2023-07-13 14:14:53 +00:00
|
|
|
{favoriteProjects.map((project) => (
|
|
|
|
<SingleSidebarProject
|
|
|
|
key={project.id}
|
|
|
|
project={project}
|
|
|
|
sidebarCollapse={sidebarCollapse}
|
|
|
|
handleDeleteProject={() => handleDeleteProject(project)}
|
|
|
|
handleCopyText={() => handleCopyText(project.id)}
|
|
|
|
shortContextMenu
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{allProjects && allProjects.length > 0 && (
|
|
|
|
<div className="flex flex-col space-y-2 mt-5">
|
|
|
|
{!sidebarCollapse && (
|
|
|
|
<h5 className="text-sm font-medium text-custom-sidebar-text-200">Projects</h5>
|
|
|
|
)}
|
|
|
|
{allProjects.map((project) => (
|
|
|
|
<SingleSidebarProject
|
|
|
|
key={project.id}
|
|
|
|
project={project}
|
|
|
|
sidebarCollapse={sidebarCollapse}
|
|
|
|
handleDeleteProject={() => handleDeleteProject(project)}
|
|
|
|
handleCopyText={() => handleCopyText(project.id)}
|
|
|
|
/>
|
|
|
|
))}
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2023-07-13 14:14:53 +00:00
|
|
|
{allProjects && allProjects.length === 0 && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="flex w-full items-center gap-2 px-3 py-2 text-sm text-custom-sidebar-text-200 mt-5"
|
|
|
|
onClick={() => {
|
|
|
|
const e = new KeyboardEvent("keydown", {
|
|
|
|
key: "p",
|
|
|
|
});
|
|
|
|
document.dispatchEvent(e);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PlusIcon className="h-5 w-5" />
|
|
|
|
{!sidebarCollapse && "Add Project"}
|
|
|
|
</button>
|
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|