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-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
|
|
|
|
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-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
|
|
|
|
2023-07-31 11:40:23 +00:00
|
|
|
const orderedAllProjects = allProjects
|
|
|
|
? orderArrayBy(allProjects, "sort_order", "ascending")
|
|
|
|
: [];
|
|
|
|
|
|
|
|
const orderedFavProjects = favoriteProjects
|
|
|
|
? orderArrayBy(favoriteProjects, "sort_order", "ascending")
|
|
|
|
: [];
|
|
|
|
|
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;
|
|
|
|
if (source.index === destination.index) return;
|
|
|
|
|
|
|
|
const projectList =
|
|
|
|
destination.droppableId === "all-projects" ? orderedAllProjects : orderedFavProjects;
|
|
|
|
|
|
|
|
let updatedSortOrder = projectList[source.index].sort_order;
|
|
|
|
if (destination.index === 0) {
|
|
|
|
updatedSortOrder = projectList[0].sort_order - 1000;
|
|
|
|
} else if (destination.index === projectList.length - 1) {
|
|
|
|
updatedSortOrder = projectList[projectList.length - 1].sort_order + 1000;
|
|
|
|
} else {
|
|
|
|
const destinationSortingOrder = projectList[destination.index].sort_order;
|
|
|
|
const relativeDestinationSortingOrder =
|
|
|
|
source.index < destination.index
|
|
|
|
? projectList[destination.index + 1].sort_order
|
|
|
|
: projectList[destination.index - 1].sort_order;
|
|
|
|
|
|
|
|
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-07-13 14:14:53 +00:00
|
|
|
<div className="h-full overflow-y-auto px-4">
|
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 && (
|
|
|
|
<div className="flex flex-col space-y-2 mt-5">
|
|
|
|
{!sidebarCollapse && (
|
|
|
|
<h5 className="text-sm font-medium text-custom-sidebar-text-200">
|
|
|
|
Favorites
|
|
|
|
</h5>
|
|
|
|
)}
|
|
|
|
{orderedFavProjects.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)}
|
|
|
|
shortContextMenu
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
|
|
|
))}
|
|
|
|
{provided.placeholder}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-07-10 07:17:00 +00:00
|
|
|
)}
|
2023-07-31 11:40:23 +00:00
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
|
|
|
<Droppable droppableId="all-projects">
|
|
|
|
{(provided) => (
|
|
|
|
<div ref={provided.innerRef} {...provided.droppableProps}>
|
|
|
|
{orderedAllProjects && orderedAllProjects.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>
|
|
|
|
)}
|
|
|
|
{orderedAllProjects.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>
|
|
|
|
))}
|
|
|
|
{provided.placeholder}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-07-13 14:14:53 +00:00
|
|
|
)}
|
2023-07-31 11:40:23 +00:00
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|