Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions src/bundled/channels/MessagesSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,20 +346,17 @@ function ScopedSidebar({
after,
dialogs,
}) => (
<MessagesSidebarEnvironment
value={{
session,
scope,
navigator: props.navigator,
target: props.target,
sidebar,
filter,
soloTitle,
active: () => true,
chatIcons: props.chatIcons,
chatMenus: props.chatMenus,
sidebarRows: props.sidebarRows,
}}
<SidebarEnvironment
session={session}
scope={scope}
navigator={props.navigator}
target={props.target}
sidebar={sidebar}
filter={filter}
soloTitle={soloTitle}
chatIcons={props.chatIcons}
chatMenus={props.chatMenus}
sidebarRows={props.sidebarRows}
>
{props.renderSections({
settled: sourcesSettled,
Expand Down Expand Up @@ -456,7 +453,7 @@ function ScopedSidebar({
);
},
})}
</MessagesSidebarEnvironment>
</SidebarEnvironment>
)}
/>
) : (
Expand Down Expand Up @@ -631,3 +628,53 @@ function RestrictionBanner({
</OwnedContribution>
);
}

const alwaysActive = () => true;
/** A new environment value makes React search every memoized sidebar subtree
* that skips rendering for its consumers, so keep it while its fields hold. */
function SidebarEnvironment({
session,
scope,
navigator,
target,
sidebar,
filter,
soloTitle,
chatIcons,
chatMenus,
sidebarRows,
children,
}: Omit<MessagesSidebarState, "active"> & { children: ReactNode }) {
const value = useMemo(
() => ({
session,
scope,
navigator,
target,
sidebar,
filter,
soloTitle,
active: alwaysActive,
chatIcons,
chatMenus,
sidebarRows,
}),
[
session,
scope,
navigator,
target,
sidebar,
filter,
soloTitle,
chatIcons,
chatMenus,
sidebarRows,
],
);
return (
<MessagesSidebarEnvironment value={value}>
{children}
</MessagesSidebarEnvironment>
);
}
56 changes: 50 additions & 6 deletions src/bundled/channels/TeamSidebarSource.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
import type { SidebarSections } from "../../features/channel-navigation/sidebar-sections-service";
import { createContext, useContext, type ReactNode } from "react";
import {
createContext,
useContext,
useLayoutEffect,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import type {
SidebarComponentSection,
SidebarSectionSource,
} from "../../features/channel-navigation/sidebar-section-components";

/** The shell-mounted inventory remains the one owner of preferences and actions. */
export const TeamSidebarSections = createContext<
readonly SidebarComponentSection[]
>([]);
type Sections = readonly SidebarComponentSection[];
const noSections: Sections = [];
const TeamSections = createContext({
snapshot: () => noSections,
subscribe: (_listener: () => void) => () => {},
});

/** The shell-mounted inventory remains the one owner of preferences and actions.
* Its sections hold new elements on every render. A changing context value
* would make React search each memoized sidebar subtree for consumers, so the
* sections reach their source through a store that keeps its identity. */
export function TeamSidebarSections({
value,
children,
}: {
value: Sections;
children: ReactNode;
}) {
const [store] = useState(() => {
const listeners = new Set<() => void>();
return {
sections: value,
snapshot: () => store.sections,
subscribe(listener: () => void) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
},
notify() {
for (const listener of listeners) listener();
},
};
});
// The source renders below this one, so it reads these sections in the same
// commit as the rows' owners; only a source that skipped it needs the notice.
store.sections = value;
useLayoutEffect(() => store.notify());
return <TeamSections value={store}>{children}</TeamSections>;
}

/** Construct each section's rows below its own error boundary. */
export function TeamSidebarSection({ render }: { render(): ReactNode }) {
Expand All @@ -21,7 +64,8 @@ export const teamSidebarSource: SidebarSectionSource = {
context: "buzz.channels/messages",
scope: "team",
component: function TeamSidebarSource({ render }) {
return render(useContext(TeamSidebarSections));
const store = useContext(TeamSections);
return render(useSyncExternalStore(store.subscribe, store.snapshot));
},
};

Expand Down
33 changes: 22 additions & 11 deletions src/bundled/channels/useSidebarView.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useLayoutEffect, useRef, useState } from "react";
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
import { readView, subscribeView, writeView } from "../../shared/view-state";
import { contributedSidebarSectionKey } from "./sidebar-sections";

Expand Down Expand Up @@ -279,14 +279,25 @@ export function useSidebarView(
},
[update],
);
return {
list,
collapsed: view.collapsed,
contributionOrder: view.contributionOrder,
setContributionOrder,
toggle,
retryReveal,
revealAgain,
beforeReveal,
};
return useMemo(
() => ({
list,
collapsed: view.collapsed,
contributionOrder: view.contributionOrder,
setContributionOrder,
toggle,
retryReveal,
revealAgain,
beforeReveal,
}),
[
view.collapsed,
view.contributionOrder,
setContributionOrder,
toggle,
retryReveal,
revealAgain,
beforeReveal,
],
);
}
Loading