Settings card with "follow the default"
Settings that are either the platform default or the brand's own value, with a save bar.
Some settings should follow a platform default until the brand decides otherwise. DefaultableSetting models that: its value is null (follow the default, shown next to it) or an override, and a "Use default" button sets it back to null. Edits make a draft; a SaveBar saves or discards the whole draft.
The whole recipe
Each setting is either null (follows the platform default, which is shown) or the brand's own value. Editing makes the draft dirty; SaveBar appears, Save is off while any value is invalid and Discard puts the draft back. "Use default" on a row sets it back to null.
Code
import { useState } from 'react';
import { Card, DefaultableRow, DefaultableSetting, Note, SaveBar, defaultableNumberError } from '@ergasti/bricks';
interface Draft { cooldown: string | null; oneGift: boolean | null; mode: string | null }
const PLATFORM = { cooldown: 90, oneGift: true, mode: 'first_match' };
const saved: Draft = { cooldown: null, oneGift: null, mode: 'last_match' };
function Screen() {
const [draft, setDraft] = useState<Draft>(saved);
const [base, setBase] = useState<Draft>(saved);
const [saving, setSaving] = useState(false);
const cooldownError = draft.cooldown === null ? null : defaultableNumberError(draft.cooldown, { min: 0, max: 365 }, 'en');
const dirty = JSON.stringify(draft) !== JSON.stringify(base);
const save = () => { setSaving(true); window.setTimeout(() => { setBase(draft); setSaving(false); }, 600); };
return (
<div style={{ maxWidth: 640, display: 'grid', gap: 12 }}>
<Card>
<Card.Header title="Gift rules" sub="Leave a rule on the default to follow the platform when it changes." />
<Card.Body>
<DefaultableRow label="Gift cooldown" help="Days before the same creator can pick another gift.">
<DefaultableSetting kind="number" value={draft.cooldown} defaultValue={PLATFORM.cooldown} min={0} max={365} unit="days" onChange={(cooldown) => setDraft({ ...draft, cooldown })} />
</DefaultableRow>
<DefaultableRow label="One gift per challenge">
<DefaultableSetting kind="switch" value={draft.oneGift} defaultValue={PLATFORM.oneGift} onChange={(oneGift) => setDraft({ ...draft, oneGift })} />
</DefaultableRow>
<DefaultableRow label="When more than one code matches">
<DefaultableSetting kind="choice" value={draft.mode} defaultValue={PLATFORM.mode} options={[{ value: 'first_match', label: 'First match' }, { value: 'last_match', label: 'Last match' }]} onChange={(mode) => setDraft({ ...draft, mode })} />
</DefaultableRow>
</Card.Body>
</Card>
{cooldownError && <Note flush role="alert">{cooldownError}</Note>}
<SaveBar dirty={dirty} saving={saving} saveDisabled={cooldownError !== null} onSave={save} onDiscard={() => setDraft(base)} />
</div>
);
}How it fits together
- One draft object holds every setting, and
nullmeans "follow the default". The saved copy is kept next to it, sodirtyis a comparison. DefaultableRowdraws the label, the optional help text and dims the row whenoff. Inside it,DefaultableSetting kind="number" | "switch" | "choice"draws the control. It takesvalue,defaultValueandonChange; for numbers alsomin,max,step,unitandresetTo, for choices theoptions.- Validate with the exported checkers.
defaultableNumberError(value, { min, max }, language)returns a message ornull; pass its result to the control or show it, and turn Save off withsaveDisabled. SaveBarappears whendirty, shows "Saving…" whilesaving, and callsonSaveandonDiscard. Discard puts the draft back to the saved copy.- Read-only for people who cannot edit:
readOnlyon the setting draws it as a fact.
Related: DefaultableSetting, SaveBar, GiftRulesCard.
Owner and viewer: one card, two modes
The Settings screen's section cards are SettingsSection. A viewer gets the read-only card: labelled rows, "🔒 View only" (or, with action, a card that clicks through to the screen that uses the rules). The owner gets the same card with editable: rows may hold a control through KeyValueItem.control (a Switch, a field), the card is no longer a click target (a click on a switch must never navigate), there is no "View only" pill, and action ("Open payouts →") is only the head button. A locked row still says the platform owns it, beside a control too.
The owner's card
editable cards hold controls (KeyValueItem.control): no "🔒 View only", the card is not a click target (a click on a switch never navigates), and action is only the head button.
Code
import { useState } from 'react';
import { SettingsSection, Switch } from '@ergasti/bricks';
function Editable() {
const [gifts, setGifts] = useState(true);
return (
<div style={{ maxWidth: 470 }}>
<SettingsSection editable title="Motivation" tone="butter" icon="gift" action={{ label: 'Open payouts', onClick: () => {} }} rows={[
{ label: 'Creators can pick gifts', control: <Switch rowLabelled checked={gifts} onChange={setGifts} /> },
{ label: 'One gift per challenge', value: 'On', locked: true },
]} />
</div>
);
}<SettingsSection editable={isOwner} title="Motivation" tone="butter" icon="gift"
action={{ label: 'Open payouts', onClick: openPayouts }}
rows={[
{ label: 'Creators can pick gifts', control: isOwner ? <Switch checked={pick} onChange={setPick} label="Creators can pick gifts" /> : undefined, value: pick ? 'On' : 'Off' },
{ label: 'Return window', value: '14 days after delivery', locked: true },
]} />