107 lines
4.1 KiB
TypeScript
107 lines
4.1 KiB
TypeScript
import { LoaderFunctionArgs, MetaFunction } from "react-router";
|
|
import { Outlet, useParams } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import AddressBookIcon from "~/components/ui/icons/crm/AddressBookIcon";
|
|
import AddressBookIconFilled from "~/components/ui/icons/crm/AddressBookIconFilled";
|
|
import CompanyIcon from "~/components/ui/icons/crm/CompanyIcon";
|
|
import CompanyIconFilled from "~/components/ui/icons/crm/CompanyIconFilled";
|
|
import FormIcon from "~/components/ui/icons/crm/FormIcon";
|
|
import FormIconFilled from "~/components/ui/icons/crm/FormIconFilled";
|
|
import IncreaseIcon from "~/components/ui/icons/crm/IncreaseIcon";
|
|
import IncreaseIconFilled from "~/components/ui/icons/crm/IncreaseIconFilled";
|
|
import UsDollarCircled from "~/components/ui/icons/crm/UsDollarCircled";
|
|
import UsDollarCircledFilled from "~/components/ui/icons/crm/UsDollarCircledFilled";
|
|
import SidebarIconsLayout, { IconDto } from "~/components/ui/layouts/SidebarIconsLayout";
|
|
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
|
import CrmService, { CrmConfigurationError } from "~/modules/crm/services/CrmService";
|
|
import { getTenantIdOrNull } from "~/utils/services/.server/urlService";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { featureKeys } from "@toolctrl/entitlements";
|
|
import { requireTenantProductAccess } from "~/utils/entitlements/.server/productAccess.server";
|
|
import { translateEntityTitlePlural } from "~/utils/i18n/translateLabel";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
await requireTenantProductAccess({ request, params, featureKey: featureKeys.crmBasic });
|
|
const { t } = await getTranslations(request);
|
|
const data: LoaderData = {
|
|
title: t("pages.adminCrm.metaTitle", { appName: process.env.APP_NAME }),
|
|
};
|
|
const tenantId = await getTenantIdOrNull({ request, params });
|
|
try {
|
|
await CrmService.validate(tenantId);
|
|
} catch (error) {
|
|
if (error instanceof CrmConfigurationError) {
|
|
throw new Response(t("crm.newsletter.errors.integrationMissing"), { status: 503 });
|
|
}
|
|
throw error;
|
|
}
|
|
return data;
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export default () => {
|
|
const { t, i18n } = useTranslation();
|
|
const appOrAdminData = useAppOrAdminData();
|
|
const params = useParams();
|
|
const items: IconDto[] = [
|
|
{
|
|
name: t("shared.overview"),
|
|
href: UrlUtils.getModulePath(params, "crm"),
|
|
exact: true,
|
|
icon: <IncreaseIcon className="h-5 w-5" />,
|
|
iconSelected: <IncreaseIconFilled className="h-5 w-5" />,
|
|
},
|
|
];
|
|
["opportunities", "companies", "contacts"].forEach((slug) => {
|
|
const entity = appOrAdminData.entities.find((x) => x.slug === slug);
|
|
if (entity) {
|
|
items.push({
|
|
name: translateEntityTitlePlural(t, i18n, entity),
|
|
href: UrlUtils.getModulePath(params, `crm/${slug}`),
|
|
icon: getIcons(entity.slug)?.icon,
|
|
iconSelected: getIcons(entity.slug)?.iconSelected,
|
|
});
|
|
}
|
|
});
|
|
|
|
function getIcons(entitySlug: string) {
|
|
if (entitySlug === "opportunities") {
|
|
return {
|
|
icon: <UsDollarCircled className="h-5 w-5" />,
|
|
iconSelected: <UsDollarCircledFilled className="h-5 w-5" />,
|
|
};
|
|
} else if (entitySlug === "companies") {
|
|
return {
|
|
icon: <CompanyIcon className="h-5 w-5" />,
|
|
iconSelected: <CompanyIconFilled className="h-5 w-5" />,
|
|
};
|
|
} else if (entitySlug === "contacts") {
|
|
return {
|
|
icon: <AddressBookIcon className="h-5 w-5" />,
|
|
iconSelected: <AddressBookIconFilled className="h-5 w-5" />,
|
|
};
|
|
} else if (entitySlug === "submissions") {
|
|
return {
|
|
icon: <FormIcon className="h-5 w-5" />,
|
|
iconSelected: <FormIconFilled className="h-5 w-5" />,
|
|
};
|
|
}
|
|
}
|
|
return (
|
|
<SidebarIconsLayout label={{ align: "right" }} items={items}>
|
|
<Outlet />
|
|
</SidebarIconsLayout>
|
|
);
|
|
};
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|