114 lines
5.0 KiB
TypeScript
114 lines
5.0 KiB
TypeScript
import { useState } from "react";
|
|
import { useFetcher, useParams } from "react-router";
|
|
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import AddFeedbackButton from "./buttons/AddFeedbackButton";
|
|
import ChatSupportButton from "./buttons/ChatSupportButton";
|
|
import CurrentSubscriptionButton from "./buttons/CurrentSubscriptionButton";
|
|
import OnboardingButton from "./buttons/OnboardingButton";
|
|
import ProfileButton from "./buttons/ProfileButton";
|
|
import SearchButton from "./buttons/SearchButton";
|
|
import NotificationsButton from "./buttons/NotificationsButton";
|
|
import { NotificationsDrawer } from "~/components/screens/notifications/NotificationsDrawer";
|
|
import { useNotificationsStream } from "~/components/notifications/useNotificationsStream";
|
|
import { adminPath, appPath } from "~/utils/app/CanonicalUrlUtils";
|
|
|
|
export default function NavBar({
|
|
layout,
|
|
title,
|
|
buttons,
|
|
onOpenCommandPalette,
|
|
onOpenOnboardingModal,
|
|
children,
|
|
}: {
|
|
layout?: string;
|
|
title?: string;
|
|
buttons: {
|
|
mySubscription: boolean;
|
|
feedback: boolean;
|
|
chatSupport: boolean;
|
|
search: boolean;
|
|
notifications: boolean;
|
|
onboarding: boolean;
|
|
userProfile: boolean;
|
|
};
|
|
onOpenCommandPalette: () => void;
|
|
onOpenOnboardingModal: () => void;
|
|
children?: React.ReactNode;
|
|
}) {
|
|
const appOrAdminData = useAppOrAdminData();
|
|
const rootData = useRootData();
|
|
const params = useParams();
|
|
const canReadPlatformLeads = appOrAdminData.permissions?.some(
|
|
(permission) => permission === "admin.leads.read_own" || permission === "admin.leads.read_all"
|
|
);
|
|
const isPlatformLeadAdmin = layout === "admin" && canReadPlatformLeads;
|
|
const notificationsPath = isPlatformLeadAdmin
|
|
? adminPath("leads", { mode: "host-aware" })
|
|
: layout === "admin"
|
|
? adminPath("notifications", { mode: "host-aware" })
|
|
: params.tenant
|
|
? appPath({ tenantSlug: params.tenant, path: "notifications", mode: "host-aware" })
|
|
: "/notifications";
|
|
const isTenantApp = layout === "app" && Boolean(params.tenant);
|
|
const streamPath = isPlatformLeadAdmin
|
|
? adminPath("leads/notifications-stream", { mode: "host-aware" })
|
|
: isTenantApp && params.tenant
|
|
? appPath({ tenantSlug: params.tenant, path: "notifications/stream", mode: "host-aware" })
|
|
: null;
|
|
const notificationsStream = useNotificationsStream(streamPath, []);
|
|
const notificationReadFetcher = useFetcher();
|
|
const [notificationsDrawerOpen, setNotificationsDrawerOpen] = useState(false);
|
|
const hasNotificationDrawer = isTenantApp || isPlatformLeadAdmin;
|
|
|
|
function openNotificationsDrawer() {
|
|
setNotificationsDrawerOpen(true);
|
|
if (!isPlatformLeadAdmin || !streamPath) return;
|
|
const unreadIds = notificationsStream.items.filter((item) => !item.isRead).map((item) => item.id);
|
|
if (unreadIds.length === 0) return;
|
|
const form = new FormData();
|
|
if (rootData.csrf) form.set("csrf", rootData.csrf);
|
|
for (const deliveryId of unreadIds) form.append("deliveryId", deliveryId);
|
|
void notificationReadFetcher.submit(form, { method: "post", action: streamPath });
|
|
}
|
|
return (
|
|
<div className="flex flex-1 justify-between space-x-2">
|
|
<div className="flex flex-1 items-center">{title && <div className="font-extrabold">{title}</div>}</div>
|
|
<div className="flex items-center space-x-2 md:ml-6">
|
|
{/* {layout === "app" && (
|
|
<CreditsRemaining
|
|
feature={appOrAdminData.featureSyncs}
|
|
redirectTo={appOrAdminData.currentTenant ? `/${appOrAdminData.currentTenant.slug}/settings/billing` : "/settings/billing"}
|
|
/>
|
|
)} */}
|
|
{buttons.onboarding && appOrAdminData?.onboardingSession && (
|
|
<OnboardingButton item={appOrAdminData?.onboardingSession} onClick={onOpenOnboardingModal} />
|
|
)}
|
|
{layout === "app" && buttons.mySubscription && <CurrentSubscriptionButton />}
|
|
{/* <LocaleSelector /> */}
|
|
{buttons.notifications && appOrAdminData?.user && (
|
|
<NotificationsButton
|
|
to={notificationsPath}
|
|
unseenCount={hasNotificationDrawer ? notificationsStream.unreadCount : undefined}
|
|
onOpen={hasNotificationDrawer ? openNotificationsDrawer : undefined}
|
|
/>
|
|
)}
|
|
{hasNotificationDrawer && (
|
|
<NotificationsDrawer
|
|
open={notificationsDrawerOpen}
|
|
items={notificationsStream.items}
|
|
allHref={notificationsPath}
|
|
onClose={() => setNotificationsDrawerOpen(false)}
|
|
/>
|
|
)}
|
|
{buttons.search && <SearchButton onClick={onOpenCommandPalette} />}
|
|
{layout === "app" && buttons.feedback && <AddFeedbackButton />}
|
|
{layout === "app" && buttons.chatSupport && <ChatSupportButton />}
|
|
{/* {(layout === "app" || layout === "admin") && <ThemeSelector variant="secondary" />} */}
|
|
{(layout === "app" || layout === "admin") && buttons.userProfile && <ProfileButton user={appOrAdminData?.user} layout={layout} />}
|
|
{children}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|