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-03-28 11:18:46 +00:00
|
|
|
import { StackedLayersIcon } from "components/icons";
|
|
|
|
//components
|
2023-04-10 17:16:09 +00:00
|
|
|
import { CustomMenu, Tooltip } from "components/ui";
|
|
|
|
// 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";
|
|
|
|
// 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-07-12 06:15:45 +00:00
|
|
|
import { renderShortDateWithYearFormat, render24HourFormatTime } from "helpers/date-time.helper";
|
2023-03-28 11:18:46 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-03-31 12:24:05 +00:00
|
|
|
view: IView;
|
2023-04-12 10:03:21 +00:00
|
|
|
handleEditView: () => void;
|
|
|
|
handleDeleteView: () => void;
|
2023-03-28 11:18:46 +00:00
|
|
|
};
|
|
|
|
|
2023-04-12 10:03:21 +00:00
|
|
|
export const SingleViewItem: React.FC<Props> = ({ view, 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.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="first:rounded-t-[10px] last:rounded-b-[10px] hover:bg-custom-background-90">
|
2023-04-10 17:16:09 +00:00
|
|
|
<Link href={`/${workspaceSlug}/projects/${projectId}/views/${view.id}`}>
|
|
|
|
<a>
|
2023-04-21 11:00:16 +00:00
|
|
|
<div className="relative rounded p-4">
|
2023-04-10 17:16:09 +00:00
|
|
|
<div className="flex items-center justify-between">
|
2023-03-31 12:24:05 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<StackedLayersIcon height={18} width={18} />
|
2023-04-21 11:00:16 +00:00
|
|
|
<p className="mr-2 truncate text-sm">{truncateText(view.name, 75)}</p>
|
2023-03-31 12:24:05 +00:00
|
|
|
</div>
|
2023-04-10 17:16:09 +00:00
|
|
|
<div className="ml-2 flex flex-shrink-0">
|
|
|
|
<div className="flex items-center gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="rounded-full bg-custom-background-80 py-0.5 px-2 text-xs text-custom-text-200">
|
2023-04-10 17:16:09 +00:00
|
|
|
{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>
|
|
|
|
<Tooltip
|
2023-07-12 06:15:45 +00:00
|
|
|
tooltipContent={`Last updated at ${render24HourFormatTime(
|
2023-04-10 17:16:09 +00:00
|
|
|
view.updated_at
|
2023-07-04 12:49:19 +00:00
|
|
|
)} ${renderShortDateWithYearFormat(view.updated_at)}`}
|
2023-03-31 12:24:05 +00:00
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="text-sm text-custom-text-200">
|
2023-07-12 06:15:45 +00:00
|
|
|
{render24HourFormatTime(view.updated_at)}
|
2023-04-21 11:00:16 +00:00
|
|
|
</p>
|
2023-04-10 17:16:09 +00:00
|
|
|
</Tooltip>
|
|
|
|
{view.is_favorite ? (
|
|
|
|
<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>
|
|
|
|
)}
|
|
|
|
<CustomMenu width="auto" verticalEllipsis>
|
2023-04-12 10:03:21 +00:00
|
|
|
<CustomMenu.MenuItem
|
2023-04-10 17:16:09 +00:00
|
|
|
onClick={(e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2023-04-12 10:03:21 +00:00
|
|
|
handleEditView();
|
2023-04-10 17:16:09 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span className="flex items-center justify-start gap-2">
|
|
|
|
<PencilIcon className="h-3.5 w-3.5" />
|
|
|
|
<span>Edit View</span>
|
|
|
|
</span>
|
2023-04-12 10:03:21 +00:00
|
|
|
</CustomMenu.MenuItem>
|
2023-04-10 17:16:09 +00:00
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={(e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2023-04-12 10:03:21 +00:00
|
|
|
handleDeleteView();
|
2023-04-10 17:16:09 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<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>
|
|
|
|
</div>
|
2023-03-31 12:24:05 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{view?.description && (
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="px-[27px] text-sm font-normal leading-5 text-custom-text-200">
|
2023-03-31 12:24:05 +00:00
|
|
|
{view.description}
|
|
|
|
</p>
|
|
|
|
)}
|
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
|
|
|
);
|
|
|
|
};
|