Skip to content

Commit 11a99fe

Browse files
committed
ENG-1472: Refactor BlockPropSettingPanels to add accessor-backed default reads (with initialValue fallback)
1 parent 5a02381 commit 11a99fe

1 file changed

Lines changed: 129 additions & 15 deletions

File tree

apps/roam/src/components/settings/components/BlockPropSettingPanels.tsx

Lines changed: 129 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import Description from "roamjs-components/components/Description";
1818
import useSingleChildValue from "roamjs-components/components/ConfigPanels/useSingleChildValue";
1919
import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid";
2020
import {
21+
getGlobalSetting,
22+
getPersonalSetting,
23+
getFeatureFlag,
24+
getDiscourseNodeSetting,
2125
setGlobalSetting,
2226
setPersonalSetting,
2327
setFeatureFlag,
@@ -466,6 +470,18 @@ const createAccessors = <T,>(
466470
setter: setFn,
467471
});
468472

473+
const readWithFallback = <T,>(
474+
reader: () => T | undefined,
475+
fallback?: T,
476+
): T | undefined => {
477+
try {
478+
const value = reader();
479+
return value ?? fallback;
480+
} catch {
481+
return fallback;
482+
}
483+
};
484+
469485
const globalAccessors = {
470486
text: createAccessors<string>(setGlobalSetting),
471487
flag: createAccessors<boolean>(setGlobalSetting),
@@ -515,7 +531,7 @@ export const FeatureFlagPanel = ({
515531
description={description}
516532
settingKeys={[featureKey as string]}
517533
setter={featureFlagSetter}
518-
initialValue={initialValue}
534+
initialValue={readWithFallback(() => getFeatureFlag(featureKey), initialValue)}
519535
onBeforeChange={handleBeforeChange}
520536
onChange={onAfterChange}
521537
parentUid={parentUid}
@@ -526,43 +542,113 @@ export const FeatureFlagPanel = ({
526542
};
527543

528544
export const GlobalTextPanel = (props: TextWrapperProps) => (
529-
<BaseTextPanel {...props} {...globalAccessors.text} />
545+
<BaseTextPanel
546+
{...props}
547+
initialValue={readWithFallback(
548+
() => getGlobalSetting<string>(props.settingKeys),
549+
props.initialValue,
550+
)}
551+
{...globalAccessors.text}
552+
/>
530553
);
531554

532555
export const GlobalFlagPanel = (props: FlagWrapperProps) => (
533-
<BaseFlagPanel {...props} {...globalAccessors.flag} />
556+
<BaseFlagPanel
557+
{...props}
558+
initialValue={readWithFallback(
559+
() => getGlobalSetting<boolean>(props.settingKeys),
560+
props.initialValue,
561+
)}
562+
{...globalAccessors.flag}
563+
/>
534564
);
535565

536566
export const GlobalNumberPanel = (props: NumberWrapperProps) => (
537-
<BaseNumberPanel {...props} {...globalAccessors.number} />
567+
<BaseNumberPanel
568+
{...props}
569+
initialValue={readWithFallback(
570+
() => getGlobalSetting<number>(props.settingKeys),
571+
props.initialValue,
572+
)}
573+
{...globalAccessors.number}
574+
/>
538575
);
539576

540577
export const GlobalSelectPanel = (props: SelectWrapperProps) => (
541-
<BaseSelectPanel {...props} {...globalAccessors.text} />
578+
<BaseSelectPanel
579+
{...props}
580+
initialValue={readWithFallback(
581+
() => getGlobalSetting<string>(props.settingKeys),
582+
props.initialValue,
583+
)}
584+
{...globalAccessors.text}
585+
/>
542586
);
543587

544588
export const GlobalMultiTextPanel = (props: MultiTextWrapperProps) => (
545-
<BaseMultiTextPanel {...props} {...globalAccessors.multiText} />
589+
<BaseMultiTextPanel
590+
{...props}
591+
initialValue={readWithFallback(
592+
() => getGlobalSetting<string[]>(props.settingKeys),
593+
props.initialValue,
594+
)}
595+
{...globalAccessors.multiText}
596+
/>
546597
);
547598

548599
export const PersonalTextPanel = (props: TextWrapperProps) => (
549-
<BaseTextPanel {...props} {...personalAccessors.text} />
600+
<BaseTextPanel
601+
{...props}
602+
initialValue={readWithFallback(
603+
() => getPersonalSetting<string>(props.settingKeys),
604+
props.initialValue,
605+
)}
606+
{...personalAccessors.text}
607+
/>
550608
);
551609

552610
export const PersonalFlagPanel = (props: FlagWrapperProps) => (
553-
<BaseFlagPanel {...props} {...personalAccessors.flag} />
611+
<BaseFlagPanel
612+
{...props}
613+
initialValue={readWithFallback(
614+
() => getPersonalSetting<boolean>(props.settingKeys),
615+
props.initialValue,
616+
)}
617+
{...personalAccessors.flag}
618+
/>
554619
);
555620

556621
export const PersonalNumberPanel = (props: NumberWrapperProps) => (
557-
<BaseNumberPanel {...props} {...personalAccessors.number} />
622+
<BaseNumberPanel
623+
{...props}
624+
initialValue={readWithFallback(
625+
() => getPersonalSetting<number>(props.settingKeys),
626+
props.initialValue,
627+
)}
628+
{...personalAccessors.number}
629+
/>
558630
);
559631

560632
export const PersonalSelectPanel = (props: SelectWrapperProps) => (
561-
<BaseSelectPanel {...props} {...personalAccessors.text} />
633+
<BaseSelectPanel
634+
{...props}
635+
initialValue={readWithFallback(
636+
() => getPersonalSetting<string>(props.settingKeys),
637+
props.initialValue,
638+
)}
639+
{...personalAccessors.text}
640+
/>
562641
);
563642

564643
export const PersonalMultiTextPanel = (props: MultiTextWrapperProps) => (
565-
<BaseMultiTextPanel {...props} {...personalAccessors.multiText} />
644+
<BaseMultiTextPanel
645+
{...props}
646+
initialValue={readWithFallback(
647+
() => getPersonalSetting<string[]>(props.settingKeys),
648+
props.initialValue,
649+
)}
650+
{...personalAccessors.multiText}
651+
/>
566652
);
567653

568654
const createDiscourseNodeSetter =
@@ -587,7 +673,14 @@ export const DiscourseNodeTextPanel = ({
587673
getValidationError?: Validator<string>;
588674
onChange?: (value: string) => void;
589675
}) => (
590-
<BaseTextPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
676+
<BaseTextPanel
677+
{...props}
678+
initialValue={readWithFallback(
679+
() => getDiscourseNodeSetting<string>(nodeType, props.settingKeys),
680+
props.initialValue,
681+
)}
682+
setter={createDiscourseNodeSetter(nodeType)}
683+
/>
591684
);
592685

593686
export const DiscourseNodeFlagPanel = ({
@@ -600,15 +693,29 @@ export const DiscourseNodeFlagPanel = ({
600693
onBeforeChange?: (checked: boolean) => Promise<boolean>;
601694
onChange?: (checked: boolean) => void;
602695
}) => (
603-
<BaseFlagPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
696+
<BaseFlagPanel
697+
{...props}
698+
initialValue={readWithFallback(
699+
() => getDiscourseNodeSetting<boolean>(nodeType, props.settingKeys),
700+
props.initialValue,
701+
)}
702+
setter={createDiscourseNodeSetter(nodeType)}
703+
/>
604704
);
605705

606706
export const DiscourseNodeSelectPanel = ({
607707
nodeType,
608708
...props
609709
}: DiscourseNodeBaseProps &
610710
RoamBlockSyncProps & { options: string[]; initialValue?: string }) => (
611-
<BaseSelectPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
711+
<BaseSelectPanel
712+
{...props}
713+
initialValue={readWithFallback(
714+
() => getDiscourseNodeSetting<string>(nodeType, props.settingKeys),
715+
props.initialValue,
716+
)}
717+
setter={createDiscourseNodeSetter(nodeType)}
718+
/>
612719
);
613720

614721
export const DiscourseNodeNumberPanel = ({
@@ -620,5 +727,12 @@ export const DiscourseNodeNumberPanel = ({
620727
min?: number;
621728
max?: number;
622729
}) => (
623-
<BaseNumberPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
730+
<BaseNumberPanel
731+
{...props}
732+
initialValue={readWithFallback(
733+
() => getDiscourseNodeSetting<number>(nodeType, props.settingKeys),
734+
props.initialValue,
735+
)}
736+
setter={createDiscourseNodeSetter(nodeType)}
737+
/>
624738
);

0 commit comments

Comments
 (0)