2022-10-25 15:47:24 -07:00
|
|
|
import React, { useState } from 'react';
|
2022-12-17 11:05:50 -08:00
|
|
|
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
|
2022-12-17 17:39:54 -08:00
|
|
|
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';
|
|
|
|
import { useAppDispatch, useAppSelector, useSoapboxConfig } from 'soapbox/hooks';
|
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-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);
|
2022-10-25 15:47:24 -07:00
|
|
|
|
|
|
|
const [colors, setColors] = useState(soapbox.colors.toJS() as any);
|
2022-12-17 11:05:50 -08:00
|
|
|
const [submitting, setSubmitting] = useState(false);
|
2022-12-17 17:39:54 -08:00
|
|
|
const [resetKey, setResetKey] = useState(uuidv4());
|
2022-10-25 15:47:24 -07:00
|
|
|
|
|
|
|
const updateColors = (key: string) => {
|
2022-12-17 11:05:50 -08:00
|
|
|
return (newColors: ColorGroup) => {
|
2022-10-25 15:47:24 -07:00
|
|
|
setColors({
|
|
|
|
...colors,
|
|
|
|
[key]: {
|
|
|
|
...colors[key],
|
|
|
|
...newColors,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2022-10-25 14:56:25 -07:00
|
|
|
|
2022-12-17 17:39:54 -08: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));
|
|
|
|
};
|
|
|
|
|
|
|
|
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')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<PaletteListItem
|
|
|
|
label='Secondary'
|
|
|
|
palette={colors.secondary}
|
|
|
|
onChange={updateColors('secondary')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<PaletteListItem
|
|
|
|
label='Accent'
|
|
|
|
palette={colors.accent}
|
|
|
|
onChange={updateColors('accent')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<PaletteListItem
|
|
|
|
label='Gray'
|
|
|
|
palette={colors.gray}
|
|
|
|
onChange={updateColors('gray')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<PaletteListItem
|
|
|
|
label='Success'
|
|
|
|
palette={colors.success}
|
|
|
|
onChange={updateColors('success')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<PaletteListItem
|
|
|
|
label='Danger'
|
|
|
|
palette={colors.danger}
|
|
|
|
onChange={updateColors('danger')}
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey={resetKey}
|
2022-12-17 11:05:50 -08:00
|
|
|
/>
|
|
|
|
</List>
|
|
|
|
|
|
|
|
<FormActions>
|
2022-12-17 17:39:54 -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-10-25 14:56:25 -07:00
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IPaletteListItem {
|
|
|
|
label: React.ReactNode,
|
|
|
|
palette: ColorGroup,
|
2022-10-25 15:47:24 -07:00
|
|
|
onChange: (palette: ColorGroup) => void,
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey?: string,
|
2022-10-25 14:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Palette editor inside a ListItem. */
|
2022-12-17 17:39:54 -08:00
|
|
|
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>}>
|
2022-12-17 17:39:54 -08:00
|
|
|
<Palette palette={palette} onChange={onChange} resetKey={resetKey} />
|
2022-10-25 14:56:25 -07:00
|
|
|
</ListItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ThemeEditor;
|