2021-08-08 11:17:16 -04:00
|
|
|
import AutoSizer from "react-virtualized-auto-sizer";
|
2021-08-08 13:26:16 -04:00
|
|
|
import { VariableSizeList as List } from "react-window";
|
|
|
|
import { Channel } from "revolt.js/dist/maps/Channels";
|
2021-08-08 11:17:16 -04:00
|
|
|
import { User } from "revolt.js/dist/maps/Users";
|
2021-08-08 12:18:58 -04:00
|
|
|
import styled from "styled-components";
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 12:18:58 -04:00
|
|
|
import { Text } from "preact-i18n";
|
2021-08-08 11:17:16 -04:00
|
|
|
import { forwardRef } from "preact/compat";
|
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
import {
|
|
|
|
Screen,
|
|
|
|
useIntermediate,
|
|
|
|
} from "../../../context/intermediate/Intermediate";
|
|
|
|
|
2021-08-08 11:17:16 -04:00
|
|
|
import { UserButton } from "../items/ButtonItem";
|
|
|
|
|
|
|
|
export type MemberListEntry = string | User;
|
|
|
|
interface ItemData {
|
|
|
|
entries: MemberListEntry[];
|
2021-08-08 13:26:16 -04:00
|
|
|
context: Channel;
|
|
|
|
openScreen: (screen: Screen) => void;
|
2021-08-08 11:17:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const PADDING_SIZE = 6;
|
|
|
|
|
2021-08-08 12:18:58 -04:00
|
|
|
const ListCategory = styled.div`
|
2021-08-08 13:26:16 -04:00
|
|
|
height: 100%;
|
2021-08-08 12:18:58 -04:00
|
|
|
display: flex;
|
2021-08-08 13:26:16 -04:00
|
|
|
padding: 0 14px;
|
2021-08-08 12:18:58 -04:00
|
|
|
font-size: 0.8em;
|
|
|
|
font-weight: 600;
|
|
|
|
user-select: none;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: flex-end;
|
|
|
|
color: var(--secondary-foreground);
|
|
|
|
`;
|
|
|
|
|
2021-08-08 11:17:16 -04:00
|
|
|
const Row = ({
|
|
|
|
data,
|
2021-08-08 13:26:16 -04:00
|
|
|
style: styleIn,
|
2021-08-08 11:17:16 -04:00
|
|
|
index,
|
|
|
|
}: {
|
|
|
|
data: ItemData;
|
|
|
|
index: number;
|
|
|
|
style: JSX.CSSProperties;
|
|
|
|
}) => {
|
|
|
|
const item = data.entries[index];
|
2021-08-08 13:26:16 -04:00
|
|
|
const style = {
|
|
|
|
...styleIn,
|
|
|
|
top: `${parseFloat(styleIn.top as string) + PADDING_SIZE}px`,
|
|
|
|
};
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 12:18:58 -04:00
|
|
|
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}>
|
2021-08-08 11:17:16 -04:00
|
|
|
<UserButton
|
|
|
|
key={item._id}
|
|
|
|
user={item}
|
|
|
|
margin
|
2021-08-08 13:26:16 -04:00
|
|
|
context={data.context}
|
2021-08-08 11:17:16 -04:00
|
|
|
onClick={() =>
|
2021-08-08 13:26:16 -04:00
|
|
|
data.openScreen({
|
2021-08-08 11:17:16 -04:00
|
|
|
id: "profile",
|
2021-08-08 13:26:16 -04:00
|
|
|
user_id: item._id,
|
2021-08-08 11:17:16 -04:00
|
|
|
})
|
2021-08-08 13:26:16 -04:00
|
|
|
}
|
2021-08-08 11:17:16 -04:00
|
|
|
/>
|
2021-08-08 12:18:58 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-08-08 11:17:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// @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,
|
2021-08-08 13:26:16 -04:00
|
|
|
context,
|
2021-08-08 11:17:16 -04:00
|
|
|
}: {
|
|
|
|
entries: MemberListEntry[];
|
2021-08-08 13:26:16 -04:00
|
|
|
context: Channel;
|
2021-08-08 11:17:16 -04:00
|
|
|
}) {
|
2021-08-08 13:26:16 -04:00
|
|
|
const { openScreen } = useIntermediate();
|
2021-08-08 11:17:16 -04:00
|
|
|
return (
|
|
|
|
<AutoSizer>
|
|
|
|
{({ width, height }) => (
|
|
|
|
<List
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
itemData={{
|
|
|
|
entries,
|
2021-08-08 13:26:16 -04:00
|
|
|
context,
|
|
|
|
openScreen,
|
2021-08-08 11:17:16 -04:00
|
|
|
}}
|
|
|
|
itemCount={entries.length}
|
|
|
|
innerElementType={innerElementType}
|
2021-08-08 13:26:16 -04:00
|
|
|
itemSize={(index) =>
|
|
|
|
typeof entries[index] === "string" ? 24 : 42
|
|
|
|
}
|
|
|
|
estimatedItemSize={42}>
|
2021-08-08 11:17:16 -04:00
|
|
|
{
|
|
|
|
// eslint-disable-next-line
|
|
|
|
Row as any
|
|
|
|
}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
|
|
|
);
|
|
|
|
}
|