import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { IView } from "types"; // icons import { TrashIcon, StarIcon } from "@heroicons/react/24/outline"; import { StackedLayersIcon } from "components/icons"; //components import { CustomMenu } from "components/ui"; import viewsService from "services/views.service"; import { mutate } from "swr"; import { VIEWS_LIST } from "constants/fetch-keys"; import useToast from "hooks/use-toast"; type Props = { view: IView; setSelectedView: React.Dispatch>; }; export const SingleViewItem: React.FC = ({ view, setSelectedView }) => { 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 (
{view.is_favorite ? ( ) : ( )} { e.preventDefault(); e.stopPropagation(); setSelectedView(view); }} > Delete
{view?.description && (

{view.description}

)}
); };