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';
|
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 { 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 19:13:30 -08:00
|
|
|
import ColorWithPicker from 'soapbox/features/soapbox-config/components/color-with-picker';
|
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-20 07:47:46 -08:00
|
|
|
import toast from 'soapbox/toast';
|
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';
|
|
|
|
|
2022-12-17 19:13:30 -08:00
|
|
|
import type { ColorChangeHandler } from 'react-color';
|
|
|
|
|
2022-10-25 14:56:25 -07:00
|
|
|
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);
|
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
|
|
|
|
2022-12-17 18:29:25 -08:00
|
|
|
const fileInput = useRef<HTMLInputElement>(null);
|
|
|
|
|
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 19:13:30 -08:00
|
|
|
const updateColor = (key: string) => {
|
|
|
|
return (hex: string) => {
|
|
|
|
setColors({
|
|
|
|
...colors,
|
|
|
|
[key]: hex,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-12-17 18:48:02 -08:00
|
|
|
const setTheme = (theme: any) => {
|
2022-12-17 17:39:54 -08:00
|
|
|
setResetKey(uuidv4());
|
2022-12-17 18:48:02 -08:00
|
|
|
setTimeout(() => setColors(theme));
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetTheme = () => {
|
|
|
|
setTheme(soapbox.colors.toJS() as any);
|
2022-12-17 17:39:54 -08:00
|
|
|
};
|
|
|
|
|
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-20 07:47:46 -08:00
|
|
|
toast.success(intl.formatMessage(messages.importSuccess));
|
2022-12-17 18:29:25 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-17 11:05:50 -08:00
|
|
|
const handleSubmit = async() => {
|
|
|
|
setSubmitting(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await dispatch(fetchSoapboxConfig(host));
|
|
|
|
await updateTheme();
|
2022-12-20 07:47:46 -08:00
|
|
|
toast.success(intl.formatMessage(messages.saved));
|
2022-12-17 11:05:50 -08:00
|
|
|
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
|
|
|
/>
|
2022-12-17 19:25:20 -08:00
|
|
|
</List>
|
2022-12-17 19:13:30 -08:00
|
|
|
|
2022-12-17 19:25:20 -08:00
|
|
|
<List>
|
2022-12-17 19:13:30 -08:00
|
|
|
<ColorListItem
|
|
|
|
label='Greentext'
|
|
|
|
value={colors.greentext}
|
|
|
|
onChange={updateColor('greentext')}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ColorListItem
|
|
|
|
label='Accent Blue'
|
|
|
|
value={colors['accent-blue']}
|
|
|
|
onChange={updateColor('accent-blue')}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ColorListItem
|
|
|
|
label='Gradient Start'
|
|
|
|
value={colors['gradient-start']}
|
|
|
|
onChange={updateColor('gradient-start')}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ColorListItem
|
|
|
|
label='Gradient End'
|
|
|
|
value={colors['gradient-end']}
|
|
|
|
onChange={updateColor('gradient-end')}
|
|
|
|
/>
|
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-20 07:47:46 -08:00
|
|
|
}, {
|
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
|
|
|
}]}
|
|
|
|
/>
|
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-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,
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-17 19:13:30 -08:00
|
|
|
interface IColorListItem {
|
|
|
|
label: React.ReactNode,
|
|
|
|
value: string,
|
|
|
|
onChange: (hex: string) => void,
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Single-color picker. */
|
|
|
|
const ColorListItem: React.FC<IColorListItem> = ({ label, value, onChange }) => {
|
|
|
|
const handleChange: ColorChangeHandler = (color, _e) => {
|
|
|
|
onChange(color.hex);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ListItem label={label}>
|
|
|
|
<ColorWithPicker
|
|
|
|
value={value}
|
|
|
|
onChange={handleChange}
|
|
|
|
className='w-10 h-8 rounded-md overflow-hidden'
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-25 14:56:25 -07:00
|
|
|
export default ThemeEditor;
|