diff --git a/apps/dokploy/components/proprietary/license-keys/license-key.tsx b/apps/dokploy/components/proprietary/license-keys/license-key.tsx index d96a30670..3bb27e854 100644 --- a/apps/dokploy/components/proprietary/license-keys/license-key.tsx +++ b/apps/dokploy/components/proprietary/license-keys/license-key.tsx @@ -1,4 +1,4 @@ -import { Key } from "lucide-react"; +import { Key, Loader2 } from "lucide-react"; import Link from "next/link"; import { useEffect, useState } from "react"; import { toast } from "sonner"; @@ -20,164 +20,174 @@ export function LicenseKeySettings() { api.licenseKey.validate.useMutation(); const { mutateAsync: deactivateLicenseKey, isLoading: isDeactivating } = api.licenseKey.deactivate.useMutation(); - + const { data: haveValidLicenseKey, isLoading: isCheckingLicenseKey } = + api.licenseKey.haveValidLicenseKey.useQuery(); const [licenseKey, setLicenseKey] = useState(""); - const [isValid, setIsValid] = useState(false); useEffect(() => { if (data?.licenseKey) { setLicenseKey(data.licenseKey); - validateLicenseKey({ licenseKey: data.licenseKey }) - .then((valid) => { - console.log("valid", valid); - setIsValid(valid); - }) - .catch(() => setIsValid(false)); } - }, [data?.licenseKey, validateLicenseKey]); + }, [data?.licenseKey]); const enabled = !!data?.enableEnterpriseFeatures; return (
-
-
-
- - License Key -
- -
- - {enabled ? "Enabled" : "Disabled"} - - { - try { - await updateEnterpriseSettings({ - enableEnterpriseFeatures: next, - }); - await utils.licenseKey.getEnterpriseSettings.invalidate(); - toast.success("Enterprise features updated"); - } catch (error) { - console.error(error); - toast.error("Failed to update enterprise features"); - } - }} - /> -
+ {isCheckingLicenseKey ? ( +
+ + + Checking license key... +
+ ) : ( + <> +
+
+
+ + License Key +
-

- To unlock extra features you need an enterprise license key. Contact - us{" "} - - here - - . -

-
+
+ + {enabled ? "Enabled" : "Disabled"} + + { + try { + await updateEnterpriseSettings({ + enableEnterpriseFeatures: next, + }); + await utils.licenseKey.getEnterpriseSettings.invalidate(); + toast.success("Enterprise features updated"); + } catch (error) { + console.error(error); + toast.error("Failed to update enterprise features"); + } + }} + /> +
+
- {enabled && ( -
-
- - setLicenseKey(e.target.value)} - /> -
-
- {isValid && ( - { - try { - await deactivateLicenseKey(); - await utils.licenseKey.getEnterpriseSettings.invalidate(); - setIsValid(false); - toast.success("License key deactivated"); - } catch (error) { - console.error(error); - toast.error( - error instanceof Error - ? error.message - : "Failed to deactivate license key", - ); - } - }} - disabled={isDeactivating || !data?.licenseKey} +

+ To unlock extra features you need an enterprise license key. + Contact us{" "} + - - - )} - - {!isValid && ( - - )} + here + + . +

-
+ {enabled && ( +
+
+ + setLicenseKey(e.target.value)} + /> +
+
+ {haveValidLicenseKey && ( + { + try { + await deactivateLicenseKey(); + await utils.licenseKey.getEnterpriseSettings.invalidate(); + await utils.licenseKey.haveValidLicenseKey.invalidate(); + toast.success("License key deactivated"); + } catch (error) { + console.error(error); + toast.error( + error instanceof Error + ? error.message + : "Failed to deactivate license key", + ); + } + }} + disabled={isDeactivating || !haveValidLicenseKey} + > + + + )} + {haveValidLicenseKey && ( + + )} + {!haveValidLicenseKey && ( + + )} +
+
+ )} + )}
); diff --git a/apps/dokploy/server/api/routers/proprietary/license-key.ts b/apps/dokploy/server/api/routers/proprietary/license-key.ts index bea9f18f3..eeb5a483a 100644 --- a/apps/dokploy/server/api/routers/proprietary/license-key.ts +++ b/apps/dokploy/server/api/routers/proprietary/license-key.ts @@ -126,6 +126,23 @@ export const licenseKeyRouter = createTRPCRouter({ licenseKey: currentUser.licenseKey ?? "", }; }), + haveValidLicenseKey: adminProcedure.query(async ({ ctx }) => { + const currentUserId = ctx.user.id; + const currentUser = await db.query.user.findFirst({ + where: eq(user.id, currentUserId), + }); + if (!currentUser?.enableEnterpriseFeatures) { + return false; + } + if (!currentUser.licenseKey) { + return false; + } + try { + return await validateLicenseKey(currentUser.licenseKey ?? ""); + } catch (error) { + return false; + } + }), updateEnterpriseSettings: adminProcedure .input(