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-07-31 11:40:23 +00:00
|
|
|
import { mutate } from "swr";
|
2023-03-06 13:08:01 +00:00
|
|
|
|
2023-07-31 11:40:23 +00:00
|
|
|
// react-beautiful-dnd
|
|
|
|
import { DragDropContext, Draggable, DropResult, Droppable } from "react-beautiful-dnd";
|
2023-08-01 06:54:34 +00:00
|
|
|
// headless ui
|
|
|
|
import { Disclosure } from "@headlessui/react";
|
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";
|
2023-07-31 11:40:23 +00:00
|
|
|
// services
|
|
|
|
import projectService from "services/project.service";
|
2023-07-13 14:14:53 +00:00
|
|
|
// icons
|
2023-08-01 06:54:34 +00:00
|
|
|
import { Icon } from "components/ui";
|
2023-07-13 14:14:53 +00:00
|
|
|
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";
|
2023-07-31 11:40:23 +00:00
|
|
|
import { orderArrayBy } from "helpers/array.helper";
|
2023-03-06 13:08:01 +00:00
|
|
|
// types
|
2023-07-13 14:14:53 +00:00
|
|
|
import { IProject } from "types";
|
2023-07-31 11:40:23 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { PROJECTS_LIST } from "constants/fetch-keys";
|
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-08-01 06:54:34 +00:00
|
|
|
const { workspaceSlug, projectId } = 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-08-01 06:54:34 +00:00
|
|
|
|
|
|
|
const joinedProjects = allProjects?.filter((p) => p.sort_order);
|
2023-07-14 09:38:07 +00:00
|
|
|
const favoriteProjects = allProjects?.filter((p) => p.is_favorite);
|
2023-08-01 06:54:34 +00:00
|
|
|
const otherProjects = allProjects?.filter((p) => p.sort_order === null);
|
2023-03-06 13:08:01 +00:00
|
|
|
|
2023-08-01 06:54:34 +00:00
|
|
|
const orderedJoinedProjects: IProject[] | undefined = joinedProjects
|
|
|
|
? orderArrayBy(joinedProjects, "sort_order", "ascending")
|
|
|
|
: undefined;
|
2023-07-31 11:40:23 +00:00
|
|
|
|
2023-08-01 06:54:34 +00:00
|
|
|
const orderedFavProjects: IProject[] | undefined = favoriteProjects
|
2023-07-31 11:40:23 +00:00
|
|
|
? orderArrayBy(favoriteProjects, "sort_order", "ascending")
|
2023-08-01 06:54:34 +00:00
|
|
|
: undefined;
|
2023-07-31 11:40:23 +00:00
|
|
|
|
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-07-31 11:40:23 +00:00
|
|
|
const onDragEnd = async (result: DropResult) => {
|
|
|
|
const { source, destination, draggableId } = result;
|
|
|
|
|
|
|
|
if (!destination || !workspaceSlug) return;
|
2023-08-01 06:54:34 +00:00
|
|
|
|
2023-07-31 11:40:23 +00:00
|
|
|
if (source.index === destination.index) return;
|
|
|
|
|
2023-08-01 06:54:34 +00:00
|
|
|
const projectsList =
|
|
|
|
(destination.droppableId === "joined-projects"
|
|
|
|
? orderedJoinedProjects
|
|
|
|
: orderedFavProjects) ?? [];
|
2023-07-31 11:40:23 +00:00
|
|
|
|
2023-08-01 06:54:34 +00:00
|
|
|
let updatedSortOrder = projectsList[source.index].sort_order;
|
|
|
|
|
|
|
|
if (destination.index === 0) updatedSortOrder = (projectsList[0].sort_order as number) - 1000;
|
|
|
|
else if (destination.index === projectsList.length - 1)
|
|
|
|
updatedSortOrder = (projectsList[projectsList.length - 1].sort_order as number) + 1000;
|
|
|
|
else {
|
|
|
|
const destinationSortingOrder = projectsList[destination.index].sort_order as number;
|
2023-07-31 11:40:23 +00:00
|
|
|
const relativeDestinationSortingOrder =
|
|
|
|
source.index < destination.index
|
2023-08-01 06:54:34 +00:00
|
|
|
? (projectsList[destination.index + 1].sort_order as number)
|
|
|
|
: (projectsList[destination.index - 1].sort_order as number);
|
2023-07-31 11:40:23 +00:00
|
|
|
|
|
|
|
updatedSortOrder = Math.round(
|
|
|
|
(destinationSortingOrder + relativeDestinationSortingOrder) / 2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutate<IProject[]>(
|
|
|
|
PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all" }),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
return prevData.map((p) =>
|
|
|
|
p.id === draggableId ? { ...p, sort_order: updatedSortOrder } : p
|
|
|
|
);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
await projectService
|
|
|
|
.setProjectView(workspaceSlug as string, draggableId, { sort_order: updatedSortOrder })
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Something went wrong. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-08-01 06:54:34 +00:00
|
|
|
<div className="h-full overflow-y-auto px-5 space-y-3 pt-3 border-t border-custom-sidebar-border-300">
|
2023-07-31 11:40:23 +00:00
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
|
|
|
<Droppable droppableId="favorite-projects">
|
|
|
|
{(provided) => (
|
|
|
|
<div ref={provided.innerRef} {...provided.droppableProps}>
|
|
|
|
{orderedFavProjects && orderedFavProjects.length > 0 && (
|
2023-08-01 06:54:34 +00:00
|
|
|
<Disclosure
|
|
|
|
as="div"
|
|
|
|
className="flex flex-col space-y-2"
|
|
|
|
defaultOpen={
|
|
|
|
projectId && orderedFavProjects.find((p) => p.id === projectId) ? true : false
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
{!sidebarCollapse && (
|
|
|
|
<Disclosure.Button
|
|
|
|
as="button"
|
|
|
|
type="button"
|
|
|
|
className="group flex items-center gap-1 px-1.5 text-xs font-semibold text-custom-sidebar-text-200 text-left hover:bg-custom-sidebar-background-80 rounded w-min whitespace-nowrap"
|
|
|
|
>
|
|
|
|
Favorites
|
|
|
|
<Icon
|
|
|
|
iconName={open ? "arrow_drop_down" : "arrow_right"}
|
|
|
|
className="group-hover:opacity-100 opacity-0 !text-lg"
|
2023-07-31 11:40:23 +00:00
|
|
|
/>
|
2023-08-01 06:54:34 +00:00
|
|
|
</Disclosure.Button>
|
2023-07-31 11:40:23 +00:00
|
|
|
)}
|
2023-08-01 06:54:34 +00:00
|
|
|
<Disclosure.Panel as="div" className="space-y-2">
|
|
|
|
{orderedFavProjects.map((project, index) => (
|
|
|
|
<Draggable
|
|
|
|
key={project.id}
|
|
|
|
draggableId={project.id}
|
|
|
|
index={index}
|
|
|
|
isDragDisabled={project.sort_order === null}
|
|
|
|
>
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div ref={provided.innerRef} {...provided.draggableProps}>
|
|
|
|
<SingleSidebarProject
|
|
|
|
key={project.id}
|
|
|
|
project={project}
|
|
|
|
sidebarCollapse={sidebarCollapse}
|
|
|
|
provided={provided}
|
|
|
|
snapshot={snapshot}
|
|
|
|
handleDeleteProject={() => handleDeleteProject(project)}
|
|
|
|
handleCopyText={() => handleCopyText(project.id)}
|
|
|
|
shortContextMenu
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
|
|
|
))}
|
|
|
|
</Disclosure.Panel>
|
|
|
|
{provided.placeholder}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Disclosure>
|
2023-07-31 11:40:23 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2023-07-10 07:17:00 +00:00
|
|
|
)}
|
2023-07-31 11:40:23 +00:00
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
2023-08-01 06:54:34 +00:00
|
|
|
<Droppable droppableId="joined-projects">
|
2023-07-31 11:40:23 +00:00
|
|
|
{(provided) => (
|
|
|
|
<div ref={provided.innerRef} {...provided.droppableProps}>
|
2023-08-01 06:54:34 +00:00
|
|
|
{orderedJoinedProjects && orderedJoinedProjects.length > 0 && (
|
|
|
|
<Disclosure
|
|
|
|
as="div"
|
|
|
|
className="flex flex-col space-y-2"
|
|
|
|
defaultOpen={
|
|
|
|
projectId && orderedJoinedProjects.find((p) => p.id === projectId)
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
{!sidebarCollapse && (
|
|
|
|
<Disclosure.Button
|
|
|
|
as="button"
|
|
|
|
type="button"
|
|
|
|
className="group flex items-center gap-1 px-1.5 text-xs font-semibold text-custom-sidebar-text-200 text-left hover:bg-custom-sidebar-background-80 rounded w-min whitespace-nowrap"
|
|
|
|
>
|
|
|
|
Projects
|
|
|
|
<Icon
|
|
|
|
iconName={open ? "arrow_drop_down" : "arrow_right"}
|
|
|
|
className="group-hover:opacity-100 opacity-0 !text-lg"
|
2023-07-31 11:40:23 +00:00
|
|
|
/>
|
2023-08-01 06:54:34 +00:00
|
|
|
</Disclosure.Button>
|
2023-07-31 11:40:23 +00:00
|
|
|
)}
|
2023-08-01 06:54:34 +00:00
|
|
|
<Disclosure.Panel as="div" className="space-y-2">
|
|
|
|
{orderedJoinedProjects.map((project, index) => (
|
|
|
|
<Draggable key={project.id} draggableId={project.id} index={index}>
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div ref={provided.innerRef} {...provided.draggableProps}>
|
|
|
|
<SingleSidebarProject
|
|
|
|
key={project.id}
|
|
|
|
project={project}
|
|
|
|
sidebarCollapse={sidebarCollapse}
|
|
|
|
provided={provided}
|
|
|
|
snapshot={snapshot}
|
|
|
|
handleDeleteProject={() => handleDeleteProject(project)}
|
|
|
|
handleCopyText={() => handleCopyText(project.id)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
|
|
|
))}
|
|
|
|
</Disclosure.Panel>
|
|
|
|
{provided.placeholder}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Disclosure>
|
2023-07-31 11:40:23 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2023-07-13 14:14:53 +00:00
|
|
|
)}
|
2023-07-31 11:40:23 +00:00
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
2023-08-01 06:54:34 +00:00
|
|
|
{otherProjects && otherProjects.length > 0 && (
|
|
|
|
<Disclosure
|
|
|
|
as="div"
|
|
|
|
className="flex flex-col space-y-2"
|
|
|
|
defaultOpen={projectId && otherProjects.find((p) => p.id === projectId) ? true : false}
|
|
|
|
>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
|
|
|
{!sidebarCollapse && (
|
|
|
|
<Disclosure.Button
|
|
|
|
as="button"
|
|
|
|
type="button"
|
|
|
|
className="group flex items-center gap-1 px-1.5 text-xs font-semibold text-custom-sidebar-text-200 text-left hover:bg-custom-sidebar-background-80 rounded w-min whitespace-nowrap"
|
|
|
|
>
|
|
|
|
Other Projects
|
|
|
|
<Icon
|
|
|
|
iconName={open ? "arrow_drop_down" : "arrow_right"}
|
|
|
|
className="group-hover:opacity-100 opacity-0 !text-lg"
|
|
|
|
/>
|
|
|
|
</Disclosure.Button>
|
|
|
|
)}
|
|
|
|
<Disclosure.Panel as="div" className="space-y-2">
|
|
|
|
{otherProjects?.map((project, index) => (
|
|
|
|
<SingleSidebarProject
|
|
|
|
key={project.id}
|
|
|
|
project={project}
|
|
|
|
sidebarCollapse={sidebarCollapse}
|
|
|
|
handleDeleteProject={() => handleDeleteProject(project)}
|
|
|
|
handleCopyText={() => handleCopyText(project.id)}
|
|
|
|
shortContextMenu
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Disclosure.Panel>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Disclosure>
|
|
|
|
)}
|
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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|