2021-08-05 09:47:00 -04:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2021-07-29 10:11:21 -04:00
|
|
|
import { observer } from "mobx-react-lite";
|
2021-08-08 13:26:16 -04:00
|
|
|
import { useParams } from "react-router-dom";
|
2021-07-30 17:40:49 -04:00
|
|
|
import { Presence } from "revolt-api/types/Users";
|
|
|
|
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-07-05 06:23:23 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
import { useContext, useEffect, useMemo } from "preact/hooks";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
ClientStatus,
|
|
|
|
StatusContext,
|
2021-07-30 17:40:49 -04:00
|
|
|
useClient,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "../../../context/revoltjs/RevoltClient";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
import { GenericSidebarBase } from "../SidebarBase";
|
|
|
|
import MemberList from "./MemberList";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-31 04:34:55 -04:00
|
|
|
export default function MemberSidebar({ channel: obj }: { channel?: Channel }) {
|
|
|
|
const { channel: channel_id } = useParams<{ channel: string }>();
|
|
|
|
const client = useClient();
|
|
|
|
const channel = obj ?? client.channels.get(channel_id);
|
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
switch (channel?.channel_type) {
|
|
|
|
case "Group":
|
2021-07-29 13:41:01 -04:00
|
|
|
return <GroupMemberSidebar channel={channel} />;
|
2021-07-05 06:25:20 -04:00
|
|
|
case "TextChannel":
|
2021-07-29 13:41:01 -04:00
|
|
|
return <ServerMemberSidebar channel={channel} />;
|
2021-07-05 06:25:20 -04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-21 16:11:53 -04:00
|
|
|
}
|
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
function useEntries(channel: Channel, keys: string[], isServer?: boolean) {
|
|
|
|
const client = channel.client;
|
|
|
|
return useMemo(() => {
|
|
|
|
const categories: { [key: string]: [User, string][] } = {
|
|
|
|
online: [],
|
|
|
|
offline: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
keys.forEach((key) => {
|
|
|
|
let u;
|
|
|
|
if (isServer) {
|
|
|
|
const { server, user } = JSON.parse(key);
|
|
|
|
if (server !== channel.server_id) return;
|
|
|
|
u = client.users.get(user);
|
|
|
|
} else {
|
|
|
|
u = client.users.get(key);
|
|
|
|
}
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
if (!u) return;
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
const member = client.members.get(key);
|
|
|
|
const sort = member?.nickname ?? u.username;
|
|
|
|
const entry = [u, sort] as [User, string];
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
if (isServer) {
|
|
|
|
// Sort users into hoisted roles here.
|
|
|
|
} else {
|
|
|
|
// Sort users into "participants" list here.
|
|
|
|
// For voice calls.
|
2021-07-29 10:11:21 -04:00
|
|
|
}
|
2021-07-09 09:34:36 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
if (!u.online || u.status?.presence === Presence.Invisible) {
|
|
|
|
categories.offline.push(entry);
|
|
|
|
} else {
|
|
|
|
categories.online.push(entry);
|
|
|
|
}
|
2021-07-29 10:11:21 -04:00
|
|
|
});
|
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
Object.keys(categories).forEach((key) =>
|
|
|
|
categories[key].sort((a, b) => a[1].localeCompare(b[1])),
|
2021-07-29 10:11:21 -04:00
|
|
|
);
|
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
const entries = [];
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
if (categories.online.length > 0) {
|
2021-08-08 11:17:16 -04:00
|
|
|
entries.push(
|
2021-08-08 12:18:58 -04:00
|
|
|
`online:${categories.online.length}`,
|
2021-08-08 11:17:16 -04:00
|
|
|
...categories.online.map((x) => x[0]),
|
2021-08-08 13:26:16 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (categories.offline.length > 0) {
|
|
|
|
entries.push(
|
2021-08-08 12:18:58 -04:00
|
|
|
`offline:${categories.offline.length}`,
|
2021-08-08 11:17:16 -04:00
|
|
|
...categories.offline.map((x) => x[0]),
|
|
|
|
);
|
2021-08-08 13:26:16 -04:00
|
|
|
}
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
return entries;
|
|
|
|
// eslint-disable-next-line
|
|
|
|
}, [keys]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GroupMemberSidebar = observer(
|
|
|
|
({ channel }: { channel: Channel }) => {
|
|
|
|
const keys = [...channel.recipient_ids!];
|
|
|
|
const entries = useEntries(channel, keys);
|
2021-08-08 11:17:16 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<GenericSidebarBase>
|
2021-08-08 13:26:16 -04:00
|
|
|
<MemberList entries={entries} context={channel} />
|
2021-08-08 11:17:16 -04:00
|
|
|
</GenericSidebarBase>
|
|
|
|
);
|
2021-08-08 13:26:16 -04:00
|
|
|
},
|
|
|
|
);
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
export const ServerMemberSidebar = observer(
|
|
|
|
({ channel }: { channel: Channel }) => {
|
2021-07-30 17:40:49 -04:00
|
|
|
const client = useClient();
|
2021-07-29 10:11:21 -04:00
|
|
|
const status = useContext(StatusContext);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-07-30 17:40:49 -04:00
|
|
|
if (status === ClientStatus.ONLINE) {
|
|
|
|
channel.server!.fetchMembers();
|
2021-07-29 10:11:21 -04:00
|
|
|
}
|
2021-08-05 09:47:00 -04:00
|
|
|
}, [status, channel.server]);
|
2021-07-29 10:11:21 -04:00
|
|
|
|
2021-08-08 13:26:16 -04:00
|
|
|
const keys = [...client.members.keys()];
|
|
|
|
const entries = useEntries(channel, keys, true);
|
2021-07-29 10:11:21 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<GenericSidebarBase>
|
2021-08-08 13:26:16 -04:00
|
|
|
<MemberList entries={entries} context={channel} />
|
2021-07-29 10:11:21 -04:00
|
|
|
</GenericSidebarBase>
|
2021-08-08 13:26:16 -04:00
|
|
|
);
|
2021-07-29 10:11:21 -04:00
|
|
|
},
|
|
|
|
);
|