Skip to content

Commit c90e2c8

Browse files
committed
ENG-1472: Refactor BlockPropSettingPanels to add accessor-backed default reads (with initialValue fallback)
1 parent 75d22ad commit c90e2c8

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
@@ -19,6 +19,10 @@ import Description from "roamjs-components/components/Description";
1919
import useSingleChildValue from "roamjs-components/components/ConfigPanels/useSingleChildValue";
2020
import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid";
2121
import {
22+
getGlobalSetting,
23+
getPersonalSetting,
24+
getFeatureFlag,
25+
getDiscourseNodeSetting,
2226
setGlobalSetting,
2327
setPersonalSetting,
2428
setFeatureFlag,
@@ -478,6 +482,18 @@ const createAccessors = <T,>(
478482
setter: setFn,
479483
});
480484

485+
const readWithFallback = <T,>(
486+
reader: () => T | undefined,
487+
fallback?: T,
488+
): T | undefined => {
489+
try {
490+
const value = reader();
491+
return value ?? fallback;
492+
} catch {
493+
return fallback;
494+
}
495+
};
496+
481497
const globalAccessors = {
482498
text: createAccessors<string>(setGlobalSetting),
483499
flag: createAccessors<boolean>(setGlobalSetting),
@@ -527,7 +543,7 @@ export const FeatureFlagPanel = ({
527543
description={description}
528544
settingKeys={[featureKey as string]}
529545
setter={featureFlagSetter}
530-
initialValue={initialValue}
546+
initialValue={readWithFallback(() => getFeatureFlag(featureKey), initialValue)}
531547
onBeforeChange={handleBeforeChange}
532548
onChange={onAfterChange}
533549
parentUid={parentUid}
@@ -538,43 +554,113 @@ export const FeatureFlagPanel = ({
538554
};
539555

540556
export const GlobalTextPanel = (props: TextWrapperProps) => (
541-
<BaseTextPanel {...props} {...globalAccessors.text} />
557+
<BaseTextPanel
558+
{...props}
559+
initialValue={readWithFallback(
560+
() => getGlobalSetting<string>(props.settingKeys),
561+
props.initialValue,
562+
)}
563+
{...globalAccessors.text}
564+
/>
542565
);
543566

544567
export const GlobalFlagPanel = (props: FlagWrapperProps) => (
545-
<BaseFlagPanel {...props} {...globalAccessors.flag} />
568+
<BaseFlagPanel
569+
{...props}
570+
initialValue={readWithFallback(
571+
() => getGlobalSetting<boolean>(props.settingKeys),
572+
props.initialValue,
573+
)}
574+
{...globalAccessors.flag}
575+
/>
546576
);
547577

548578
export const GlobalNumberPanel = (props: NumberWrapperProps) => (
549-
<BaseNumberPanel {...props} {...globalAccessors.number} />
579+
<BaseNumberPanel
580+
{...props}
581+
initialValue={readWithFallback(
582+
() => getGlobalSetting<number>(props.settingKeys),
583+
props.initialValue,
584+
)}
585+
{...globalAccessors.number}
586+
/>
550587
);
551588

552589
export const GlobalSelectPanel = (props: SelectWrapperProps) => (
553-
<BaseSelectPanel {...props} {...globalAccessors.text} />
590+
<BaseSelectPanel
591+
{...props}
592+
initialValue={readWithFallback(
593+
() => getGlobalSetting<string>(props.settingKeys),
594+
props.initialValue,
595+
)}
596+
{...globalAccessors.text}
597+
/>
554598
);
555599

556600
export const GlobalMultiTextPanel = (props: MultiTextWrapperProps) => (
557-
<BaseMultiTextPanel {...props} {...globalAccessors.multiText} />
601+
<BaseMultiTextPanel
602+
{...props}
603+
initialValue={readWithFallback(
604+
() => getGlobalSetting<string[]>(props.settingKeys),
605+
props.initialValue,
606+
)}
607+
{...globalAccessors.multiText}
608+
/>
558609
);
559610

560611
export const PersonalTextPanel = (props: TextWrapperProps) => (
561-
<BaseTextPanel {...props} {...personalAccessors.text} />
612+
<BaseTextPanel
613+
{...props}
614+
initialValue={readWithFallback(
615+
() => getPersonalSetting<string>(props.settingKeys),
616+
props.initialValue,
617+
)}
618+
{...personalAccessors.text}
619+
/>
562620
);
563621

564622
export const PersonalFlagPanel = (props: FlagWrapperProps) => (
565-
<BaseFlagPanel {...props} {...personalAccessors.flag} />
623+
<BaseFlagPanel
624+
{...props}
625+
initialValue={readWithFallback(
626+
() => getPersonalSetting<boolean>(props.settingKeys),
627+
props.initialValue,
628+
)}
629+
{...personalAccessors.flag}
630+
/>
566631
);
567632

568633
export const PersonalNumberPanel = (props: NumberWrapperProps) => (
569-
<BaseNumberPanel {...props} {...personalAccessors.number} />
634+
<BaseNumberPanel
635+
{...props}
636+
initialValue={readWithFallback(
637+
() => getPersonalSetting<number>(props.settingKeys),
638+
props.initialValue,
639+
)}
640+
{...personalAccessors.number}
641+
/>
570642
);
571643

572644
export const PersonalSelectPanel = (props: SelectWrapperProps) => (
573-
<BaseSelectPanel {...props} {...personalAccessors.text} />
645+
<BaseSelectPanel
646+
{...props}
647+
initialValue={readWithFallback(
648+
() => getPersonalSetting<string>(props.settingKeys),
649+
props.initialValue,
650+
)}
651+
{...personalAccessors.text}
652+
/>
574653
);
575654

576655
export const PersonalMultiTextPanel = (props: MultiTextWrapperProps) => (
577-
<BaseMultiTextPanel {...props} {...personalAccessors.multiText} />
656+
<BaseMultiTextPanel
657+
{...props}
658+
initialValue={readWithFallback(
659+
() => getPersonalSetting<string[]>(props.settingKeys),
660+
props.initialValue,
661+
)}
662+
{...personalAccessors.multiText}
663+
/>
578664
);
579665

580666
const createDiscourseNodeSetter =
@@ -600,7 +686,14 @@ export const DiscourseNodeTextPanel = ({
600686
error?: string;
601687
onChange?: (value: string) => void;
602688
}) => (
603-
<BaseTextPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
689+
<BaseTextPanel
690+
{...props}
691+
initialValue={readWithFallback(
692+
() => getDiscourseNodeSetting<string>(nodeType, props.settingKeys),
693+
props.initialValue,
694+
)}
695+
setter={createDiscourseNodeSetter(nodeType)}
696+
/>
604697
);
605698

606699
export const DiscourseNodeFlagPanel = ({
@@ -613,15 +706,29 @@ export const DiscourseNodeFlagPanel = ({
613706
onBeforeChange?: (checked: boolean) => Promise<boolean>;
614707
onChange?: (checked: boolean) => void;
615708
}) => (
616-
<BaseFlagPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
709+
<BaseFlagPanel
710+
{...props}
711+
initialValue={readWithFallback(
712+
() => getDiscourseNodeSetting<boolean>(nodeType, props.settingKeys),
713+
props.initialValue,
714+
)}
715+
setter={createDiscourseNodeSetter(nodeType)}
716+
/>
617717
);
618718

619719
export const DiscourseNodeSelectPanel = ({
620720
nodeType,
621721
...props
622722
}: DiscourseNodeBaseProps &
623723
RoamBlockSyncProps & { options: string[]; initialValue?: string }) => (
624-
<BaseSelectPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
724+
<BaseSelectPanel
725+
{...props}
726+
initialValue={readWithFallback(
727+
() => getDiscourseNodeSetting<string>(nodeType, props.settingKeys),
728+
props.initialValue,
729+
)}
730+
setter={createDiscourseNodeSetter(nodeType)}
731+
/>
625732
);
626733

627734
export const DiscourseNodeNumberPanel = ({
@@ -633,5 +740,12 @@ export const DiscourseNodeNumberPanel = ({
633740
min?: number;
634741
max?: number;
635742
}) => (
636-
<BaseNumberPanel {...props} setter={createDiscourseNodeSetter(nodeType)} />
743+
<BaseNumberPanel
744+
{...props}
745+
initialValue={readWithFallback(
746+
() => getDiscourseNodeSetting<number>(nodeType, props.settingKeys),
747+
props.initialValue,
748+
)}
749+
setter={createDiscourseNodeSetter(nodeType)}
750+
/>
637751
);

0 commit comments

Comments
 (0)