MCPcopy Create free account
hub / github.com/dailydotdev/apps / Section

Function Section

packages/shared/src/components/sidebar/Section.tsx:39–202  ·  view source on GitHub ↗
({
  title,
  items,
  sidebarExpanded,
  shouldShowLabel,
  activePage,
  isItemsButton,
  className,
  flag,
  isAlwaysOpenOnMobile,
  onAdd,
  addHref,
  isV2Panel = false,
}: SectionProps)

Source from the content-addressed store, hash-verified

37}
38
39export function Section({
40 title,
41 items,
42 sidebarExpanded,
43 shouldShowLabel,
44 activePage,
45 isItemsButton,
46 className,
47 flag,
48 isAlwaysOpenOnMobile,
49 onAdd,
50 addHref,
51 isV2Panel = false,
52}: SectionProps): ReactElement {
53 const { flags, updateFlag } = useSettingsContext();
54 const { sidebarRendered } = useSidebarRendered();
55 const shouldAlwaysBeVisible = isAlwaysOpenOnMobile && !sidebarRendered;
56 const currentFlagValue = flag ? flags?.[flag] : undefined;
57 // The collapse toggle only renders alongside a section `title`. Without a
58 // title (e.g. the V2 sidebar's category panels) there is no way to
59 // re-expand, so a persisted `false` flag set from a layout that did show the
60 // toggle would strand the section permanently closed. Titleless sections are
61 // always expanded.
62 const initialIsVisible =
63 !title || isNullOrUndefined(currentFlagValue) ? true : currentFlagValue;
64 // State (not a ref) so toggling re-renders even for sections without a
65 // persisted `flag` (e.g. the settings panel groups) — otherwise the
66 // collapse never visibly happens.
67 const [isVisible, setIsVisible] = useState(initialIsVisible);
68 const shouldRenderItems = !title || isVisible || shouldAlwaysBeVisible;
69 const hasVisibleItems = items.some((item) => !item.isSeparator);
70 const shouldRenderHeader =
71 !!title && (sidebarExpanded || (shouldRenderItems && hasVisibleItems));
72
73 const toggleFlag = () => {
74 const nextIsVisible = !isVisible;
75
76 if (flag) {
77 updateFlag(flag, nextIsVisible);
78 }
79
80 setIsVisible(nextIsVisible);
81 };
82
83 return (
84 <NavSection className={classNames('group/section mt-1', className)}>
85 {shouldRenderHeader && (
86 <NavHeader className="relative hidden laptop:flex">
87 {/* Divider shown when a visible titled section is collapsed */}
88 <div
89 className={classNames(
90 'absolute inset-x-0 flex items-center justify-center px-2 transition-opacity duration-300',
91 sidebarExpanded ? 'opacity-0' : 'opacity-100',
92 )}
93 >
94 <hr className="w-full border-t border-border-subtlest-tertiary" />
95 </div>
96 {/* Header content shown when sidebar is expanded */}

Callers

nothing calls this directly

Calls 3

useSettingsContextFunction · 0.90
isNullOrUndefinedFunction · 0.90
useSidebarRenderedFunction · 0.85

Tested by

no test coverage detected