Skip to content

Commit abcda92

Browse files
committed
global left sidebar settings
1 parent ce8ecb3 commit abcda92

File tree

4 files changed

+154
-223
lines changed

4 files changed

+154
-223
lines changed

apps/roam/src/components/LeftSidebarView.tsx

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByPar
4242
import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage";
4343
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
4444
import { migrateLeftSidebarSettings } from "~/utils/migrateLeftSidebarSettings";
45+
import { useLeftSidebarGlobalSettings } from "./settings/utils/hooks";
46+
import { setGlobalSetting } from "./settings/utils/accessors";
4547

4648
const parseReference = (text: string) => {
4749
const extracted = extractRef(text);
@@ -230,26 +232,37 @@ const PersonalSections = ({ config }: { config: LeftSidebarConfig }) => {
230232
);
231233
};
232234

233-
const GlobalSection = ({ config }: { config: LeftSidebarConfig["global"] }) => {
234-
const [isOpen, setIsOpen] = useState<boolean>(
235-
!!config.settings?.folded.value,
236-
);
237-
if (!config.children?.length) return null;
238-
const isCollapsable = config.settings?.collapsable.value;
235+
const GlobalSection = () => {
236+
const globalSettings = useLeftSidebarGlobalSettings();
237+
const children = globalSettings.Children || [];
238+
const isCollapsable = globalSettings.Settings?.Collapsable ?? false;
239+
const isFolded = globalSettings.Settings?.Folded ?? false;
240+
241+
const [isOpen, setIsOpen] = useState<boolean>(isFolded);
242+
243+
useEffect(() => {
244+
setIsOpen(isFolded);
245+
}, [isFolded]);
246+
247+
const handleToggleFold = useCallback(() => {
248+
if (!isCollapsable) return;
249+
const newFoldedState = !isOpen;
250+
setIsOpen(newFoldedState);
251+
setGlobalSetting(["Left Sidebar", "Settings", "Folded"], newFoldedState);
252+
}, [isCollapsable, isOpen]);
253+
254+
if (!children.length) return null;
255+
256+
const childrenNodes = children.map((uid) => ({
257+
uid,
258+
text: uid,
259+
}));
239260

240261
return (
241262
<>
242263
<div
243264
className="sidebar-title-button flex w-full items-center border-none bg-transparent py-1 pl-6 pr-2.5 font-semibold outline-none"
244-
onClick={() => {
245-
if (!isCollapsable || !config.settings) return;
246-
toggleFoldedState({
247-
isOpen,
248-
setIsOpen,
249-
folded: config.settings.folded,
250-
parentUid: config.settings.uid,
251-
});
252-
}}
265+
onClick={handleToggleFold}
253266
>
254267
<div className="flex w-full items-center justify-between">
255268
<span>GLOBAL</span>
@@ -262,10 +275,10 @@ const GlobalSection = ({ config }: { config: LeftSidebarConfig["global"] }) => {
262275
</div>
263276
{isCollapsable ? (
264277
<Collapse isOpen={isOpen}>
265-
<SectionChildren childrenNodes={config.children} />
278+
<SectionChildren childrenNodes={childrenNodes} />
266279
</Collapse>
267280
) : (
268-
<SectionChildren childrenNodes={config.children} />
281+
<SectionChildren childrenNodes={childrenNodes} />
269282
)}
270283
</>
271284
);
@@ -408,7 +421,7 @@ const LeftSidebarView = ({ onloadArgs }: { onloadArgs: OnloadArgs }) => {
408421
return (
409422
<>
410423
<FavoritesPopover onloadArgs={onloadArgs} />
411-
<GlobalSection config={config.global} />
424+
<GlobalSection />
412425
<PersonalSections config={config} />
413426
</>
414427
);

0 commit comments

Comments
 (0)