Create LicenseKey page

This commit is contained in:
Mathias Wagner
2024-07-22 17:21:29 +02:00
parent e93fe35059
commit e7c439c920
3 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import {Button, TextField, Typography} from "@mui/material";
import {Key} from "@mui/icons-material";
import {useContext} from "react";
import {ProjectContext} from "@/states/Dashboard/contexts/Project";
import {replaceLicenseDefaults} from "./util.js";
export const LicenseKey = ({licenseKey, setLicenseKey, goBack}) => {
const {currentProject} = useContext(ProjectContext);
const generateKey = () => {
const key = replaceLicenseDefaults(currentProject.defaults.licenseKey);
setLicenseKey(key);
goBack();
}
return (
<>
<Typography variant="h5" fontWeight={700}>New key</Typography>
<TextField variant="outlined" fullWidth size="small" value={licenseKey} onChange={(event) => setLicenseKey(event.target.value)}
InputProps={{startAdornment: <Key sx={{color: 'text.secondary', mr: 1}}/>}}/>
<Button variant="outlined" color="primary" sx={{mt: 1}} onClick={generateKey}>
Generate new key
</Button>
</>
);
}

View File

@ -0,0 +1 @@
export {LicenseKey as default} from "./LicenseKey.jsx";

View File

@ -0,0 +1,41 @@
export const generateCharacter = () => {
return String.fromCharCode(Math.random() < 0.5 ? Math.floor(Math.random() * 26) + 65 : Math.floor(Math.random() * 26) + 97);
}
export const generateSpecialCharacter = () => {
const specialChars = "!@#$%^&*()_+-=[]{};':\",./<>?";
return specialChars[Math.floor(Math.random() * specialChars.length)];
}
export const generateAlphaNumeric = () => {
const alphanumericChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return alphanumericChars[Math.floor(Math.random() * alphanumericChars.length)];
}
export const generateRandom = () => {
const allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[]{};':\",./<>?";
return allChars[Math.floor(Math.random() * allChars.length)];
}
export const replaceLicenseDefaults = (defaultKey) => {
return defaultKey.split('').map(char => {
switch (char) {
case 'N':
return String(Math.floor(Math.random() * 10));
case 'C':
return generateCharacter();
case 'L':
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
case 'U':
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
case 'S':
return generateSpecialCharacter();
case 'A':
return generateAlphaNumeric();
case 'R':
return generateRandom();
default:
return char;
}
}).join('');
}