Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[license] Updated the LicenseInfo class with 2 additional checks #16404

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
showLicenseKeyPlanMismatchError,
showExpiredPackageVersionError,
showNotAvailableInInitialProPlanError,
showLicenseKeyNotSetError,
showSetLicenseKeyNotCalledError,
} from '../utils/licenseErrorMessageUtils';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import MuiLicenseInfoContext from '../Unstable_LicenseInfoProvider/MuiLicenseInfoContext';
Expand All @@ -31,6 +33,12 @@ export function useLicenseVerifier(
} {
const { key: contextKey } = React.useContext(MuiLicenseInfoContext);
return React.useMemo(() => {
if (!LicenseInfo.getHasSetLicenseKeyRun()) {
showSetLicenseKeyNotCalledError();
}
if (!LicenseInfo.getIsLicenseKeySet()) {
showLicenseKeyNotSetError();
}
Comment on lines +36 to +41
Copy link
Member

@oliviertassinari oliviertassinari Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error looks out of place, I got the impression that LICENSE_STATUS is meant for this.

const licenseKey = contextKey ?? LicenseInfo.getLicenseKey();

// Cache the response to not trigger the error twice.
Expand Down
22 changes: 22 additions & 0 deletions packages/x-license/src/utils/licenseErrorMessageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ function showError(message: string[]) {
);
}

export function showSetLicenseKeyNotCalledError() {
showError([
'MUI X: `setLicenseKey` was never called.',
'',
'The `setLicenseKey()` function was never called. This might be because you did not call `setLicenseKey()` during runtime',
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
'',
'To solve the issue, you need to double check that `setLicenseKey()` is called with the right argument during runtime',
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
'Please check the license key installation instructions: https://mui.com/r/x-license-key-installation.',
mapache-salvaje marked this conversation as resolved.
Show resolved Hide resolved
]);
}

export function showLicenseKeyNotSetError() {
showError([
'MUI X: License key not set.',
'',
'The license key is not set. This might be because you did not call `setLicenseKey()` or called it without an argument.',
'',
'To solve the issue, you need to double check that `setLicenseKey()` is called with the right argument',
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
'Please check the license key installation instructions: https://mui.com/r/x-license-key-installation.',
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
]);
}

export function showInvalidLicenseKeyError() {
showError([
'MUI X: Invalid license key.',
Expand Down
14 changes: 14 additions & 0 deletions packages/x-license/src/utils/licenseInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ ponyfillGlobal.__MUI_LICENSE_INFO__ = ponyfillGlobal.__MUI_LICENSE_INFO__ || {
};

export class LicenseInfo {
private static hasSetLicenseKeyRun: boolean = false;

private static isLicenseKeySet: boolean = false;

private static getLicenseInfo() {
// eslint-disable-next-line no-underscore-dangle
return ponyfillGlobal.__MUI_LICENSE_INFO__;
Expand All @@ -29,5 +33,15 @@ export class LicenseInfo {
public static setLicenseKey(key: string) {
const licenseInfo = LicenseInfo.getLicenseInfo();
licenseInfo.key = key;
LicenseInfo.isLicenseKeySet = !!key;
LicenseInfo.hasSetLicenseKeyRun = true;
}

public static getIsLicenseKeySet(): boolean {
return LicenseInfo.isLicenseKeySet;
}

public static getHasSetLicenseKeyRun(): boolean {
return LicenseInfo.hasSetLicenseKeyRun;
Comment on lines +36 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do this with less code, no?

Suggested change
LicenseInfo.isLicenseKeySet = !!key;
LicenseInfo.hasSetLicenseKeyRun = true;
}
public static getIsLicenseKeySet(): boolean {
return LicenseInfo.isLicenseKeySet;
}
public static getHasSetLicenseKeyRun(): boolean {
return LicenseInfo.hasSetLicenseKeyRun;
}

and then

 ponyfillGlobal.__MUI_LICENSE_INFO__ = ponyfillGlobal.__MUI_LICENSE_INFO__ || {
-  key: undefined,
+  key: 'default',
 };

}
}