bigbuffet-rw/app/soapbox/features/theme-editor/index.tsx

212 lines
6.3 KiB
TypeScript
Raw Normal View History

2022-12-17 18:29:25 -08:00
import React, { useRef, useState } from 'react';
2022-12-17 11:05:50 -08:00
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { v4 as uuidv4 } from 'uuid';
2022-10-25 14:56:25 -07:00
2022-12-17 11:05:50 -08:00
import { updateSoapboxConfig } from 'soapbox/actions/admin';
import { getHost } from 'soapbox/actions/instance';
import snackbar from 'soapbox/actions/snackbar';
import { fetchSoapboxConfig } from 'soapbox/actions/soapbox';
2022-10-25 14:56:25 -07:00
import List, { ListItem } from 'soapbox/components/list';
2022-12-17 11:05:50 -08:00
import { Button, Column, Form, FormActions } from 'soapbox/components/ui';
2022-12-17 18:16:05 -08:00
import DropdownMenuContainer from 'soapbox/containers/dropdown-menu-container';
2022-12-17 11:05:50 -08:00
import { useAppDispatch, useAppSelector, useSoapboxConfig } from 'soapbox/hooks';
2022-12-17 18:29:25 -08:00
import { normalizeSoapboxConfig } from 'soapbox/normalizers';
2022-12-17 18:16:05 -08:00
import { download } from 'soapbox/utils/download';
2022-10-25 14:56:25 -07:00
import Palette, { ColorGroup } from './components/palette';
const messages = defineMessages({
title: { id: 'admin.theme.title', defaultMessage: 'Theme' },
2022-12-17 11:05:50 -08:00
saved: { id: 'theme_editor.saved', defaultMessage: 'Theme updated!' },
2022-12-17 18:48:02 -08:00
restore: { id: 'theme_editor.restore', defaultMessage: 'Restore default theme' },
2022-12-17 18:16:05 -08:00
export: { id: 'theme_editor.export', defaultMessage: 'Export theme' },
2022-12-17 18:29:25 -08:00
import: { id: 'theme_editor.import', defaultMessage: 'Import theme' },
importSuccess: { id: 'theme_editor.import_success', defaultMessage: 'Theme was successfully imported!' },
2022-10-25 14:56:25 -07:00
});
interface IThemeEditor {
}
/** UI for editing Tailwind theme colors. */
const ThemeEditor: React.FC<IThemeEditor> = () => {
const intl = useIntl();
2022-12-17 11:05:50 -08:00
const dispatch = useAppDispatch();
2022-10-25 14:56:25 -07:00
const soapbox = useSoapboxConfig();
2022-12-17 11:05:50 -08:00
const host = useAppSelector(state => getHost(state));
const rawConfig = useAppSelector(state => state.soapbox);
const [colors, setColors] = useState(soapbox.colors.toJS() as any);
2022-12-17 11:05:50 -08:00
const [submitting, setSubmitting] = useState(false);
const [resetKey, setResetKey] = useState(uuidv4());
2022-12-17 18:29:25 -08:00
const fileInput = useRef<HTMLInputElement>(null);
const updateColors = (key: string) => {
2022-12-17 11:05:50 -08:00
return (newColors: ColorGroup) => {
setColors({
...colors,
[key]: {
...colors[key],
...newColors,
},
});
};
};
2022-10-25 14:56:25 -07:00
2022-12-17 18:48:02 -08:00
const setTheme = (theme: any) => {
setResetKey(uuidv4());
2022-12-17 18:48:02 -08:00
setTimeout(() => setColors(theme));
};
const resetTheme = () => {
setTheme(soapbox.colors.toJS() as any);
};
2022-12-17 11:05:50 -08:00
const updateTheme = async () => {
const params = rawConfig.set('colors', colors).toJS();
await dispatch(updateSoapboxConfig(params));
};
2022-12-17 18:48:02 -08:00
const restoreDefaultTheme = () => {
const colors = normalizeSoapboxConfig({ brandColor: '#0482d8' }).colors.toJS();
setTheme(colors);
};
2022-12-17 18:16:05 -08:00
const exportTheme = () => {
const data = JSON.stringify(colors, null, 2);
download(data, 'theme.json');
};
2022-12-17 18:29:25 -08:00
const importTheme = () => {
fileInput.current?.click();
};
const handleSelectFile: React.ChangeEventHandler<HTMLInputElement> = async (e) => {
const file = e.target.files?.item(0);
if (file) {
const text = await file.text();
const json = JSON.parse(text);
const colors = normalizeSoapboxConfig({ colors: json }).colors.toJS();
2022-12-17 18:48:02 -08:00
setTheme(colors);
2022-12-17 18:29:25 -08:00
dispatch(snackbar.success(intl.formatMessage(messages.importSuccess)));
}
};
2022-12-17 11:05:50 -08:00
const handleSubmit = async() => {
setSubmitting(true);
try {
await dispatch(fetchSoapboxConfig(host));
await updateTheme();
dispatch(snackbar.success(intl.formatMessage(messages.saved)));
setSubmitting(false);
} catch (e) {
setSubmitting(false);
}
};
2022-10-25 14:56:25 -07:00
return (
<Column label={intl.formatMessage(messages.title)}>
2022-12-17 11:05:50 -08:00
<Form onSubmit={handleSubmit}>
<List>
<PaletteListItem
label='Primary'
palette={colors.primary}
onChange={updateColors('primary')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
<PaletteListItem
label='Secondary'
palette={colors.secondary}
onChange={updateColors('secondary')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
<PaletteListItem
label='Accent'
palette={colors.accent}
onChange={updateColors('accent')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
<PaletteListItem
label='Gray'
palette={colors.gray}
onChange={updateColors('gray')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
<PaletteListItem
label='Success'
palette={colors.success}
onChange={updateColors('success')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
<PaletteListItem
label='Danger'
palette={colors.danger}
onChange={updateColors('danger')}
resetKey={resetKey}
2022-12-17 11:05:50 -08:00
/>
</List>
<FormActions>
2022-12-17 18:16:05 -08:00
<DropdownMenuContainer
items={[{
2022-12-17 18:48:02 -08:00
text: intl.formatMessage(messages.restore),
action: restoreDefaultTheme,
icon: require('@tabler/icons/refresh.svg'),
},{
2022-12-17 18:29:25 -08:00
text: intl.formatMessage(messages.import),
action: importTheme,
icon: require('@tabler/icons/upload.svg'),
}, {
2022-12-17 18:16:05 -08:00
text: intl.formatMessage(messages.export),
action: exportTheme,
2022-12-17 18:29:25 -08:00
icon: require('@tabler/icons/download.svg'),
2022-12-17 18:16:05 -08:00
}]}
/>
<Button theme='secondary' onClick={resetTheme}>
<FormattedMessage id='theme_editor.Reset' defaultMessage='Reset' />
</Button>
2022-12-17 11:05:50 -08:00
<Button type='submit' theme='primary' disabled={submitting}>
<FormattedMessage id='theme_editor.save' defaultMessage='Save theme' />
</Button>
</FormActions>
</Form>
2022-12-17 18:29:25 -08:00
<input
type='file'
ref={fileInput}
multiple
accept='application/json'
className='hidden'
onChange={handleSelectFile}
/>
2022-10-25 14:56:25 -07:00
</Column>
);
};
interface IPaletteListItem {
label: React.ReactNode,
palette: ColorGroup,
onChange: (palette: ColorGroup) => void,
resetKey?: string,
2022-10-25 14:56:25 -07:00
}
/** Palette editor inside a ListItem. */
const PaletteListItem: React.FC<IPaletteListItem> = ({ label, palette, onChange, resetKey }) => {
2022-10-25 14:56:25 -07:00
return (
<ListItem label={<div className='w-20'>{label}</div>}>
<Palette palette={palette} onChange={onChange} resetKey={resetKey} />
2022-10-25 14:56:25 -07:00
</ListItem>
);
};
export default ThemeEditor;