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

162 lines
4.7 KiB
TypeScript
Raw Normal View History

import React, { 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: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:16:05 -08:00
export: { id: 'theme_editor.export', defaultMessage: 'Export theme' },
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());
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
const resetTheme = () => {
setResetKey(uuidv4());
setTimeout(() => setColors(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:16:05 -08:00
const exportTheme = () => {
const data = JSON.stringify(colors, null, 2);
download(data, 'theme.json');
};
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={[{
text: intl.formatMessage(messages.export),
action: exportTheme,
}]}
/>
<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-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;