mue/src/features/marketplace/views/Added.jsx

289 lines
8.4 KiB
React
Raw Normal View History

import variables from 'config/variables';
import { PureComponent } from 'react';
import { MdUpdate, MdOutlineExtensionOff, MdSendTimeExtension } from 'react-icons/md';
2021-08-15 21:28:37 +00:00
import { toast } from 'react-toastify';
import Modal from 'react-modal';
2021-03-07 19:06:49 +00:00
import { SideloadFailedModal } from '../components/Elements/SideloadFailedModal/SideloadFailedModal';
import Item from '../components/Items/Item';
import Items from '../components/Items/Items';
import { Dropdown, FileUpload } from 'components/Form/Settings';
import { Header, CustomActions } from 'components/Layout/Settings';
import { Button } from 'components/Elements';
2021-03-07 19:06:49 +00:00
import { install, uninstall, urlParser } from 'utils/marketplace';
2021-03-13 18:15:57 +00:00
2021-08-14 19:10:48 +00:00
export default class Added extends PureComponent {
2023-03-16 11:11:18 +00:00
constructor() {
super();
2021-03-07 19:06:49 +00:00
this.state = {
installed: JSON.parse(localStorage.getItem('installed')),
2021-05-03 10:27:52 +00:00
item: {},
button: '',
showFailed: false,
failedReason: '',
};
2021-03-07 19:06:49 +00:00
this.buttons = {
uninstall: (
<Button type="settings" onClick={() => this.uninstall()} label={variables.getMessage('modals.main.marketplace.product.buttons.remove')} />
),
2021-04-16 11:26:56 +00:00
};
2021-03-07 19:06:49 +00:00
}
installAddon(input) {
let failedReason = '';
if (!input.name) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.no_name');
} else if (!input.author) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.no_author');
} else if (!input.type) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.no_type');
} else if (!input.version) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.no_version');
} else if (
input.type === 'photos' &&
(!input.photos ||
!input.photos.length ||
!input.photos[0].url ||
!input.photos[0].url.default ||
!input.photos[0].photographer ||
!input.photos[0].location)
) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.invalid_photos');
} else if (
input.type === 'quotes' &&
(!input.quotes || !input.quotes.length || !input.quotes[0].quote || !input.quotes[0].author)
) {
failedReason = variables.getMessage('modals.main.addons.sideload.errors.invalid_quotes');
}
if (failedReason !== '' && this.state.showFailed === false) {
return this.setState({
failedReason,
showFailed: true,
});
}
install(input.type, input);
toast(variables.getMessage('toasts.installed'));
variables.stats.postEvent('marketplace', 'Sideload');
}
getSideloadButton() {
return (
<Button
type="settings"
onClick={() => document.getElementById('file-input').click()}
ref={this.customDnd}
icon={<MdSendTimeExtension />}
label={variables.getMessage('modals.main.addons.sideload.title')}
/>
);
}
2021-04-14 12:45:11 +00:00
toggle(type, data) {
2021-03-07 19:06:49 +00:00
if (type === 'item') {
const installed = JSON.parse(localStorage.getItem('installed'));
const info = {
data: installed.find((i) => i.name === data.name),
};
2021-03-07 19:06:49 +00:00
this.setState({
2021-04-14 12:45:11 +00:00
item: {
type: info.data.type,
display_name: info.data.name,
author: info.data.author,
description: urlParser(info.data.description.replace(/\n/g, '<br>')),
2021-04-14 12:45:11 +00:00
//updated: info.updated,
version: info.data.version,
icon: info.data.screenshot_url,
data: info.data,
},
button: this.buttons.uninstall,
2021-04-14 12:45:11 +00:00
});
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Item viewed');
} else {
2021-04-14 12:45:11 +00:00
this.setState({
item: {},
2021-04-14 12:45:11 +00:00
});
2021-03-07 19:06:49 +00:00
}
2021-03-13 18:15:57 +00:00
}
2021-04-14 12:45:11 +00:00
uninstall() {
2021-08-14 19:10:48 +00:00
uninstall(this.state.item.type, this.state.item.display_name);
toast(variables.getMessage('toasts.uninstalled'));
2021-03-13 18:15:57 +00:00
this.setState({
2021-04-14 12:45:11 +00:00
button: '',
installed: JSON.parse(localStorage.getItem('installed')),
2021-03-13 18:15:57 +00:00
});
2021-06-21 16:42:14 +00:00
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Uninstall');
2021-03-13 18:15:57 +00:00
}
2021-03-07 19:06:49 +00:00
2021-06-21 16:42:14 +00:00
sortAddons(value, sendEvent) {
2021-05-02 13:44:23 +00:00
let installed = JSON.parse(localStorage.getItem('installed'));
switch (value) {
case 'newest':
installed.reverse();
break;
case 'oldest':
break;
case 'a-z':
installed.sort();
break;
case 'z-a':
installed.sort();
installed.reverse();
2021-05-03 10:27:52 +00:00
break;
default:
break;
2021-05-02 13:44:23 +00:00
}
this.setState({
installed,
2021-05-02 13:44:23 +00:00
});
2021-06-21 16:42:14 +00:00
if (sendEvent) {
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Sort');
2021-06-21 16:42:14 +00:00
}
2021-05-02 13:44:23 +00:00
}
updateCheck() {
let updates = 0;
this.state.installed.forEach(async (item) => {
const data = await (
await fetch(variables.constants.API_URL + 'marketplace//item/' + item.name)
).json();
if (data.version !== item.version) {
updates++;
}
});
if (updates > 0) {
toast(
variables.getMessage('modals.main.addons.updates_available', {
amount: updates,
}),
);
} else {
toast(variables.getMessage('modals.main.addons.no_updates'));
}
}
2021-05-02 13:44:23 +00:00
componentDidMount() {
2021-06-21 16:42:14 +00:00
this.sortAddons(localStorage.getItem('sortAddons'), false);
2021-05-02 13:44:23 +00:00
}
2021-03-07 19:06:49 +00:00
render() {
const sideLoadBackendElements = () => (
<>
<Modal
closeTimeoutMS={100}
onRequestClose={() => this.setState({ showFailed: false })}
isOpen={this.state.showFailed}
className="Modal resetmodal mainModal resetmodal"
overlayClassName="Overlay resetoverlay"
ariaHideApp={false}
>
<SideloadFailedModal
modalClose={() => this.setState({ showFailed: false })}
reason={this.state.failedReason}
/>
</Modal>
<FileUpload
id="file-input"
type="settings"
accept="application/json"
loadFunction={(e) => this.installAddon(JSON.parse(e))}
/>
</>
);
2021-03-07 19:06:49 +00:00
if (this.state.installed.length === 0) {
return (
<>
<Header title={variables.getMessage('modals.main.navbar.addons')} report={false}>
<CustomActions>{this.getSideloadButton()}</CustomActions>
</Header>
<div className="emptyItems">
<div className="emptyNewMessage">
<MdOutlineExtensionOff />
<span className="title">
{variables.getMessage('modals.main.addons.empty.title')}
</span>
<span className="subtitle">
{variables.getMessage('modals.main.addons.empty.description')}
</span>
</div>
2021-03-07 19:06:49 +00:00
</div>
</>
);
2021-03-07 19:06:49 +00:00
}
2021-05-03 10:27:52 +00:00
if (this.state.item.display_name) {
return (
<Item
data={this.state.item}
button={this.state.button}
toggleFunction={() => this.toggle()}
/>
);
2021-05-03 10:27:52 +00:00
}
2021-03-07 19:06:49 +00:00
return (
2021-04-03 12:32:00 +00:00
<>
<div className="modalHeader">
<span className="mainTitle">{variables.getMessage('modals.main.addons.added')}</span>
<div className="filter">
{sideLoadBackendElements()}
<div className="buttonSection">
<Button
type="settings"
onClick={() => this.updateCheck()}
icon={<MdUpdate />}
label={variables.getMessage('modals.main.addons.check_updates')}
/>
<Button
type="settings"
onClick={() => document.getElementById('file-input').click()}
icon={<MdSendTimeExtension />}
label={variables.getMessage('modals.main.addons.sideload.title')}
/>
</div>
</div>
</div>
<Dropdown
label={variables.getMessage('modals.main.addons.sort.title')}
name="sortAddons"
onChange={(value) => this.sortAddons(value)}
items={[
{
value: 'newest',
text: variables.getMessage('modals.main.addons.sort.newest'),
},
{
value: 'oldest',
text: variables.getMessage('modals.main.addons.sort.oldest'),
},
{
value: 'a-z',
text: variables.getMessage('modals.main.addons.sort.a_z'),
},
{
value: 'z-a',
text: variables.getMessage('modals.main.addons.sort.z_a'),
},
]}
/>
<Items
items={this.state.installed}
filter=""
toggleFunction={(input) => this.toggle('item', input)}
/>
2021-04-03 12:32:00 +00:00
</>
2021-03-07 19:06:49 +00:00
);
}
}