Switch to React Virtuoso from react-window

This commit is contained in:
Paul
2021-08-08 20:28:47 +01:00
parent a3e2ed3b94
commit a19ff58e12
5 changed files with 105 additions and 148 deletions

View File

@@ -1,135 +1,92 @@
import AutoSizer from "react-virtualized-auto-sizer";
import { VariableSizeList as List } from "react-window";
import { GroupedVirtuoso } from "react-virtuoso";
import { Channel } from "revolt.js/dist/maps/Channels";
import { User } from "revolt.js/dist/maps/Users";
import styled from "styled-components";
import styled, { css } from "styled-components";
import { Text } from "preact-i18n";
import { forwardRef } from "preact/compat";
import {
Screen,
useIntermediate,
} from "../../../context/intermediate/Intermediate";
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { UserButton } from "../items/ButtonItem";
export type MemberListEntry = string | User;
interface ItemData {
entries: MemberListEntry[];
context: Channel;
openScreen: (screen: Screen) => void;
}
export type MemberListGroup = {
type: "online" | "offline" | "role";
name?: string;
users: User[];
};
const PADDING_SIZE = 6;
const ListCategory = styled.div`
height: 100%;
display: flex;
padding: 0 14px;
const ListCategory = styled.div<{ first?: boolean }>`
opacity: 0.8;
font-size: 0.8em;
font-weight: 600;
user-select: none;
flex-direction: column;
justify-content: flex-end;
padding: 4px 14px;
padding-top: 12px;
color: var(--secondary-foreground);
background: var(--secondary-background);
${(props) =>
!props.first &&
css`
padding-top: 16px;
`}
`;
const Row = ({
data,
style: styleIn,
index,
}: {
data: ItemData;
index: number;
style: JSX.CSSProperties;
}) => {
const item = data.entries[index];
const style = {
...styleIn,
top: `${parseFloat(styleIn.top as string) + PADDING_SIZE}px`,
};
if (typeof item === "string") {
const [cat, count] = item.split(":");
return (
<div style={style}>
<ListCategory>
{cat === "online" ? (
<Text id="app.status.online" />
) : (
<Text id="app.status.offline" />
)}
{" - "}
{count}
</ListCategory>
</div>
);
// eslint-disable-next-line
} else {
return (
<div style={style}>
<UserButton
key={item._id}
user={item}
margin
context={data.context}
onClick={() =>
data.openScreen({
id: "profile",
user_id: item._id,
})
}
/>
</div>
);
}
};
// @ts-expect-error Copied directly from example code.
const innerElementType = forwardRef(({ style, ...rest }, ref) => (
<div
// @ts-expect-error Copied directly from example code.
ref={ref}
style={{
...style,
height: `${parseFloat(style.height) + PADDING_SIZE * 2}px`,
}}
{...rest}
/>
));
export default function MemberList({
entries,
context,
}: {
entries: MemberListEntry[];
entries: MemberListGroup[];
context: Channel;
}) {
const { openScreen } = useIntermediate();
return (
<AutoSizer>
{({ width, height }) => (
<List
width={width}
height={height}
itemData={{
entries,
context,
openScreen,
}}
itemCount={entries.length}
innerElementType={innerElementType}
itemSize={(index) =>
typeof entries[index] === "string" ? 24 : 42
}
estimatedItemSize={42}>
{
// eslint-disable-next-line
Row as any
}
</List>
)}
</AutoSizer>
<GroupedVirtuoso
groupCounts={entries.map((x) => x.users.length)}
groupContent={(index) => {
const type = entries[index].type;
return (
<ListCategory first={index === 0}>
{type === "online" ? (
<Text id="app.status.online" />
) : (
<Text id="app.status.offline" />
)}
{" - "}
{entries[index].users.length}
</ListCategory>
);
}}
itemContent={(absoluteIndex, groupIndex) => {
const relativeIndex =
absoluteIndex -
entries
.slice(0, groupIndex)
.reduce((a, b) => a + b.users.length, 0);
const item = entries[groupIndex].users[relativeIndex];
if (!item) return null;
return (
<div>
<UserButton
key={item._id}
user={item}
margin
context={context}
onClick={() =>
openScreen({
id: "profile",
user_id: item._id,
})
}
/>
</div>
);
}}
/>
);
}

View File

@@ -14,7 +14,7 @@ import {
} from "../../../context/revoltjs/RevoltClient";
import { GenericSidebarBase } from "../SidebarBase";
import MemberList from "./MemberList";
import MemberList, { MemberListGroup } from "./MemberList";
export default function MemberSidebar({ channel: obj }: { channel?: Channel }) {
const { channel: channel_id } = useParams<{ channel: string }>();
@@ -73,20 +73,20 @@ function useEntries(channel: Channel, keys: string[], isServer?: boolean) {
categories[key].sort((a, b) => a[1].localeCompare(b[1])),
);
const entries = [];
const entries: MemberListGroup[] = [];
if (categories.online.length > 0) {
entries.push(
`online:${categories.online.length}`,
...categories.online.map((x) => x[0]),
);
entries.push({
type: "online",
users: categories.online.map((x) => x[0]),
});
}
if (categories.offline.length > 0) {
entries.push(
`offline:${categories.offline.length}`,
...categories.offline.map((x) => x[0]),
);
entries.push({
type: "offline",
users: categories.offline.map((x) => x[0]),
});
}
return entries;