Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,181 @@
|
||||
import { Link, LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { useSearchParams } from "react-router";
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { DashboardLoaderData, loadDashboardData } from "~/utils/data/useDashboardData";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAppDashboardStats } from "~/utils/services/appDashboardService";
|
||||
import ProfileBanner from "~/components/app/ProfileBanner";
|
||||
import { DashboardStats } from "~/components/ui/stats/DashboardStats";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { Stat } from "~/application/dtos/stats/Stat";
|
||||
import InputSelect from "~/components/ui/input/InputSelect";
|
||||
import PeriodHelper, { defaultPeriodFilter, PeriodFilters } from "~/utils/helpers/PeriodHelper";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import { serverTimingHeaders } from "~/modules/metrics/utils/defaultHeaders";
|
||||
import { createMetrics } from "~/modules/metrics/services/.server/MetricTracker";
|
||||
import { promiseHash } from "~/utils/promises/promiseHash";
|
||||
import { getTenant } from "~/utils/db/tenants.db.server";
|
||||
import { Fragment } from "react";
|
||||
import { loadAppData } from "~/utils/data/.server/appData";
|
||||
import { getUserHasPermission } from "~/utils/helpers/PermissionsHelper";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
||||
import { appTenantPlanRequiredPath } from "~/utils/app/AppTenantStatusPaths";
|
||||
import {
|
||||
evaluateDashboardModuleGates,
|
||||
getDashboardModuleDiagnostics,
|
||||
getVisibleDashboardModules,
|
||||
type DashboardModuleGateDiagnostic,
|
||||
} from "~/services/dashboard-registry/gating.server";
|
||||
import type { DashboardModuleDefinition, DashboardModuleId } from "~/services/dashboard-registry/registry.server";
|
||||
export { serverTimingHeaders as headers };
|
||||
|
||||
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
||||
|
||||
type LoaderData = DashboardLoaderData & {
|
||||
title: string;
|
||||
stats: Stat[];
|
||||
modules: DashboardModuleDefinition[];
|
||||
moduleDiagnostics: DashboardModuleGateDiagnostic[];
|
||||
};
|
||||
|
||||
function isDashboardModuleVisible(modules: readonly DashboardModuleDefinition[], moduleId: DashboardModuleId) {
|
||||
return modules.some((module) => module.id === moduleId);
|
||||
}
|
||||
|
||||
function getSafeModuleNoticePath(diagnostic: DashboardModuleGateDiagnostic) {
|
||||
if (diagnostic.source === "plan") {
|
||||
return diagnostic.redirectPath?.startsWith("/") ? diagnostic.redirectPath : "/plan-required";
|
||||
}
|
||||
|
||||
return diagnostic.redirectPath?.startsWith("/") ? diagnostic.redirectPath : "/feature-disabled";
|
||||
}
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const { time, getServerTimingHeader } = await createMetrics({ request, params }, "app.$tenant.dashboard");
|
||||
let { t } = await getTranslations(request);
|
||||
const { tenantId, appData } = await time(
|
||||
promiseHash({
|
||||
tenantId: getTenantIdFromUrl(params),
|
||||
appData: loadAppData({ request, params, t, time }),
|
||||
}),
|
||||
"app.$tenant.dashboard.context"
|
||||
);
|
||||
const tenant = await time(getTenant(tenantId), "getTenant");
|
||||
|
||||
if (!tenant) {
|
||||
throw Response.json({ error: t("app.dashboard.errors.tenantNotFound") }, { status: 404 });
|
||||
}
|
||||
await verifyUserHasPermission(request, "app.dashboard.view", tenantId);
|
||||
|
||||
const { stats, dashboardData, moduleDecisions } = await time(
|
||||
promiseHash({
|
||||
stats: getAppDashboardStats({ t, tenant, gte: PeriodHelper.getGreaterThanOrEqualsFromRequest({ request }) }),
|
||||
dashboardData: loadDashboardData({ request, params }),
|
||||
moduleDecisions: evaluateDashboardModuleGates({
|
||||
request,
|
||||
params,
|
||||
subscription: appData.mySubscription,
|
||||
redirectPaths: {
|
||||
"feature-flag": `/feature-disabled?tenant=${encodeURIComponent(tenant.slug)}`,
|
||||
plan: appTenantPlanRequiredPath({ tenant: tenant.slug }),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
"app.$tenant.dashboard.details"
|
||||
);
|
||||
|
||||
const data: LoaderData = {
|
||||
title: `${t("app.sidebar.dashboard")} | ${process.env.APP_NAME}`,
|
||||
...dashboardData,
|
||||
stats,
|
||||
modules: getVisibleDashboardModules(moduleDecisions),
|
||||
moduleDiagnostics: getDashboardModuleDiagnostics(moduleDecisions),
|
||||
};
|
||||
return Response.json(data, { headers: getServerTimingHeader() });
|
||||
};
|
||||
|
||||
export default function DashboardRoute() {
|
||||
const { t } = useTranslation();
|
||||
const appData = useAppData();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const canViewSummary = isDashboardModuleVisible(data.modules, "summary-stats");
|
||||
|
||||
return (
|
||||
<main className="relative z-0 flex-1 pb-8" data-ui-id="ui.dashboard.page">
|
||||
{/*Page header */}
|
||||
<div className="bg-background border-border hidden border-b md:block">
|
||||
<ProfileBanner user={appData.user} />
|
||||
</div>
|
||||
|
||||
<div className="mx-auto grid max-w-5xl gap-5 px-4 py-5 sm:px-8">
|
||||
{getUserHasPermission(appData, "app.dashboard.view") ? (
|
||||
<Fragment>
|
||||
{canViewSummary && data.stats.length > 0 && (
|
||||
<div className="space-y-3 truncate p-1">
|
||||
<div className="flex items-center justify-between space-x-2">
|
||||
<h3 className="text-foreground grow leading-4 font-medium">{t("app.dashboard.summary")}</h3>
|
||||
<div>
|
||||
<InputSelect
|
||||
className="w-44"
|
||||
name="period"
|
||||
value={searchParams.get("period")?.toString() ?? defaultPeriodFilter}
|
||||
options={PeriodFilters.map((f) => {
|
||||
return {
|
||||
value: f.value,
|
||||
name: t(f.name),
|
||||
};
|
||||
})}
|
||||
setValue={(value) => {
|
||||
if (value) {
|
||||
searchParams.set("period", value?.toString() ?? "");
|
||||
} else {
|
||||
searchParams.delete("period");
|
||||
}
|
||||
setSearchParams(searchParams);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DashboardStats items={data.stats} />
|
||||
</div>
|
||||
)}
|
||||
{data.moduleDiagnostics.length > 0 && (
|
||||
<div className="space-y-2">
|
||||
{data.moduleDiagnostics.map((diagnostic) => (
|
||||
<div key={diagnostic.moduleId} className="border-border bg-secondary rounded-md border border-dashed px-4 py-3 text-sm">
|
||||
<div className="text-foreground font-medium">
|
||||
{diagnostic.reason === "plan-required"
|
||||
? t("app.dashboard.moduleDiagnostics.planRequiredTitle")
|
||||
: t("app.dashboard.moduleDiagnostics.unavailableTitle")}
|
||||
</div>
|
||||
<div className="text-muted-foreground mt-1">
|
||||
{diagnostic.reason === "plan-required"
|
||||
? t("app.dashboard.moduleDiagnostics.planRequiredDescription")
|
||||
: t("app.dashboard.moduleDiagnostics.unavailableDescription")}
|
||||
</div>
|
||||
<Link className="text-theme-600 mt-2 inline-flex underline" to={getSafeModuleNoticePath(diagnostic)}>
|
||||
{diagnostic.reason === "plan-required"
|
||||
? t("app.dashboard.moduleDiagnostics.viewPlanOptions")
|
||||
: t("app.dashboard.moduleDiagnostics.learnMore")}
|
||||
</Link>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Fragment>
|
||||
) : (
|
||||
<div className="font-medium">{t("app.dashboard.noPermission")}</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
return <ServerError />;
|
||||
}
|
||||
Reference in new issue
Block a user