mirror of
https://github.com/fosrl/pangolin.git
synced 2026-01-28 22:00:51 +00:00
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import LicenseStatusContext from "@app/contexts/licenseStatusContext";
|
|
import { LicenseStatus } from "@server/license/license";
|
|
import { useState } from "react";
|
|
|
|
interface ProviderProps {
|
|
children: React.ReactNode;
|
|
licenseStatus: LicenseStatus | null;
|
|
}
|
|
|
|
export function LicenseStatusProvider({
|
|
children,
|
|
licenseStatus
|
|
}: ProviderProps) {
|
|
const [licenseStatusState, setLicenseStatusState] =
|
|
useState<LicenseStatus | null>(licenseStatus);
|
|
|
|
const updateLicenseStatus = (updatedLicenseStatus: LicenseStatus) => {
|
|
setLicenseStatusState((prev) => {
|
|
return {
|
|
...updatedLicenseStatus
|
|
};
|
|
});
|
|
};
|
|
|
|
const isUnlocked = () => {
|
|
if (licenseStatusState?.isHostLicensed) {
|
|
if (licenseStatusState?.isLicenseValid) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
const isLicenseViolation = () => {
|
|
if (
|
|
licenseStatusState?.isHostLicensed &&
|
|
!licenseStatusState?.isLicenseValid
|
|
) {
|
|
return true;
|
|
}
|
|
if (
|
|
licenseStatusState?.maxSites &&
|
|
licenseStatusState?.usedSites &&
|
|
licenseStatusState.usedSites > licenseStatusState.maxSites
|
|
) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
return (
|
|
<LicenseStatusContext.Provider
|
|
value={{
|
|
licenseStatus: licenseStatusState,
|
|
updateLicenseStatus,
|
|
isLicenseViolation,
|
|
isUnlocked
|
|
}}
|
|
>
|
|
{children}
|
|
</LicenseStatusContext.Provider>
|
|
);
|
|
}
|
|
|
|
export default LicenseStatusProvider;
|