93 lines
4.8 KiB
TypeScript
93 lines
4.8 KiB
TypeScript
import { createContext, useContext, useEffect, useMemo, useState, type ReactNode } from "react";
|
||
import { apiOrigin } from "../lib/api";
|
||
import { resolveDeploymentAsset } from "~/lib/deployment-assets";
|
||
|
||
export interface DeploymentActivation {
|
||
isActivated: boolean;
|
||
serverFingerprint: string;
|
||
fingerprintLength: number;
|
||
profileVersion: string;
|
||
issuedAtUtc?: string;
|
||
activationCode?: string;
|
||
}
|
||
|
||
export interface DeploymentProfile {
|
||
activation: DeploymentActivation;
|
||
profileVersion: string;
|
||
company: { displayName: string; shortName: string; englishName?: string };
|
||
branding: { dashboardLoginLogoAssetKey:string; adminLoginLogoAssetKey:string; ecosystemCenterLogoSource:"dashboard"|"admin"; applicationTitle: string; adminTitle: string; loginTitle: string; loginSubtitle: string; loginDescription?: string; footerText?: string };
|
||
theme: { primaryColor: string; secondaryColor: string; accentColor: string };
|
||
dashboard: { layout: string; visibleMetrics: string[]; showRevenueSeries: boolean; costSeriesLabel: string; units: { dataKey: string; title: string; imageAssetKey: string; imageColumn: number; imageRow: number; infoColumn: number; infoRow: number; metrics: string[] }[]; ecosystemCenter: { label: string; imageAssetKey: string; width: number; height: number } };
|
||
features: { showAdminLink: boolean; reportingModules: string[]; adminModules: string[] };
|
||
}
|
||
|
||
interface DeploymentProfileContextValue { profile: DeploymentProfile; assetUrl: (key?: string) => string; refreshProfile: () => Promise<void> }
|
||
const DeploymentProfileContext = createContext<DeploymentProfileContextValue | null>(null);
|
||
|
||
export function DeploymentProfileProvider({ children }: { children: ReactNode }) {
|
||
const [profile, setProfile] = useState<DeploymentProfile | null>(null);
|
||
const [failed, setFailed] = useState(false);
|
||
const assetUrl = useMemo(
|
||
() => (key?: string) => resolveDeploymentAsset(`${apiOrigin}/api/v1`, key),
|
||
[],
|
||
);
|
||
|
||
async function refreshProfile() {
|
||
const response = await fetch(`${apiOrigin}/api/v1/deployment/profile`, {
|
||
credentials: "include", headers: { Accept: "application/json", "Cache-Control": "no-cache" }
|
||
});
|
||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||
setProfile(await response.json() as DeploymentProfile);
|
||
setFailed(false);
|
||
}
|
||
|
||
useEffect(() => {
|
||
const controller = new AbortController();
|
||
fetch(`${apiOrigin}/api/v1/deployment/profile`, { credentials: "include", headers: { Accept: "application/json" }, signal: controller.signal })
|
||
.then(async response => {
|
||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||
return response.json() as Promise<DeploymentProfile>;
|
||
})
|
||
.then(setProfile)
|
||
.catch(reason => {
|
||
if (reason instanceof DOMException && reason.name === "AbortError") return;
|
||
console.error("Deployment profile could not be loaded.", reason);
|
||
setFailed(true);
|
||
});
|
||
return () => controller.abort();
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (!profile) return;
|
||
const previousTitle = document.title;
|
||
document.documentElement.style.setProperty("--deployment-primary", profile.theme.primaryColor);
|
||
document.documentElement.style.setProperty("--deployment-secondary", profile.theme.secondaryColor);
|
||
document.documentElement.style.setProperty("--deployment-accent", profile.theme.accentColor);
|
||
document.documentElement.style.setProperty("--green", profile.theme.primaryColor);
|
||
document.documentElement.style.setProperty("--blue", profile.theme.secondaryColor);
|
||
document.documentElement.style.setProperty("--danger", profile.theme.accentColor);
|
||
document.documentElement.dataset.deploymentProfile = profile.profileVersion;
|
||
document.title = `${profile.branding.adminTitle} - ${profile.company.shortName}`;
|
||
{
|
||
let link = document.querySelector<HTMLLinkElement>('link[data-deployment-favicon="true"]');
|
||
if (!link) { link = document.createElement("link"); link.rel = "icon"; link.dataset.deploymentFavicon = "true"; document.head.appendChild(link); }
|
||
link.href = "/product-logo.svg";
|
||
}
|
||
return () => {
|
||
document.title = previousTitle;
|
||
};
|
||
}, [assetUrl, profile]);
|
||
|
||
if (failed) return <main className="profile-state"><h1>تنظیمات استقرار در دسترس نیست</h1><p>اتصال API و فایل پروفایل فعال را بررسی کنید.</p></main>;
|
||
if (!profile) return <main className="profile-state"><p>در حال دریافت تنظیمات استقرار…</p></main>;
|
||
return <DeploymentProfileContext.Provider value={{ profile, assetUrl, refreshProfile }}>{children}</DeploymentProfileContext.Provider>;
|
||
}
|
||
|
||
export function useDeploymentProfile() {
|
||
const value = useContext(DeploymentProfileContext);
|
||
if (!value) throw new Error("useDeploymentProfile must be used inside DeploymentProfileProvider.");
|
||
return value;
|
||
}
|
||
|
||
|