import React from "react"; import { mutate } from "swr"; import Link from "next/link"; import { useRouter } from "next/router"; // icons import { TrashIcon, StarIcon, PencilIcon } from "@heroicons/react/24/outline"; import { StackedLayersIcon } from "components/icons"; //components import { CustomMenu, Tooltip } from "components/ui"; // services import viewsService from "services/views.service"; // types import { IView } from "types"; // fetch keys import { VIEWS_LIST } from "constants/fetch-keys"; // hooks import useToast from "hooks/use-toast"; // helpers import { truncateText } from "helpers/string.helper"; import { renderShortDateWithYearFormat, render24HourFormatTime } from "helpers/date-time.helper"; type Props = { view: IView; handleEditView: () => void; handleDeleteView: () => void; }; export const SingleViewItem: React.FC = ({ view, handleEditView, handleDeleteView }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const handleAddToFavorites = () => { if (!workspaceSlug || !projectId || !view) return; mutate( 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( 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 (

{truncateText(view.name, 75)}

{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

{render24HourFormatTime(view.updated_at)}

{view.is_favorite ? ( ) : ( )} { e.preventDefault(); e.stopPropagation(); handleEditView(); }} > Edit View { e.preventDefault(); e.stopPropagation(); handleDeleteView(); }} > Delete View
{view?.description && (

{view.description}

)}
); };