Rework some of the css var state to fix flaky customization UI. Fixes #2505

This commit is contained in:
Gabe Kangas 2022-12-28 13:05:35 -08:00
parent 78b956532f
commit a88da10c74
No known key found for this signature in database
GPG Key ID: 4345B2060657F330

View File

@ -24,38 +24,6 @@ interface AppearanceVariable {
description: string; description: string;
} }
function ColorPicker({
value,
name,
description,
onChange,
}: {
value: string;
name: string;
description: string;
onChange: (name: string, value: string, description: string) => void;
}) {
return (
<Col span={3} key={name}>
<input
type="color"
id={name}
name={description}
title={description}
value={value}
className={s.colorPicker}
onChange={e => onChange(name, e.target.value, description)}
/>
<div style={{ padding: '2px' }}>{description}</div>
</Col>
);
}
export default function Appearance() {
const serverStatusData = useContext(ServerStatusContext);
const { serverConfig } = serverStatusData || {};
const { instanceDetails } = serverConfig;
const { appearanceVariables } = instanceDetails;
const chatColorVariables = [ const chatColorVariables = [
{ name: 'theme-color-users-0', description: '' }, { name: 'theme-color-users-0', description: '' },
{ name: 'theme-color-users-1', description: '' }, { name: 'theme-color-users-1', description: '' },
@ -99,6 +67,50 @@ export default function Appearance() {
const others = [{ name: 'theme-rounded-corners', description: 'Corner radius' }]; const others = [{ name: 'theme-rounded-corners', description: 'Corner radius' }];
// Create an object so these vars can be indexed by name.
const allAvailableValues = [
...paletteVariables,
...componentColorVariables,
...chatColorVariables,
...others,
].reduce((obj, val) => {
// eslint-disable-next-line no-param-reassign
obj[val.name] = { name: val.name, description: val.description };
return obj;
}, {});
function ColorPicker({
value,
name,
description,
onChange,
}: {
value: string;
name: string;
description: string;
onChange: (name: string, value: string, description: string) => void;
}) {
return (
<Col span={3} key={name}>
<input
type="color"
id={name}
name={description}
title={description}
value={value}
className={s.colorPicker}
onChange={e => onChange(name, e.target.value, description)}
/>
<div style={{ padding: '2px' }}>{description}</div>
</Col>
);
}
export default function Appearance() {
const serverStatusData = useContext(ServerStatusContext);
const { serverConfig } = serverStatusData;
const { instanceDetails } = serverConfig;
const { appearanceVariables } = instanceDetails;
const [colors, setColors] = useState<Record<string, AppearanceVariable>>(); const [colors, setColors] = useState<Record<string, AppearanceVariable>>();
const [submitStatus, setSubmitStatus] = useState<StatusState>(null); const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
@ -127,11 +139,14 @@ export default function Appearance() {
}, []); }, []);
useEffect(() => { useEffect(() => {
if (!appearanceVariables || !colors) return; if (Object.keys(appearanceVariables).length === 0) return;
const c = colors; const c = colors || {};
Object.keys(appearanceVariables).forEach(key => { Object.keys(appearanceVariables).forEach(key => {
c[key] = { value: appearanceVariables[key], description: colors[key]?.description || '' }; c[key] = {
value: appearanceVariables[key],
description: allAvailableValues[key]?.description || '',
};
}); });
setColors(c); setColors(c);
}, [appearanceVariables]); }, [appearanceVariables]);
@ -179,16 +194,16 @@ export default function Appearance() {
}); });
}; };
if (!colors) {
return <div>Loading...</div>;
}
const onBorderRadiusChange = (value: string) => { const onBorderRadiusChange = (value: string) => {
const variableName = 'theme-rounded-corners'; const variableName = 'theme-rounded-corners';
updateColor(variableName, `${value.toString()}px`, ''); updateColor(variableName, `${value.toString()}px`, '');
}; };
if (!colors) {
return <div>Loading...</div>;
}
return ( return (
<Space direction="vertical"> <Space direction="vertical">
<Title>Customize Appearance</Title> <Title>Customize Appearance</Title>
@ -203,6 +218,7 @@ export default function Appearance() {
{componentColorVariables.map(colorVar => { {componentColorVariables.map(colorVar => {
const { name } = colorVar; const { name } = colorVar;
const c = colors[name]; const c = colors[name];
return ( return (
<ColorPicker <ColorPicker
key={name} key={name}