2023-04-12 10:03:21 +00:00
|
|
|
import React from "react";
|
2023-04-10 17:16:09 +00:00
|
|
|
import { mutate } from "swr";
|
2023-03-28 11:18:46 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
// icons
|
2023-04-10 17:16:09 +00:00
|
|
|
import { TrashIcon, StarIcon, PencilIcon } from "@heroicons/react/24/outline";
|
2023-09-22 09:52:05 +00:00
|
|
|
import { PhotoFilterOutlined } from "@mui/icons-material";
|
2023-03-28 11:18:46 +00:00
|
|
|
//components
|
2023-09-22 09:52:05 +00:00
|
|
|
import { CustomMenu } from "components/ui";
|
2023-04-10 17:16:09 +00:00
|
|
|
// services
|
2023-03-28 11:18:46 +00:00
|
|
|
import viewsService from "services/views.service";
|
2023-04-10 17:16:09 +00:00
|
|
|
// types
|
|
|
|
import { IView } from "types";
|
2023-09-25 06:17:06 +00:00
|
|
|
import { IWorkspaceView } from "types/workspace-view";
|
2023-04-10 17:16:09 +00:00
|
|
|
// fetch keys
|
2023-03-28 11:18:46 +00:00
|
|
|
import { VIEWS_LIST } from "constants/fetch-keys";
|
2023-04-10 17:16:09 +00:00
|
|
|
// hooks
|
2023-03-28 11:18:46 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2023-04-10 17:16:09 +00:00
|
|
|
// helpers
|
|
|
|
import { truncateText } from "helpers/string.helper";
|
2023-03-28 11:18:46 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-09-25 06:17:06 +00:00
|
|
|
view: IView | IWorkspaceView;
|
2023-09-22 09:52:05 +00:00
|
|
|
viewType: "project" | "workspace";
|
2023-04-12 10:03:21 +00:00
|
|
|
handleEditView: () => void;
|
|
|
|
handleDeleteView: () => void;
|
2023-03-28 11:18:46 +00:00
|
|
|
};
|
|
|
|
|
2023-09-22 09:52:05 +00:00
|
|
|
export const SingleViewItem: React.FC<Props> = ({
|
|
|
|
view,
|
|
|
|
viewType,
|
|
|
|
handleEditView,
|
|
|
|
handleDeleteView,
|
|
|
|
}) => {
|
2023-03-28 11:18:46 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const handleAddToFavorites = () => {
|
|
|
|
if (!workspaceSlug || !projectId || !view) return;
|
|
|
|
|
|
|
|
mutate<IView[]>(
|
|
|
|
VIEWS_LIST(projectId as string),
|
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((v) => ({
|
|
|
|
...v,
|
|
|
|
is_favorite: v.id === view.id ? true : v.is_favorite,
|
|
|
|
})),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
viewsService
|
|
|
|
.addViewToFavorites(workspaceSlug as string, projectId as string, {
|
|
|
|
view: view.id,
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Couldn't add the view to favorites. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveFromFavorites = () => {
|
|
|
|
if (!workspaceSlug || !view) return;
|
|
|
|
|
|
|
|
mutate<IView[]>(
|
|
|
|
VIEWS_LIST(projectId as string),
|
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((v) => ({
|
|
|
|
...v,
|
|
|
|
is_favorite: v.id === view.id ? false : v.is_favorite,
|
|
|
|
})),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
viewsService
|
|
|
|
.removeViewFromFavorites(workspaceSlug as string, projectId as string, view.id)
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Couldn't remove the view from favorites. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-22 09:52:05 +00:00
|
|
|
const viewRedirectionUrl =
|
|
|
|
viewType === "project"
|
|
|
|
? `/${workspaceSlug}/projects/${projectId}/views/${view.id}`
|
|
|
|
: `/${workspaceSlug}/workspace-views/${view.id}`;
|
|
|
|
|
2023-03-28 11:18:46 +00:00
|
|
|
return (
|
2023-09-22 09:52:05 +00:00
|
|
|
<div className="group hover:bg-custom-background-90 border-b border-custom-border-200">
|
|
|
|
<Link href={viewRedirectionUrl}>
|
|
|
|
<a className="flex items-center justify-between relative rounded px-5 py-4 w-full">
|
|
|
|
<div className="flex items-center justify-between w-full">
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
<div
|
|
|
|
className={`flex items-center justify-center h-10 w-10 rounded bg-custom-background-90 group-hover:bg-custom-background-100`}
|
|
|
|
>
|
|
|
|
<PhotoFilterOutlined className="!text-base !leading-6" />
|
2023-03-31 12:24:05 +00:00
|
|
|
</div>
|
2023-09-22 09:52:05 +00:00
|
|
|
<div className="flex flex-col">
|
|
|
|
<p className="truncate text-sm leading-4 font-medium">
|
|
|
|
{truncateText(view.name, 75)}
|
|
|
|
</p>
|
|
|
|
{view?.description && (
|
|
|
|
<p className="text-xs text-custom-text-200">{view.description}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="ml-2 flex flex-shrink-0">
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
<p className="rounded bg-custom-background-80 py-1 px-2 text-xs text-custom-text-200 opacity-0 group-hover:opacity-100">
|
|
|
|
{Object.keys(view.query_data)
|
|
|
|
.map((key: string) =>
|
|
|
|
view.query_data[key as keyof typeof view.query_data] !== null
|
|
|
|
? (view.query_data[key as keyof typeof view.query_data] as any).length
|
|
|
|
: 0
|
|
|
|
)
|
|
|
|
.reduce((curr, prev) => curr + prev, 0)}{" "}
|
|
|
|
filters
|
|
|
|
</p>
|
|
|
|
|
|
|
|
{viewType === "project" ? (
|
|
|
|
view.is_favorite ? (
|
2023-04-10 17:16:09 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
handleRemoveFromFavorites();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<StarIcon className="h-4 w-4 text-orange-400" fill="#f6ad55" />
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
handleAddToFavorites();
|
|
|
|
}}
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<StarIcon className="h-4 w-4 " color="rgb(var(--color-text-200))" />
|
2023-04-10 17:16:09 +00:00
|
|
|
</button>
|
2023-09-22 09:52:05 +00:00
|
|
|
)
|
|
|
|
) : null}
|
|
|
|
<CustomMenu width="auto" ellipsis>
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={(e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
handleEditView();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span className="flex items-center justify-start gap-2">
|
|
|
|
<PencilIcon className="h-3.5 w-3.5" />
|
|
|
|
<span>Edit View</span>
|
|
|
|
</span>
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={(e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
handleDeleteView();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span className="flex items-center justify-start gap-2">
|
|
|
|
<TrashIcon className="h-3.5 w-3.5" />
|
|
|
|
<span>Delete View</span>
|
|
|
|
</span>
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
</CustomMenu>
|
2023-03-31 12:24:05 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-03-28 11:18:46 +00:00
|
|
|
</div>
|
2023-04-10 17:16:09 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
2023-04-21 11:00:16 +00:00
|
|
|
</div>
|
2023-03-31 12:24:05 +00:00
|
|
|
);
|
|
|
|
};
|