/**
* Copyright (C) 2021 Thomas Weber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Search from './search';
import importedAddons from '../generated/addon-manifests';
import messagesByLocale from '../generated/l10n-settings-entries';
import settingsTranslationsEnglish from './en.json';
import settingsTranslationsOther from './translations.json';
import upstreamMeta from '../generated/upstream-meta.json';
import { detectLocale } from '../../lib/detect-locale';
import { getInitialDarkMode } from '../../lib/tw-theme-hoc.jsx';
import SettingsStore from '../settings-store-singleton';
import Channels from '../channels';
import extensionImage from './icons/extension.svg';
import brushImage from './icons/brush.svg';
import undoImage from './icons/undo.svg';
import expandImageBlack from './icons/expand.svg';
import infoImage from './icons/info.svg';
import styles from './settings.css';
import '../polyfill';
import '../../lib/normalize.css';
/* eslint-disable no-alert */
/* eslint-disable no-console */
/* eslint-disable react/no-multi-comp */
/* eslint-disable react/jsx-no-bind */
const locale = detectLocale(Object.keys(messagesByLocale));
document.documentElement.lang = locale;
const addonTranslations = messagesByLocale[locale] ? messagesByLocale[locale]() : {};
const settingsTranslations = settingsTranslationsEnglish;
if (locale !== 'en') {
const messages = settingsTranslationsOther[locale] || settingsTranslationsOther[locale.split('-')[0]];
if (messages) {
Object.assign(settingsTranslations, messages);
}
}
document.title = `${settingsTranslations.title} - PenguinMod`;
const theme = getInitialDarkMode() ? 'dark' : 'light';
document.body.setAttribute('theme', theme);
let _throttleTimeout;
const postThrottledSettingsChange = store => {
if (_throttleTimeout) {
clearTimeout(_throttleTimeout);
}
_throttleTimeout = setTimeout(() => {
Channels.changeChannel.postMessage({
version: upstreamMeta.commit,
store
});
}, 100);
};
const filterAddonsBySupport = () => {
const supported = {};
const unsupported = {};
for (const [id, manifest] of Object.entries(importedAddons)) {
if (manifest.unsupported) {
unsupported[id] = manifest;
} else {
supported[id] = manifest;
}
}
return {
supported,
unsupported
};
};
const { supported: supportedAddons, unsupported: unsupportedAddons } = filterAddonsBySupport();
const groupAddons = () => {
const groups = {
new: {
label: settingsTranslations.groupNew,
open: true,
addons: []
},
others: {
label: settingsTranslations.groupOthers,
open: true,
addons: []
},
danger: {
label: settingsTranslations.groupDanger,
open: false,
addons: []
}
};
const manifests = Object.values(supportedAddons);
for (let index = 0; index < manifests.length; index++) {
const manifest = manifests[index];
if (manifest.tags.includes('new')) {
groups.new.addons.push(index);
} else if (manifest.tags.includes('danger') || manifest.noCompiler) {
groups.danger.addons.push(index);
} else {
groups.others.addons.push(index);
}
}
return groups;
};
const groupedAddons = groupAddons();
const CreditList = ({ credits }) => (
credits.map((author, index) => {
const isLast = index === credits.length - 1;
return (
{author.link ? (
{author.name}
) : (
{author.name}
)}
{isLast ? null : ', '}
);
})
);
CreditList.propTypes = {
credits: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
link: PropTypes.string
}))
};
const Switch = ({ onChange, value, ...props }) => (