// warning: this file is even worse than Background.jsx
// if anyone wants to rewrite it to be actually decent, feel free
// otherwise it will be cleaned up probably when alex asks me to add something here :(
// - david
import variables from 'modules/variables';
import { PureComponent } from 'react';
import {
MdSettings as Settings,
MdOutlineInsertPhoto as Photos,
MdOutlineFormatQuote as Quotes,
MdUpload as ImportIcon,
MdDownload as ExportIcon,
MdArrowBack,
MdDownload,
MdOpenInNew,
} from 'react-icons/md';
import { TextField } from '@mui/material';
import { toast } from 'react-toastify';
import SettingsItem from '../../../main/settings/SettingsItem';
import { saveFile } from 'modules/helpers/settings/modals';
import Tooltip from '../../../../helpers/tooltip/Tooltip';
import FileUpload from '../../settings/FileUpload';
import Dropdown from '../../settings/Dropdown';
import '../../../welcome/welcome.scss';
export default class Create extends PureComponent {
constructor() {
super();
this.state = {
currentTab: 1,
addonMetadata: {
name: '',
description: '',
type: '',
version: '',
author: '',
icon_url: '',
screenshot_url: '',
},
addonData: '',
settingsClasses: {
current: 'toggle lightTheme',
json: 'toggle lightTheme',
},
};
}
changeTab(tab, type) {
if (type) {
return this.setState({
currentTab: tab,
addonMetadata: {
type: type,
},
});
} else {
this.setState({
currentTab: tab,
});
}
}
importSettings(input) {
const data = input || localStorage;
let settings = {};
Object.keys(data).forEach((key) => {
if (
key === 'statsData' ||
key === 'firstRun' ||
key === 'showWelcome' ||
key === 'language' ||
key === 'installed' ||
key === 'stats' ||
key === 'backup_settings' ||
key === 'showReminder' ||
key === 'experimental' ||
key === 'debugtimeout' ||
key === 'quotelanguage'
) {
return;
}
settings[key] = localStorage.getItem(key);
});
this.setState({
addonData: settings,
settingsClasses: {
current: input ? 'toggle lightTheme active' : 'toggle lightTheme',
json: input ? 'toggle lightTheme active' : 'toggle lightTheme',
},
});
toast(variables.language.getMessage(variables.languagecode, 'toasts.imported'));
}
updateQuotePackType(type) {
const addonMetadata = {
type,
name: this.state.addonMetadata.name,
description: this.state.addonMetadata.description,
version: this.state.addonMetadata.version,
author: this.state.addonMetadata.author,
icon_url: this.state.addonMetadata.icon_url,
screenshot_url: this.state.addonMetadata.screenshot_url,
};
if (type === 'quotePack') {
this.setState({
addonMetadata: {
addonMetadata,
quotes: [],
},
});
} else {
this.setState({
addonMetadata: {
addonMetadata,
},
addonData: {
url: '',
name: '',
author: '',
},
});
}
}
updateQuotePackAPI(type, data) {
this.setState({
addonData: {
url: type === 'url' ? data : this.state.addonData.url || '',
name: type === 'name' ? data : this.state.addonData.name || '',
author: type === 'author' ? data : this.state.addonData.author || '',
},
});
}
importQuotes() {
this.setState({
addonData: JSON.parse(localStorage.getItem('customQuote')) || [],
});
toast(variables.language.getMessage(variables.languagecode, 'toasts.imported'));
}
importPhotos() {
let data = [];
try {
const current = JSON.parse(localStorage.getItem('customBackground')) || [];
data = current.map((item) => {
return {
photographer: '???',
location: '???',
url: {
default: item,
},
};
});
toast(variables.language.getMessage(variables.languagecode, 'toasts.imported'));
} catch (e) {
console.log(e);
toast(variables.language.getMessage(variables.languagecode, 'toasts.error'));
}
this.setState({
addonData: data,
});
}
downloadAddon() {
saveFile(
{
name: this.state.addonMetadata.name,
description: this.state.addonMetadata.description,
type:
this.state.addonMetadata.type === 'quote_api' ? 'quotes' : this.state.addonMetadata.type,
version: this.state.addonMetadata.version,
author: this.state.addonMetadata.author,
icon_url: this.state.addonMetadata.icon_url,
screenshot_url: this.state.addonMetadata.screenshot_url,
[this.state.addonMetadata.type]: this.state.addonData,
},
this.state.addonMetadata.name + '.json',
);
}
render() {
let tabContent;
const getMessage = (text) => variables.language.getMessage(variables.languagecode, text);
const chooseType = (
<>
Help Centre
Home of all docs and guides on creating addons for Mue's marketplace
this.changeTab(2, 'photos')}>
{getMessage('modals.main.marketplace.photo_packs')}
this.changeTab(2, 'quotes')}>
{getMessage('modals.main.marketplace.quote_packs')}
this.changeTab(2, 'settings')}>
{getMessage('modals.main.marketplace.preset_settings')}
>
);
// todo: find a better way to do all this
const nextDescriptionDisabled = !(
this.state.addonMetadata.name !== undefined &&
this.state.addonMetadata.description !== undefined &&
this.state.addonMetadata.version !== undefined &&
this.state.addonMetadata.author !== undefined &&
this.state.addonMetadata.icon_url !== undefined &&
this.state.addonMetadata.screenshot_url !== undefined
);
const setMetadata = (data, type) => {
this.setState({
addonMetadata: {
name: type === 'name' ? data : this.state.addonMetadata.name,
description: type === 'description' ? data : this.state.addonMetadata.description,
version: type === 'version' ? data : this.state.addonMetadata.version,
author: type === 'author' ? data : this.state.addonMetadata.author,
icon_url: type === 'icon_url' ? data : this.state.addonMetadata.icon_url,
screenshot_url:
type === 'screenshot_url' ? data : this.state.addonMetadata.screenshot_url,
type: this.state.addonMetadata.type,
},
});
};
const writeDescription = (
<>
Create {this.state.addonMetadata.type} Pack
Description of what is being made
setMetadata(e.target.value, 'name')}
/>
setMetadata(e.target.value, 'version')}
/>
setMetadata(e.target.value, 'author')}
/>
setMetadata(e.target.value, 'icon_url')}
/>
setMetadata(e.target.value, 'screenshot_url')}
/>
setMetadata(e.target.value, 'description')}
/>
>
);
// settings
const nextSettingsDisabled = this.state.addonData === '';
const importSettings = (
<>
this.importSettings()}
>
{getMessage('modals.main.addons.create.settings.current')}
document.getElementById('file-input').click()}
>
{getMessage('modals.main.addons.create.settings.json')}
this.importSettings(JSON.parse(e.target.result))}
/>
>
);
// quotes
const nextQuotesDisabled = !(
(this.state.addonMetadata.type === 'quote_api' &&
this.state.addonData.url !== '' &&
this.state.addonData.name !== '' &&
this.state.addonData.author !== '') ||
(this.state.addonMetadata.type === 'quotes' && this.state.addonData.quotes !== '')
);
const addQuotes = (
<>
this.updateQuotePackType(e)}
>
{this.state.addonMetadata.type === 'quote_api' ? (
<>
this.updateQuotePack(e.target.value, 'url')}
/>
this.updateQuotePack(e.target.value, 'name')}
/>
this.updateQuotePack(e.target.value, 'author')}
/>
>
) : (
this.importQuotes()}
className="toggle lightTheme"
style={{ width: '60%', margin: '10px 0 10px 0' }}
>
{getMessage('modals.main.addons.create.settings.current')}
)}
>
);
// photos
const nextPhotosDisabled = !(
this.state.addonData.photos !== '' && this.state.addonData.photos !== []
);
const addPhotos = (
<>
this.importPhotos()}
className="toggle lightTheme"
style={{ width: '60%', margin: '10px 0 10px 0' }}
>
{getMessage('modals.main.addons.create.settings.current')}
>
);
const downloadAddon = (
<>
Next step, Publishing...
Visit the Mue Knowledgebase on information on how to publish your newly created addon.
this.downloadAddon()}
className="toggle lightTheme"
style={{ width: '60%', margin: '10px 0 10px 0' }}
>
{getMessage('modals.main.addons.create.finish.download')}
{/**/}
>
);
switch (this.state.currentTab) {
case 2:
tabContent = writeDescription;
break;
case 'settings':
tabContent = importSettings;
break;
case 'quotes':
tabContent = addQuotes;
break;
case 'photos':
tabContent = addPhotos;
break;
case 3:
tabContent = downloadAddon;
break;
default:
tabContent = chooseType;
}
return (
<>
{this.state.currentTab !== 1 && (
this.changeTab(this.state.currentTab - 1)}
/>
)}
{getMessage('modals.main.addons.create.other_title')}
{tabContent}
>
);
}
}