import React, { FC } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; // icons import { User2 } from "lucide-react"; // types import { IModule } from "@plane/types"; // ui import { Avatar, AvatarGroup, Tooltip, setPromiseToast } from "@plane/ui"; // components import { FavoriteStar } from "@/components/core"; import { ModuleQuickActions } from "@/components/modules"; // constants import { MODULE_FAVORITED, MODULE_UNFAVORITED } from "@/constants/event-tracker"; import { MODULE_STATUS } from "@/constants/module"; import { EUserProjectRoles } from "@/constants/project"; // helpers import { getDate, renderFormattedDate } from "@/helpers/date-time.helper"; // hooks import { useEventTracker, useMember, useModule, useUser } from "@/hooks/store"; import { usePlatformOS } from "@/hooks/use-platform-os"; type Props = { moduleId: string; moduleDetails: IModule; parentRef: React.RefObject; }; export const ModuleListItemAction: FC = observer((props) => { const { moduleId, moduleDetails, parentRef } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // store hooks const { membership: { currentProjectRole }, } = useUser(); const { addModuleToFavorites, removeModuleFromFavorites } = useModule(); const { getUserDetails } = useMember(); const { captureEvent } = useEventTracker(); const { isMobile } = usePlatformOS(); // derived values const endDate = getDate(moduleDetails.target_date); const startDate = getDate(moduleDetails.start_date); const renderDate = moduleDetails.start_date || moduleDetails.target_date; const moduleStatus = MODULE_STATUS.find((status) => status.value === moduleDetails.status); const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER; // handlers const handleAddToFavorites = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); if (!workspaceSlug || !projectId) return; const addToFavoritePromise = addModuleToFavorites(workspaceSlug.toString(), projectId.toString(), moduleId).then( () => { captureEvent(MODULE_FAVORITED, { module_id: moduleId, element: "Grid layout", state: "SUCCESS", }); } ); setPromiseToast(addToFavoritePromise, { loading: "Adding module to favorites...", success: { title: "Success!", message: () => "Module added to favorites.", }, error: { title: "Error!", message: () => "Couldn't add the module to favorites. Please try again.", }, }); }; const handleRemoveFromFavorites = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); if (!workspaceSlug || !projectId) return; const removeFromFavoritePromise = removeModuleFromFavorites( workspaceSlug.toString(), projectId.toString(), moduleId ).then(() => { captureEvent(MODULE_UNFAVORITED, { module_id: moduleId, element: "Grid layout", state: "SUCCESS", }); }); setPromiseToast(removeFromFavoritePromise, { loading: "Removing module from favorites...", success: { title: "Success!", message: () => "Module removed from favorites.", }, error: { title: "Error!", message: () => "Couldn't remove the module from favorites. Please try again.", }, }); }; return ( <> {moduleStatus && ( {moduleStatus.label} )} {renderDate && ( {renderFormattedDate(startDate) ?? "_ _"} - {renderFormattedDate(endDate) ?? "_ _"} )}
{moduleDetails.member_ids.length > 0 ? ( {moduleDetails.member_ids.map((member_id) => { const member = getUserDetails(member_id); return ; })} ) : ( )}
{isEditingAllowed && !moduleDetails.archived_at && ( { if (moduleDetails.is_favorite) handleRemoveFromFavorites(e); else handleAddToFavorites(e); }} selected={moduleDetails.is_favorite} /> )} {workspaceSlug && projectId && ( )} ); });