Overhaul of config-editor

This commit is contained in:
mcchampions committed 2024-08-14 20:44:10 +08:00
1 parent 4eb1aa0851
commit 939209b4ba
1 file changed
+37 -47
+37 -47
View File
@@ -8,7 +8,8 @@ import { useRef, useState } from 'react';
const ConfigEditorPage: React.FC = () => { const ConfigEditorPage: React.FC = () => {
const [config, setConfig] = useState(''); const [config, setConfig] = useState('');
const [parsedConfig, setParsedConfig] = useState<any>({}); const [parsedConfig, setParsedConfig] = useState<any>({});
// match optional key
const keyReg = /^#? ?[^:\sA-Z]+:(?!\S)/
const editedConfig = useRef<any>({}); const editedConfig = useRef<any>({});
const uploadRef = useRef<any>(null); const uploadRef = useRef<any>(null);
@@ -55,7 +56,8 @@ const ConfigEditorPage: React.FC = () => {
const newConfig = {}; const newConfig = {};
// Parse each line of the config // Parse each line of the config
lines.forEach(line => { for (let i = 0; i < lines.length; i++) {
let line = lines[i];
// Reset the section if we are no indented // Reset the section if we are no indented
if (!line.startsWith(' ')) { if (!line.startsWith(' ')) {
currentSection = ''; currentSection = '';
@@ -65,38 +67,30 @@ const ConfigEditorPage: React.FC = () => {
line = line.trim() line = line.trim()
if (line === '') { if (line === '') {
currentComment = ''; currentComment = '';
return; continue;
} }
// Check if we are in a comment const commented = line.startsWith('#');
if (line.startsWith('#')) {
if (!line.includes('# ') && line.includes(': ')) {
const splitLine = line.split(':');
splitLine[0] = splitLine[0].trim();
splitLine[1] = splitLine[1].trim();
if (splitLine[0] === '#proxy-protocol-whitelisted-ips') {
currentComment = '';
return;
}
currentComment += '<strong>Note: This option is commented on, and if you change it, it will be automatically uncommented.</strong>' + '<br>';
// Add the config option to the config object
newConfig[currentSection] = newConfig[currentSection] || {};
newConfig[currentSection][splitLine[0]] = {
desc: currentComment,
value: splitLine[1]
};
// Reset the comment const match = line.match(keyReg)
currentComment = ''; if (match === null) {
return; // Check if we are in a comment
if (commented) {
currentComment += line.replace(/^# ?/i, '') + '<br>';
} }
currentComment += line.replace(/^# ?/i, '') + '<br>'; continue;
return;
} }
// Ignore these lines since they will fail to parse // Check if we are in a new section
if (!line.includes(':')) { if (line.endsWith(':')) {
return; currentSection = line.replace(':', '');
currentComment = '';
continue;
}
// Ignore user auths section
if (currentSection === 'saved-user-logins') {
continue;
} }
// Get the key and value // Get the key and value
@@ -104,29 +98,26 @@ const ConfigEditorPage: React.FC = () => {
splitLine[0] = splitLine[0].trim(); splitLine[0] = splitLine[0].trim();
splitLine[1] = splitLine[1].trim(); splitLine[1] = splitLine[1].trim();
// Check if we are in a new section if (splitLine[0] === '#proxy-protocol-whitelisted-ips') {
if (line.endsWith(':')) {
currentSection = splitLine[0];
currentComment = ''; currentComment = '';
return; continue;
} }
// Ignore user auths section if (commented) {
if (currentSection === 'saved-user-logins') { currentComment += '<strong>Note: This option is commented on, and if you change it, it will be automatically uncommented.</strong>' + '<br>';
return;
} }
// Add the config option to the config object // Add the config option to the config object
newConfig[currentSection] = newConfig[currentSection] || {}; newConfig[currentSection] = newConfig[currentSection] || {};
newConfig[currentSection][splitLine[0]] = { newConfig[currentSection][splitLine[0]] = {
desc: currentComment, desc: currentComment,
value: splitLine[1] value: splitLine[1],
line: i
}; };
// Reset the comment // Reset the comment
currentComment = ''; currentComment = '';
}) }
return newConfig; return newConfig;
} }
@@ -138,7 +129,7 @@ const ConfigEditorPage: React.FC = () => {
{Object.keys(config[configKey]).map(configOption => { {Object.keys(config[configKey]).map(configOption => {
const configOptionInfo = config[configKey][configOption]; const configOptionInfo = config[configKey][configOption];
const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`; const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`;
const showOptionName = configOption.replace('#','') const showOptionName = configOption.replace('#','').trim()
return ( return (
<div key={configOptionKey} className={styles.configOption}> <div key={configOptionKey} className={styles.configOption}>
<h3>{showOptionName}</h3> <h3>{showOptionName}</h3>
@@ -154,7 +145,7 @@ const ConfigEditorPage: React.FC = () => {
const handleExport = () => { const handleExport = () => {
let newConfig = config; let newConfig = config;
const lines = newConfig.split('\n');
Object.entries(editedConfig.current).forEach(([key, value]) => { Object.entries(editedConfig.current).forEach(([key, value]) => {
const keys = key.split('.'); const keys = key.split('.');
let currentObj = parsedConfig; let currentObj = parsedConfig;
@@ -162,19 +153,18 @@ const ConfigEditorPage: React.FC = () => {
keys.forEach((nestedKey, index) => { keys.forEach((nestedKey, index) => {
if (currentObj.hasOwnProperty(nestedKey)) { if (currentObj.hasOwnProperty(nestedKey)) {
if (index === keys.length - 1) { if (index === keys.length - 1) {
const oldValue = currentObj[nestedKey].value; const option = currentObj[nestedKey];
if (nestedKey.startsWith('#')) { const oldValue = option.value;
const newKey = nestedKey.replace('#',''); const index = option.line;
newConfig = newConfig.replace(` ${nestedKey}: ${oldValue}`, ` ${newKey}: ${value}`); const newKey = nestedKey.replace('#','').trim();
} else { lines[index] = lines[index].replace(` ${nestedKey}: ${oldValue}`,` ${newKey}: ${value}`);
newConfig = newConfig.replace(` ${nestedKey}: ${oldValue}`, ` ${nestedKey}: ${value}`);
}
} else { } else {
currentObj = currentObj[nestedKey]; currentObj = currentObj[nestedKey];
} }
} }
}); });
}); });
newConfig = lines.join('\n');
// Trigger a download of the final config // Trigger a download of the final config
downloadString(newConfig, 'text/plain', 'config.yml'); downloadString(newConfig, 'text/plain', 'config.yml');