Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix virtua list bugs and restore scroll (#697)

Caleb Tayloreb6fccd09ba0
diff --git a/js/app/bun.lock b/js/app/bun.lock
index 3cd02c6dcd..6777a63053 100644
--- a/js/app/bun.lock
+++ b/js/app/bun.lock
@@ -1,5 +1,6 @@
{
"lockfileVersion": 1,
+ "configVersion": 0,
"workspaces": {
"": {
"name": "@coparse/monorepo",
@@ -102,7 +103,7 @@
"tslog": "^4.9.3",
"unicode-emoji-json": "^0.8.0",
"uuid": "^10.0.0",
- "virtua": "^0.41.5",
+ "virtua": "0.48.1",
"vite-plugin-wasm": "^3.5.0",
"vitest": "^3.2.4",
"zod": "^3.23.8",
@@ -326,9 +327,9 @@
"esbuild",
],
"patchedDependencies": {
"@kobalte/[email protected]": "patches/@kobalte%[email protected]",
"@thisbeyond/[email protected]": "patches/@thisbeyond%[email protected]",
"@lexical/[email protected]": "patches/@lexical%[email protected]",
},
@@ -3098,7 +3099,7 @@
"vfile-message": ["[email protected]", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-stringify-position": "^4.0.0" } }, "sha512-jRDZ1IMLttGj41KcZvlrYAaI3CfqpLpfpf+Mfig13viT6NKvRzWZ+lXz0Y5D60w6uJIBAOGq9mSHf0gktF0duw=="],
- "virtua": ["[email protected]", "", { "peerDependencies": { "react": ">=16.14.0", "react-dom": ">=16.14.0", "solid-js": ">=1.0", "svelte": ">=5.0", "vue": ">=3.2" }, "optionalPeers": ["react", "react-dom", "solid-js", "svelte", "vue"] }, "sha512-x1vsA9qIQNBFcCs1rzCjyYdMvDu/kT6o6zwwQnyqFOFdOyIzqyzU3WfR/hJC8WxUZXSCo2LkuoqapL8VDDMQPg=="],
+ "virtua": ["[email protected]", "", { "peerDependencies": { "react": ">=16.14.0", "react-dom": ">=16.14.0", "solid-js": ">=1.0", "svelte": ">=5.0", "vue": ">=3.2" }, "optionalPeers": ["react", "react-dom", "solid-js", "svelte", "vue"] }, "sha512-bjVdaIIc/V1XLjWg17DNRNDO919+9caYdMHTNFaQ9GGSpCv2/b6uH9/IR5BfAQnxBcKpH0kQysZXdLLWN1VULw=="],
"vite": ["[email protected]", "", { "dependencies": { "esbuild": "^0.25.0", "fdir": "^6.4.4", "picomatch": "^4.0.2", "postcss": "^8.5.3", "rollup": "^4.34.9", "tinyglobby": "^0.2.13" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", "jiti": ">=1.21.0", "less": "*", "lightningcss": "^1.21.0", "sass": "*", "sass-embedded": "*", "stylus": "*", "sugarss": "*", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "jiti", "less", "lightningcss", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-+Oxm7q9hDoLMyJOYfUYBuHQo+dkAloi33apOPP56pzj+vsdJDzr+j1NISE5pyaAuKL4A3UD34qd0lx5+kfKp2g=="],
diff --git a/js/app/package.json b/js/app/package.json
index 55a28e29e7..598239a0f7 100644
--- a/js/app/package.json
+++ b/js/app/package.json
@@ -141,7 +141,7 @@
"tslog": "^4.9.3",
"unicode-emoji-json": "^0.8.0",
"uuid": "^10.0.0",
- "virtua": "^0.41.5",
+ "virtua": "0.48.1",
"vite-plugin-wasm": "^3.5.0",
"vitest": "^3.2.4",
"zod": "^3.23.8"
@@ -193,8 +193,8 @@
"@thisbeyond/[email protected]": "patches/@thisbeyond%[email protected]",
"@lexical/[email protected]": "patches/@lexical%[email protected]",
- "@kobalte/[email protected]": "patches/@kobalte%[email protected]"
+ "@kobalte/[email protected]": "patches/@kobalte%[email protected]",
},
"overrides": {
"cross-spawn": "7.0.6"
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsx
index 80d9f3d794..0f0c111fed 100644
--- a/js/app/packages/app/component/Root.tsx
+++ b/js/app/packages/app/component/Root.tsx
@@ -49,7 +49,6 @@ import {
onMount,
type ParentProps,
Show,
- Suspense,
} from 'solid-js';
import { currentThemeId } from '../../block-theme/signals/themeSignals';
import {
@@ -67,6 +66,7 @@ import { GlobalAppStateProvider } from './GlobalAppState';
import { Layout } from './Layout';
import MacroJump from './MacroJump';
import Onboarding from './Onboarding';
+import { SuspenseContextComp } from './SuspenseContext';
import { LAYOUT_ROUTE } from './split-layout/SplitLayoutRoute';
const { track, identify, TrackingEvents } = withAnalytics();
@@ -348,6 +348,24 @@ export function Root() {
const tabTitle = () => formatTabTitle(tabInfo());
const routerBase = isTauri() ? '/' : '/app';
+ let runRootWarningLog = false;
+ const RootSuspenseFallback = () => {
+ const runWarningLog = () => {
+ if (!runRootWarningLog) {
+ setTimeout(() => {
+ runRootWarningLog = true;
+ });
+ return;
+ }
+
+ console.warn('Root Suspsense Triggered');
+ };
+
+ runWarningLog();
+
+ return '';
+ };
+
return (
<MaybeTauriProvider>
<MetaProvider>
@@ -356,7 +374,7 @@ export function Root() {
<ChannelsContextProvider>
<Title>{tabTitle()}</Title>
<MacroJump />
- <Suspense fallback={''}>
+ <SuspenseContextComp fallback={<RootSuspenseFallback />}>
<IsomorphicRouter
transformUrl={transformShortIdInUrlPathname}
root={Layout}
@@ -369,7 +387,7 @@ export function Root() {
children: ROUTES,
}}
</IsomorphicRouter>
- </Suspense>
+ </SuspenseContextComp>
<ToastRegion />
<Show when={ENABLE_WEBSOCKET_DEBUGGER}>
<WebsocketDebugger />
diff --git a/js/app/packages/app/component/Soup.tsx b/js/app/packages/app/component/Soup.tsx
index 12aadf04ba..278b6b165b 100644
--- a/js/app/packages/app/component/Soup.tsx
+++ b/js/app/packages/app/component/Soup.tsx
@@ -51,6 +51,7 @@ import {
import { Dynamic } from 'solid-js/web';
import { EntityModal } from './EntityModal/EntityModal';
import { HelpDrawer } from './HelpDrawer';
+import { SuspenseContextComp } from './SuspenseContext';
import { SplitHeaderLeft } from './split-layout/components/SplitHeader';
import { SplitTabs } from './split-layout/components/SplitTabs';
import type { SplitPanelContextType } from './split-layout/context';
@@ -96,9 +97,9 @@ const ViewWithSearch: Component<{
</Suspense>
</Match>
<Match when={true}>
- <Suspense>
+ <SuspenseContextComp fallback={''}>
<UnifiedListView />
- </Suspense>
+ </SuspenseContextComp>
</Match>
</Switch>
</ViewTab>
diff --git a/js/app/packages/app/component/SoupContext.tsx b/js/app/packages/app/component/SoupContext.tsx
index 798c8373b3..617cd98f31 100644
--- a/js/app/packages/app/component/SoupContext.tsx
+++ b/js/app/packages/app/component/SoupContext.tsx
@@ -303,7 +303,9 @@ export function createNavigationEntityListShortcut({
}
});
} else {
- const firstIndex = virtualizerHandle()?.findStartIndex();
+ const handle = virtualizerHandle();
+ if (!handle) return;
+ const firstIndex = handle.findItemIndex(handle.scrollOffset);
if (!firstIndex) return;
const elem = getEntityElAtIndex(firstIndex);
if (elem instanceof HTMLElement) elem.focus();
@@ -863,7 +865,7 @@ export function createNavigationEntityListShortcut({
virtualizerHandle()?.scrollToIndex(index, {
// align: mode === 'jump' && axis === 'end' ? 'end' : undefined,
// align: align(),
- // align: index() < virtuaRef()!.findStartIndex() ? 'start' : 'end',
+ // align: index() < virtuaRef()!.findItemIndex(virtuaRef()!.scrollOffset) ? 'start' : 'end',
align: 'nearest',
// offset: 50,
});
@@ -1044,64 +1046,6 @@ export function createNavigationEntityListShortcut({
return true;
};
- let virtuaMount = true;
- createEffect(
- on(virtualizerHandle, (virtuaRef) => {
- if (!virtuaRef) return;
-
- // reselect entity on mount
- if (!jumpedToEnd() && activeHighlightedId()) {
- if (!virtuaMount) return;
-
- virtuaMount = false;
- scrollToEntityFromId();
- const scrollOffset = viewData()?.scrollOffset;
- // restore scroll overrides scroll to id
- const offset = scrollOffset;
- if (offset) virtuaRef.scrollTo(offset);
-
- // only add scroll event after virtua is done scrolling to scrollTop/index
- requestAnimationFrame(() => {
- requestAnimationFrame(() => {
- addScrollEventToList();
- });
- });
- } else {
- addScrollEventToList();
- }
- })
- );
-
- createEffect(
- on([entities, virtualizerHandle] as const, ([curr, virtuaRef], _prev) => {
- const prev = _prev?.[0];
- if (!curr || !prev || !virtuaRef) return;
-
- const newIndex = getHighlightedEntity()?.index;
-
- // ReSelectEntity
- // ScrollTo and Select correct Entity after EntityList fetches new page, since list shuffles items
- if (newIndex && curr[newIndex]?.id !== prev[newIndex]?.id) {
- scrollToEntityFromId().then(() => {
- const entityEl = getSelectedEntityEl();
-
- if (jumpedToEnd()) {
- // only refocus when navigation hotkey is activated before
-
- if (entityEl instanceof HTMLElement) {
- entityEl.focus();
- setTimeout(() => {
- entityEl.focus();
- setJumpedToEnd(false);
- });
- return true;
- }
- }
- });
- }
- })
- );
-
registerHotkey({
scopeId: splitHotkeyScope,
description: 'Root Modify selection',
diff --git a/js/app/packages/app/component/SuspenseContext.tsx b/js/app/packages/app/component/SuspenseContext.tsx
new file mode 100644
index 0000000000..77d192af48
--- /dev/null
+++ b/js/app/packages/app/component/SuspenseContext.tsx
@@ -0,0 +1,50 @@
+import {
+ type Accessor,
+ createContext,
+ createSignal,
+ createUniqueId,
+ type JSX,
+ onCleanup,
+ type ParentComponent,
+ Suspense,
+ useContext,
+} from 'solid-js';
+
+const SuspenseContext = createContext<{
+ isPending: Accessor<boolean>;
+}>();
+
+export const useSuspenseContext = () => {
+ const context = useContext(SuspenseContext);
+ if (!context) {
+ throw new Error(
+ `SuspenseContext is not found. Make sure you're calling it within a SuspenseContextProvider.`
+ );
+ }
+
+ return context;
+};
+
+const [isPending, setIsPending] = createSignal(false);
+
+export const SuspenseContextComp: ParentComponent<{
+ id?: string;
+ fallback?: JSX.Element;
+}> = (props) => {
+ const _id = props.id ?? createUniqueId(); // for debugging
+ const Fallback: ParentComponent = () => {
+ setIsPending(true);
+ onCleanup(() => {
+ setTimeout(() => {
+ setIsPending(false);
+ });
+ });
+ return <>{props.fallback}</>;
+ };
+
+ return (
+ <SuspenseContext.Provider value={{ isPending }}>
+ <Suspense fallback={<Fallback />}>{props.children}</Suspense>
+ </SuspenseContext.Provider>
+ );
+};
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 6d880f5563..5bc96f4173 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -1012,6 +1012,7 @@ export function UnifiedListView(props: UnifiedListViewProps) {
createUnifiedInfiniteList<
WithNotification<WithSearch<EntityData> | EntityData>
>({
+ id: `${selectedView()}-${splitContext.handle.id}`,
entityInfiniteQueries: [
{
query: dssInfiniteQuery,
diff --git a/js/app/packages/block-canvas/component/FileSelector.tsx b/js/app/packages/block-canvas/component/FileSelector.tsx
index 6984bd444a..06189e21d4 100644
--- a/js/app/packages/block-canvas/component/FileSelector.tsx
+++ b/js/app/packages/block-canvas/component/FileSelector.tsx
@@ -113,7 +113,7 @@ export function FileSelector() {
<VList
data={userFiles()}
style={{ height: '320px', 'overflow-x': 'hidden' }}
- overscan={10}
+ bufferSize={10 * 40}
>
{(item) => (
<DropdownMenu.Item>
diff --git a/js/app/packages/block-canvas/component/MediaSelector.tsx b/js/app/packages/block-canvas/component/MediaSelector.tsx
index 9b523e0030..db8695cdd3 100644
--- a/js/app/packages/block-canvas/component/MediaSelector.tsx
+++ b/js/app/packages/block-canvas/component/MediaSelector.tsx
@@ -225,7 +225,7 @@ export function MediaSelector() {
<VList
data={userMediaFiles()}
style={{ height: '256px' }}
- overscan={10}
+ bufferSize={500}
>
{(media) => (
<DropdownMenu.Item closeOnSelect={true} class="w-full">
diff --git a/js/app/packages/block-channel/component/AttachMenu.tsx b/js/app/packages/block-channel/component/AttachMenu.tsx
index 54106a0f6e..9a44bcf39e 100644
--- a/js/app/packages/block-channel/component/AttachMenu.tsx
+++ b/js/app/packages/block-channel/component/AttachMenu.tsx
@@ -150,7 +150,7 @@ export function AttachMenu(props: AttachMenuProps) {
>
<VList
data={rankedHistory()}
- overscan={10}
+ bufferSize={10 * 40}
itemSize={40}
style={{ height: '100%', contain: 'content' }}
>
diff --git a/js/app/packages/block-channel/component/MessageList/MessageList.tsx b/js/app/packages/block-channel/component/MessageList/MessageList.tsx
index e3c7d5a539..8e77a3d65b 100644
--- a/js/app/packages/block-channel/component/MessageList/MessageList.tsx
+++ b/js/app/packages/block-channel/component/MessageList/MessageList.tsx
@@ -174,7 +174,9 @@ export function MessageList(props: MessageListProps) {
const list = props.orderedMessages();
if (!handle || !list || list.length === 0) return;
- const endIndex = handle.findEndIndex?.();
+ const endIndex = handle.findItemIndex(
+ handle.scrollOffset + handle.viewportSize
+ );
if (endIndex === undefined) return;
const index = clampIndex(endIndex, 0, list.length - 1);
@@ -466,7 +468,9 @@ export function MessageList(props: MessageListProps) {
on(filteredTopLevelMessages, (newFilteredMessages, oldFilteredMessages) => {
const handle = virtualHandle();
if (!handle) return;
- const lastIndexInView = handle.findEndIndex();
+ const lastIndexInView = handle.findItemIndex(
+ handle.scrollOffset + handle.viewportSize
+ );
const lastItemOffset = handle.getItemOffset(
(oldFilteredMessages?.length ?? 0) - 1
);
@@ -572,7 +576,9 @@ export function MessageList(props: MessageListProps) {
const newCount = splitCount();
const handle = untrack(virtualHandle);
if (handle && prev !== 0 && newCount > prev) {
- const endIndex = handle.findEndIndex();
+ const endIndex = handle.findItemIndex(
+ handle.scrollOffset + handle.viewportSize
+ );
queueMicrotask(() => {
handle.scrollToIndex(endIndex, {
align: 'end',
@@ -604,7 +610,10 @@ export function MessageList(props: MessageListProps) {
if (
firstUnviewedIndex !== undefined &&
firstUnviewedIndex >= 0 &&
- (virtualHandle()?.findEndIndex() ?? 0) >= firstUnviewedIndex
+ (virtualHandle()?.findItemIndex(
+ (virtualHandle()?.scrollOffset ?? 0) +
+ (virtualHandle()?.viewportSize ?? 0)
+ ) ?? 0) >= firstUnviewedIndex
) {
setUnviewedMessages(undefined);
}
@@ -673,7 +682,7 @@ export function MessageList(props: MessageListProps) {
itemSize={BASE_ITEM_SIZE}
data-channel-message-list
data={rows() ?? []}
- overscan={10}
+ bufferSize={10 * BASE_ITEM_SIZE}
keepMounted={keepMountedIndices()}
onScroll={handleScroll}
onScrollEnd={() => {
diff --git a/js/app/packages/block-channel/component/ParticipantManager.tsx b/js/app/packages/block-channel/component/ParticipantManager.tsx
index 6426310c4f..33f9c2d276 100644
--- a/js/app/packages/block-channel/component/ParticipantManager.tsx
+++ b/js/app/packages/block-channel/component/ParticipantManager.tsx
@@ -196,7 +196,7 @@ export function ParticipantList(props: {
height: '300px',
width: '100%',
}}
- overscan={10}
+ bufferSize={500}
>
{(participant) => (
<UserItem
diff --git a/js/app/packages/block-email/component/AttachMenu.tsx b/js/app/packages/block-email/component/AttachMenu.tsx
index dfa8a6f9bd..21cf3dcdd1 100644
--- a/js/app/packages/block-email/component/AttachMenu.tsx
+++ b/js/app/packages/block-email/component/AttachMenu.tsx
@@ -153,7 +153,7 @@ export function AttachMenu(props: AttachMenuProps) {
>
<VList
data={rankedHistory()}
- overscan={10}
+ bufferSize={10 * 40}
itemSize={40}
style={{ height: '100%', contain: 'content' }}
>
diff --git a/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx b/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
index f055f752c2..dd209a930c 100644
--- a/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
+++ b/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
@@ -308,7 +308,7 @@ export function ChatAttachMenu(props: ChatAttachMenuProps) {
<VList
ref={setVlistRef}
data={rankedHistory()}
- overscan={10}
+ bufferSize={10 * 40}
itemSize={40}
style={{ height: '100%', contain: 'content' }}
>
diff --git a/js/app/packages/core/component/AI/component/tool/ListDocuments.tsx b/js/app/packages/core/component/AI/component/tool/ListDocuments.tsx
index 6606b8cc21..f1a5fa3d2e 100644
--- a/js/app/packages/core/component/AI/component/tool/ListDocuments.tsx
+++ b/js/app/packages/core/component/AI/component/tool/ListDocuments.tsx
@@ -70,7 +70,7 @@ const ListDocumentsToolResponse = (props: {
<div class="max-h-[480px] overflow-hidden">
<VList
data={results()}
- overscan={5}
+ bufferSize={5 * 32}
itemSize={32}
style={{
height: `${Math.min(results().length * 32, 480)}px`,
diff --git a/js/app/packages/core/component/AI/component/tool/ListEmails.tsx b/js/app/packages/core/component/AI/component/tool/ListEmails.tsx
index 8c47fdec58..ccfb75e3ba 100644
--- a/js/app/packages/core/component/AI/component/tool/ListEmails.tsx
+++ b/js/app/packages/core/component/AI/component/tool/ListEmails.tsx
@@ -65,7 +65,7 @@ const ListEmailsToolResponse = (props: { results: ListEmailsResult[] }) => {
<div class="max-h-[480px] overflow-hidden">
<VList
data={results()}
- overscan={5}
+ bufferSize={5 * 32}
itemSize={32}
style={{
height: `${Math.min(results().length * 32, 480)}px`,
diff --git a/js/app/packages/core/component/AI/component/tool/UnifiedSearch.tsx b/js/app/packages/core/component/AI/component/tool/UnifiedSearch.tsx
index 0d2c1ef1a1..ba81d521ff 100644
--- a/js/app/packages/core/component/AI/component/tool/UnifiedSearch.tsx
+++ b/js/app/packages/core/component/AI/component/tool/UnifiedSearch.tsx
@@ -122,7 +122,7 @@ const UnifiedSearchToolResponse = (props: {
<div class="max-h-[480px] overflow-hidden">
<VList
data={results()}
- overscan={5}
+ bufferSize={5 * 32}
itemSize={32}
style={{
height: `${Math.min(results().length * 32, 480)}px`,
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/EmojiMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/menu/EmojiMenu.tsx
index 08e292a077..d2ec0198f8 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/EmojiMenu.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/EmojiMenu.tsx
@@ -146,7 +146,11 @@ export function EmojiMenu(props: EmojiMenuProps) {
e.preventDefault();
e.stopPropagation();
setSelectedIndex((prev) => (prev + 1) % items.length);
- const endIndex = virtualHandle()?.findEndIndex();
+ const handle = virtualHandle();
+ if (!handle) break;
+ const endIndex = handle.findItemIndex(
+ handle.scrollOffset + handle.viewportSize
+ );
if (endIndex && endIndex === selectedIndex()) {
virtualHandle()?.scrollToIndex(selectedIndex() + 1, {
align: 'center',
@@ -159,7 +163,9 @@ export function EmojiMenu(props: EmojiMenuProps) {
e.preventDefault();
e.stopPropagation();
setSelectedIndex((prev) => (prev - 1 + items.length) % items.length);
- const startIndex = virtualHandle()?.findStartIndex();
+ const handle = virtualHandle();
+ if (!handle) break;
+ const startIndex = handle.findItemIndex(handle.scrollOffset);
if (startIndex && startIndex === selectedIndex()) {
virtualHandle()?.scrollToIndex(selectedIndex() - 1, {
align: 'center',
@@ -177,8 +183,10 @@ export function EmojiMenu(props: EmojiMenuProps) {
e.preventDefault();
e.stopPropagation();
if (e.shiftKey) {
+ const handle = virtualHandle();
+ if (!handle) break;
setSelectedIndex((prev) => (prev - 1 + items.length) % items.length);
- const startIndex = virtualHandle()?.findStartIndex();
+ const startIndex = handle.findItemIndex(handle.scrollOffset);
if (startIndex && startIndex === selectedIndex()) {
virtualHandle()?.scrollToIndex(selectedIndex() - 1, {
align: 'center',
@@ -186,7 +194,11 @@ export function EmojiMenu(props: EmojiMenuProps) {
}
} else {
setSelectedIndex((prev) => (prev + 1) % items.length);
- const endIndex = virtualHandle()?.findEndIndex();
+ const handle = virtualHandle();
+ if (!handle) break;
+ const endIndex = handle.findItemIndex(
+ handle.scrollOffset + handle.viewportSize
+ );
if (endIndex && endIndex === selectedIndex()) {
virtualHandle()?.scrollToIndex(selectedIndex() + 1, {
align: 'center',
diff --git a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
index f0629d7197..328ffecb31 100644
--- a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
+++ b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
@@ -1,3 +1,4 @@
+import { useSuspenseContext } from '@app/component/SuspenseContext';
import { CustomScrollbar } from '@core/component/CustomScrollbar';
import Fragment from '@core/util/Fragment';
import { onElementConnect } from '@solid-primitives/lifecycle';
@@ -39,6 +40,14 @@ import type {
import type { WithSearch } from '../types/search';
import { Entity } from './Entity';
+const cacheMap = new Map<
+ string,
+ {
+ offset: number;
+ cache?: any; // TBD
+ }
+>();
+
/**
* Merges search data from two entities, preferring service source with local as fallback.
* - Uses service entity as base
@@ -193,6 +202,7 @@ const getOperations = <T extends Partial<EntityQueryOperations>>(
};
interface UnifiedInfiniteListContext<T extends EntityData> {
+ id: string;
entityInfiniteQueries: Array<
EntityQueryWithOperations<
EntityData | WithSearch<EntityData>,
@@ -210,6 +220,7 @@ interface UnifiedInfiniteListContext<T extends EntityData> {
}
export function createUnifiedInfiniteList<T extends EntityData>({
+ id,
entityInfiniteQueries,
entityQueries,
entityMapper = (entity: EntityData) => entity as T,
@@ -474,6 +485,63 @@ export function createUnifiedInfiniteList<T extends EntityData>({
onCleanup(() => debouncedFetchMore.clear());
+ const [virtualizerHandle, setVirtualizerHandle] =
+ createSignal<VirtualizerHandle>();
+ // const cacheKey = createMemo(() => (id ? `list-cache-${id}` : null));
+ const cacheKey = `list-cache-${id}`;
+
+ // Restore scroll position on mount
+ const restoreScrollPosition = () => {
+ const handle = virtualizerHandle();
+ const { offset: cachedOffset } = cacheMap.get(cacheKey) || { offset: 0 };
+ console.log('restoreScrollPosition', { cacheKey, handle, cachedOffset });
+ if (handle && cachedOffset) {
+ console.log({ cachedOffset });
+ handle.scrollTo(cachedOffset);
+ }
+ };
+
+ createEffect(
+ on(virtualizerHandle, (virtualizerHandle, prev) => {
+ if (virtualizerHandle && prev == null) {
+ restoreScrollPosition();
+ }
+ })
+ );
+
+ const cacheVirtualizerHandle = () => {
+ const handle = virtualizerHandle();
+ const key = cacheKey;
+
+ if (handle && key) {
+ const { scrollOffset } = handle;
+ console.log('cacheVirtualizerHandle', { key, scrollOffset });
+ cacheMap.set(key, { offset: handle.scrollOffset });
+ }
+ };
+ const { isPending } = useSuspenseContext();
+
+ // Save scroll position and cache on cleanup
+ onCleanup(() => {
+ cacheVirtualizerHandle();
+ });
+
+ createEffect(
+ on(
+ isPending,
+ (isPending, prevIsPending) => {
+ if (isPending) {
+ console.log(cacheKey, 'cache BY SUSPENSE');
+ cacheVirtualizerHandle();
+ }
+ if (isPending === false && prevIsPending === true) {
+ restoreScrollPosition();
+ }
+ },
+ { defer: true }
+ )
+ );
+
return (
<Switch>
<Match
@@ -502,11 +570,18 @@ export function createUnifiedInfiniteList<T extends EntityData>({
}}
>
<VList
- ref={props.virtualizerHandle}
+ ref={(ref) => {
+ // runs before onCleanup
+ // prevent ref from being set to null on cleanup otherwise the scroll position will be lost in onCleanup saving the cache
+ if (ref) {
+ setVirtualizerHandle(ref);
+ }
+ props.virtualizerHandle?.(ref);
+ }}
data={sortedEntitiesStore}
class={`${LIST_WRAPPER} scrollbar-hidden`}
data-unified-entity-list
- overscan={computedOverscan()}
+ bufferSize={computedOverscan() * 50}
>
{(entity, index) => {
if (
diff --git a/js/app/patches/[email protected] b/js/app/patches/[email protected]
similarity index 76%
rename from js/app/patches/[email protected]
rename to js/app/patches/[email protected]
index cffde897c4..e3fe9a6b3c 100644
--- a/js/app/patches/[email protected]
+++ b/js/app/patches/[email protected]
@@ -1,8 +1,8 @@
diff --git a/lib/solid/index.jsx b/lib/solid/index.jsx
-index e234246838a529cae40cb80bf1f907b6906c5221..a56ec7862465577c16d7879711f1eef1fd866ba8 100644
+index 84788e713100b0e5a746bae3411a789002b42b8d..a62faf6f759e654aea303717fd18f9767428f7be 100644
--- a/lib/solid/index.jsx
+++ b/lib/solid/index.jsx
-@@ -1032,7 +1032,7 @@ const createResizeObserver = (cb) => {
+@@ -1069,7 +1069,7 @@ const createResizeObserver = (cb) => {
// https://www.w3.org/TR/resize-observer/#intro
(ro ||
// https://bugs.chromium.org/p/chromium/issues/detail?id=1491739