Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize UI by only rendering what's visible #2627

Merged
merged 3 commits into from
Feb 29, 2024
Merged
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
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"react-hotkeys-hook": "^3.4.6",
"react-i18next": "^12.0.0",
"react-icons": "^4.10.1",
"react-intersection-observer": "^9.8.1",
"react-markdown": "^8.0.3",
"react-query": "^3.39.3",
"react-time-ago": "^7.2.1",
Expand Down
35 changes: 35 additions & 0 deletions src/renderer/components/IfVisible.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Box } from '@chakra-ui/react';
import { memo } from 'react';
import { useInView } from 'react-intersection-observer';

export interface IfVisibleProps {
height: string | number;
visibleOffset?: number;
forceVisible?: boolean;
children: React.ReactNode;
}
export const IfVisible = memo(
({ height, visibleOffset = 200, forceVisible = false, children }: IfVisibleProps) => {
const { ref, entry } = useInView({
rootMargin: `${visibleOffset}px 0px ${visibleOffset}px 0px`,
});

const finalVisibility = forceVisible || (entry?.isIntersecting ?? false);

return (
<Box
height={typeof height === 'number' ? `${height}px` : height}
ref={ref}
style={{ contain: 'layout size' }}
>
{finalVisibility && (
<>
<Box display="flex" />
{children}
<Box display="flex" />
</>
)}
</Box>
);
}
);
75 changes: 43 additions & 32 deletions src/renderer/components/NodeDocumentation/NodesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { groupBy } from '../../../common/util';
import { BackendContext } from '../../contexts/BackendContext';
import { NodeDocumentationContext } from '../../contexts/NodeDocumentationContext';
import { IconFactory } from '../CustomIcons';
import { IfVisible } from '../IfVisible';
import { SearchBar } from '../SearchBar';

export interface NodesListProps {
Expand Down Expand Up @@ -135,41 +136,51 @@ export const NodesList = memo(
<Text>{category.name}</Text>
</HStack>
</Center>
{categoryNodesByScore.map((node) => {
const isSelected = node.schemaId === selectedSchemaId;
return (
<HStack
_hover={{
backgroundColor: 'var(--bg-700)',
}}
backgroundColor={
isSelected
? 'var(--bg-700)'
: 'var(--bg-800)'
}
borderBottomColor="gray.500"
borderBottomWidth="1px"
borderLeftColor={category.color}
borderLeftWidth={isSelected ? 8 : 4}
cursor="pointer"
key={node.schemaId}
p={2}
ref={isSelected ? selectedElement : undefined}
w="full"
onClick={() => {
setSelectedSchemaId(node.schemaId);
}}
>
<IconFactory icon={node.icon} />
<Text
<IfVisible
forceVisible={categoryNodesByScore.some(
(node) => node.schemaId === selectedSchemaId
)}
height={41 * categoryNodesByScore.length}
>
{categoryNodesByScore.map((node) => {
const isSelected =
node.schemaId === selectedSchemaId;
return (
<HStack
_hover={{
backgroundColor: 'var(--bg-700)',
}}
backgroundColor={
isSelected
? 'var(--bg-700)'
: 'var(--bg-800)'
}
borderBottomColor="gray.500"
borderBottomWidth="1px"
borderLeftColor={category.color}
borderLeftWidth={isSelected ? 8 : 4}
cursor="pointer"
key={node.schemaId}
p={2}
ref={
isSelected ? selectedElement : undefined
}
w="full"
onClick={() => {
setSelectedSchemaId(node.schemaId);
}}
>
{node.name}
</Text>
</HStack>
);
})}
<IconFactory icon={node.icon} />
<Text
cursor="pointer"
key={node.schemaId}
>
{node.name}
</Text>
</HStack>
);
})}
</IfVisible>
</Box>
);
})
Expand Down
27 changes: 18 additions & 9 deletions src/renderer/components/NodeSelectorPanel/RegularAccordionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React, { memo } from 'react';
import { Category, NodeSchema } from '../../../common/common-types';
import { groupBy } from '../../../common/util';
import { IconFactory } from '../CustomIcons';
import { IfVisible } from '../IfVisible';
import { RepresentativeNodeWrapper } from './RepresentativeNodeWrapper';
import { SubcategoryHeading } from './SubcategoryHeading';
import { TextBox } from './TextBox';
Expand Down Expand Up @@ -94,6 +95,9 @@ export const Subcategories = memo(({ collapsed, category, categoryNodes }: Subca
const nodes = byGroup.get(group.id);
if (!nodes) return null;

const nodeHeight = 28;
const nodePadding = 6;

return (
<Box key={group.id}>
<Center>
Expand All @@ -102,15 +106,20 @@ export const Subcategories = memo(({ collapsed, category, categoryNodes }: Subca
group={group}
/>
</Center>
<Box>
{nodes.map((node) => (
<RepresentativeNodeWrapper
collapsed={collapsed}
key={node.schemaId}
node={node}
/>
))}
</Box>
<IfVisible
height={nodeHeight * nodes.length + nodePadding * (nodes.length + 1)}
visibleOffset={600}
>
<Box>
{nodes.map((node) => (
<RepresentativeNodeWrapper
collapsed={collapsed}
key={node.schemaId}
node={node}
/>
))}
</Box>
</IfVisible>
</Box>
);
})}
Expand Down
53 changes: 34 additions & 19 deletions src/renderer/hooks/usePaneNodeSearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
stringifyTargetHandle,
} from '../../common/util';
import { IconFactory } from '../components/CustomIcons';
import { IfVisible } from '../components/IfVisible';
import { BackendContext } from '../contexts/BackendContext';
import { ContextMenuContext } from '../contexts/ContextMenuContext';
import { GlobalContext, GlobalVolatileContext } from '../contexts/GlobalNodeState';
Expand Down Expand Up @@ -293,6 +294,12 @@ const Menu = memo(({ onSelect, schemata, favorites, categories }: MenuProps) =>
.slice(0, groupIndex)
.reduce((acc, g) => acc + g.schemata.length, 0);

const nodeHeight = 28;
const nodePadding = 2;
const placeholderHeight =
nodeHeight * group.schemata.length +
nodePadding * (group.schemata.length + 1);

return (
<Box key={group.categoryId ?? 'favs'}>
<HStack
Expand All @@ -318,25 +325,33 @@ const Menu = memo(({ onSelect, schemata, favorites, categories }: MenuProps) =>
<Text fontSize="xs">{group.name}</Text>
</HStack>

{group.schemata.map((schema, schemaIndex) => {
const index = indexOffset + schemaIndex;
const isSelected = selectedIndex === index;

return (
<SchemaItem
accentColor={getCategoryAccentColor(
categories,
schema.category
)}
isFavorite={favorites.has(schema.schemaId)}
isSelected={isSelected}
key={schema.schemaId}
schema={schema}
scrollRef={isSelected ? scrollRef : undefined}
onClick={onClickHandler}
/>
);
})}
<IfVisible
forceVisible={
indexOffset <= selectedIndex &&
selectedIndex < indexOffset + group.schemata.length
}
height={placeholderHeight}
>
{group.schemata.map((schema, schemaIndex) => {
const index = indexOffset + schemaIndex;
const isSelected = selectedIndex === index;

return (
<SchemaItem
accentColor={getCategoryAccentColor(
categories,
schema.category
)}
isFavorite={favorites.has(schema.schemaId)}
isSelected={isSelected}
key={schema.schemaId}
schema={schema}
scrollRef={isSelected ? scrollRef : undefined}
onClick={onClickHandler}
/>
);
})}
</IfVisible>
</Box>
);
})}
Expand Down
Loading