Enhance License Key Management: Add loading state for license key validation, implement query to check for valid license keys, and improve UI feedback during license key checks.

This commit is contained in:
Mauricio Siu
2026-01-29 07:58:50 -06:00
parent 346216fc71
commit 2b52332e43
2 changed files with 170 additions and 143 deletions
@@ -1,4 +1,4 @@
import { Key } from "lucide-react"; import { Key, Loader2 } from "lucide-react";
import Link from "next/link"; import Link from "next/link";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { toast } from "sonner"; import { toast } from "sonner";
@@ -20,26 +20,29 @@ export function LicenseKeySettings() {
api.licenseKey.validate.useMutation(); api.licenseKey.validate.useMutation();
const { mutateAsync: deactivateLicenseKey, isLoading: isDeactivating } = const { mutateAsync: deactivateLicenseKey, isLoading: isDeactivating } =
api.licenseKey.deactivate.useMutation(); api.licenseKey.deactivate.useMutation();
const { data: haveValidLicenseKey, isLoading: isCheckingLicenseKey } =
api.licenseKey.haveValidLicenseKey.useQuery();
const [licenseKey, setLicenseKey] = useState(""); const [licenseKey, setLicenseKey] = useState("");
const [isValid, setIsValid] = useState(false);
useEffect(() => { useEffect(() => {
if (data?.licenseKey) { if (data?.licenseKey) {
setLicenseKey(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; const enabled = !!data?.enableEnterpriseFeatures;
return ( return (
<div className="flex flex-col gap-4 rounded-lg border p-4"> <div className="flex flex-col gap-4 rounded-lg border p-4">
{isCheckingLicenseKey ? (
<div className="flex items-center gap-2 justify-center min-h-[25vh]">
<Loader2 className="size-6 text-muted-foreground animate-spin" />
<span className="text-sm text-muted-foreground">
Checking license key...
</span>
</div>
) : (
<>
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<div className="flex items-center justify-between gap-4"> <div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
@@ -71,8 +74,8 @@ export function LicenseKeySettings() {
</div> </div>
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
To unlock extra features you need an enterprise license key. Contact To unlock extra features you need an enterprise license key.
us{" "} Contact us{" "}
<Link <Link
href="http://localhost:3001/contact" href="http://localhost:3001/contact"
target="_blank" target="_blank"
@@ -84,7 +87,6 @@ export function LicenseKeySettings() {
. .
</p> </p>
</div> </div>
{enabled && ( {enabled && (
<div className="grid gap-3 md:grid-cols-[1fr_auto] md:items-end"> <div className="grid gap-3 md:grid-cols-[1fr_auto] md:items-end">
<div className="space-y-2"> <div className="space-y-2">
@@ -99,7 +101,7 @@ export function LicenseKeySettings() {
/> />
</div> </div>
<div className="md:justify-self-end flex gap-2"> <div className="md:justify-self-end flex gap-2">
{isValid && ( {haveValidLicenseKey && (
<DialogAction <DialogAction
title="Deactivate License Key" title="Deactivate License Key"
description="Are you sure you want to deactivate this license key? This will disable enterprise features." description="Are you sure you want to deactivate this license key? This will disable enterprise features."
@@ -107,7 +109,7 @@ export function LicenseKeySettings() {
try { try {
await deactivateLicenseKey(); await deactivateLicenseKey();
await utils.licenseKey.getEnterpriseSettings.invalidate(); await utils.licenseKey.getEnterpriseSettings.invalidate();
setIsValid(false); await utils.licenseKey.haveValidLicenseKey.invalidate();
toast.success("License key deactivated"); toast.success("License key deactivated");
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -118,22 +120,28 @@ export function LicenseKeySettings() {
); );
} }
}} }}
disabled={isDeactivating || !data?.licenseKey} disabled={isDeactivating || !haveValidLicenseKey}
> >
<Button <Button
variant="destructive" variant="destructive"
disabled={isDeactivating || !data?.licenseKey} disabled={isDeactivating || !haveValidLicenseKey}
isLoading={isDeactivating}
> >
Deactivate Deactivate
</Button> </Button>
</DialogAction> </DialogAction>
)} )}
{haveValidLicenseKey && (
<Button <Button
variant="outline" variant="outline"
disabled={isSaving || isValidating || isDeactivating} disabled={
isSaving || isCheckingLicenseKey || isDeactivating
}
isLoading={isValidating}
onClick={async () => { onClick={async () => {
try { try {
const valid = await validateLicenseKey({ licenseKey }); const valid = await validateLicenseKey({ licenseKey });
console.log("valid", valid);
if (valid) { if (valid) {
toast.success("License key is valid"); toast.success("License key is valid");
} else { } else {
@@ -151,17 +159,17 @@ export function LicenseKeySettings() {
> >
Validate Validate
</Button> </Button>
{!isValid && ( )}
{!haveValidLicenseKey && (
<Button <Button
variant="secondary" variant="secondary"
disabled={isSaving || isValidating || isDeactivating} disabled={isSaving || isValidating || isDeactivating}
isLoading={isActivating}
onClick={async () => { onClick={async () => {
try { try {
await activateLicenseKey({ licenseKey }); await activateLicenseKey({ licenseKey });
await utils.licenseKey.getEnterpriseSettings.invalidate(); await utils.licenseKey.getEnterpriseSettings.invalidate();
// Re-validate after saving to update the Deactivate button visibility await utils.licenseKey.haveValidLicenseKey.invalidate();
const valid = await validateLicenseKey({ licenseKey });
setIsValid(valid);
toast.success("License key activated"); toast.success("License key activated");
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -179,6 +187,8 @@ export function LicenseKeySettings() {
</div> </div>
</div> </div>
)} )}
</>
)}
</div> </div>
); );
} }
@@ -126,6 +126,23 @@ export const licenseKeyRouter = createTRPCRouter({
licenseKey: currentUser.licenseKey ?? "", 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 updateEnterpriseSettings: adminProcedure
.input( .input(