2021-08-08 15:28:47 -04:00
|
|
|
import { GroupedVirtuoso } from "react-virtuoso";
|
2021-08-08 13:26:16 -04:00
|
|
|
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 15:28:47 -04:00
|
|
|
import styled, { css } 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-13 17:31:28 -04:00
|
|
|
import { memo } from "preact/compat";
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-13 17:31:28 -04:00
|
|
|
import {
|
|
|
|
Screen,
|
|
|
|
useIntermediate,
|
|
|
|
} from "../../../context/intermediate/Intermediate";
|
2021-08-08 13:26:16 -04:00
|
|
|
|
2021-08-08 11:17:16 -04:00
|
|
|
import { UserButton } from "../items/ButtonItem";
|
2021-09-07 08:14:22 -04:00
|
|
|
import { internalEmit } from "../../../lib/eventEmitter";
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 15:28:47 -04:00
|
|
|
export type MemberListGroup = {
|
|
|
|
type: "online" | "offline" | "role";
|
|
|
|
name?: string;
|
|
|
|
users: User[];
|
|
|
|
};
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 15:28:47 -04:00
|
|
|
const ListCategory = styled.div<{ first?: boolean }>`
|
|
|
|
opacity: 0.8;
|
2021-08-08 12:18:58 -04:00
|
|
|
font-size: 0.8em;
|
|
|
|
font-weight: 600;
|
|
|
|
user-select: none;
|
|
|
|
|
2021-08-08 15:28:47 -04:00
|
|
|
padding: 4px 14px;
|
|
|
|
padding-top: 12px;
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 15:28:47 -04:00
|
|
|
color: var(--secondary-foreground);
|
|
|
|
background: var(--secondary-background);
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 15:28:47 -04:00
|
|
|
${(props) =>
|
|
|
|
!props.first &&
|
|
|
|
css`
|
|
|
|
padding-top: 16px;
|
|
|
|
`}
|
|
|
|
`;
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-13 17:31:28 -04:00
|
|
|
const ItemContent = memo(
|
|
|
|
({
|
|
|
|
item,
|
|
|
|
context,
|
|
|
|
openScreen,
|
|
|
|
}: {
|
|
|
|
item: User;
|
|
|
|
context: Channel;
|
|
|
|
openScreen: (screen: Screen) => void;
|
|
|
|
}) => (
|
|
|
|
<UserButton
|
|
|
|
key={item._id}
|
|
|
|
user={item}
|
|
|
|
margin
|
|
|
|
context={context}
|
2021-09-07 08:14:22 -04:00
|
|
|
onClick={e => {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
internalEmit(
|
|
|
|
"MessageBox",
|
|
|
|
"append",
|
|
|
|
`<@${item._id}>`,
|
|
|
|
"mention",
|
|
|
|
);
|
|
|
|
} else[
|
|
|
|
openScreen({
|
|
|
|
id: "profile",
|
|
|
|
user_id: item._id,
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}}
|
2021-08-13 17:31:28 -04:00
|
|
|
/>
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2021-08-08 11:17:16 -04:00
|
|
|
export default function MemberList({
|
|
|
|
entries,
|
2021-08-08 13:26:16 -04:00
|
|
|
context,
|
2021-08-08 11:17:16 -04:00
|
|
|
}: {
|
2021-08-08 15:28:47 -04:00
|
|
|
entries: MemberListGroup[];
|
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 15:28:47 -04:00
|
|
|
|
2021-08-08 11:17:16 -04:00
|
|
|
return (
|
2021-08-08 15:28:47 -04:00
|
|
|
<GroupedVirtuoso
|
|
|
|
groupCounts={entries.map((x) => x.users.length)}
|
|
|
|
groupContent={(index) => {
|
|
|
|
const type = entries[index].type;
|
|
|
|
return (
|
|
|
|
<ListCategory first={index === 0}>
|
2021-08-08 16:50:29 -04:00
|
|
|
{type === "role" ? (
|
|
|
|
<>{entries[index].name}</>
|
|
|
|
) : type === "online" ? (
|
2021-08-08 15:28:47 -04:00
|
|
|
<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>
|
2021-08-13 17:31:28 -04:00
|
|
|
<ItemContent
|
|
|
|
item={item}
|
2021-08-08 15:28:47 -04:00
|
|
|
context={context}
|
2021-08-13 17:31:28 -04:00
|
|
|
openScreen={openScreen}
|
2021-08-08 15:28:47 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
2021-08-08 11:17:16 -04:00
|
|
|
);
|
|
|
|
}
|