import React, { useEffect, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import Link from "next/link"; import { useRouter } from "next/router"; // icons import { Plus } from "lucide-react"; // components import { CreateUpdateWorkspaceViewModal } from "components/workspace"; // constants import { GLOBAL_VIEW_OPENED } from "constants/event-tracker"; import { DEFAULT_GLOBAL_VIEWS_LIST, EUserWorkspaceRoles } from "constants/workspace"; // store hooks import { useEventTracker, useGlobalView, useUser } from "hooks/store"; const ViewTab = observer((props: { viewId: string }) => { const { viewId } = props; // router const router = useRouter(); const { workspaceSlug, globalViewId } = router.query; // store hooks const { getViewDetailsById } = useGlobalView(); const view = getViewDetailsById(viewId); if (!view) return null; return ( {view.name} ); }); export const GlobalViewsHeader: React.FC = observer(() => { // states const [createViewModal, setCreateViewModal] = useState(false); const containerRef = useRef(null); // router const router = useRouter(); const { workspaceSlug, globalViewId } = router.query; // store hooks const { currentWorkspaceViews } = useGlobalView(); const { membership: { currentWorkspaceRole }, } = useUser(); const { captureEvent } = useEventTracker(); // bring the active view to the centre of the header useEffect(() => { if (globalViewId && currentWorkspaceViews) { captureEvent(GLOBAL_VIEW_OPENED, { view_id: globalViewId, view_type: ["all-issues", "assigned", "created", "subscribed"].includes(globalViewId.toString()) ? "Default" : "Custom", }); const activeTabElement = document.querySelector(`#global-view-${globalViewId.toString()}`); if (activeTabElement && containerRef.current) { const containerRect = containerRef.current.getBoundingClientRect(); const activeTabRect = activeTabElement.getBoundingClientRect(); const diff = containerRect.right - activeTabRect.right; activeTabElement.scrollIntoView({ behavior: "smooth", inline: diff > 500 ? "center" : "nearest" }); } } }, [globalViewId, currentWorkspaceViews, containerRef, captureEvent]); const isAuthorizedUser = !!currentWorkspaceRole && currentWorkspaceRole >= EUserWorkspaceRoles.MEMBER; return ( <> setCreateViewModal(false)} />
{DEFAULT_GLOBAL_VIEWS_LIST.map((tab) => ( {tab.label} ))} {currentWorkspaceViews?.map((viewId) => )}
{isAuthorizedUser && ( )}
); });