2021-08-05 09:47:00 -04:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2022-01-04 09:47:39 -05:00
|
|
|
import { autorun, reaction } from "mobx";
|
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-08-16 12:02:24 -04:00
|
|
|
import { Role } from "revolt-api/types/Servers";
|
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-16 12:02:24 -04:00
|
|
|
import { Server } from "revolt.js/dist/maps/Servers";
|
2021-08-08 11:17:16 -04:00
|
|
|
import { User } from "revolt.js/dist/maps/Users";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2022-01-04 09:40:03 -05:00
|
|
|
import { useContext, useEffect, useState } from "preact/hooks";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
import { defer } from "../../../lib/defer";
|
2021-12-27 08:20:17 -05: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";
|
2021-08-08 15:28:47 -04:00
|
|
|
import MemberList, { MemberListGroup } from "./MemberList";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-08-09 12:34:25 -04:00
|
|
|
export default function MemberSidebar() {
|
|
|
|
const channel = useClient().channels.get(
|
|
|
|
useParams<{ channel: string }>().channel,
|
|
|
|
);
|
2021-07-31 04:34:55 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-04 09:35:08 -05:00
|
|
|
function useEntries(
|
|
|
|
channel: Channel,
|
2022-01-04 09:40:03 -05:00
|
|
|
renderListener: (effect: (keys: string[]) => void) => () => void,
|
2022-01-04 09:35:08 -05:00
|
|
|
isServer?: boolean,
|
|
|
|
) {
|
2021-08-08 13:26:16 -04:00
|
|
|
const client = channel.client;
|
2022-01-04 08:58:24 -05:00
|
|
|
const [entries, setEntries] = useState<MemberListGroup[]>([]);
|
|
|
|
|
2022-01-04 09:40:03 -05:00
|
|
|
function sort(keys: string[]) {
|
2022-01-04 08:58:24 -05:00
|
|
|
defer(() => {
|
|
|
|
const categories: { [key: string]: [User, string][] } = {
|
|
|
|
online: [],
|
|
|
|
offline: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const categoryInfo: { [key: string]: string } = {};
|
|
|
|
|
|
|
|
let roles: Server["roles"] | undefined;
|
|
|
|
let roleList: string[];
|
|
|
|
if (
|
|
|
|
channel.channel_type === "TextChannel" ||
|
|
|
|
channel.channel_type === "VoiceChannel"
|
|
|
|
) {
|
|
|
|
roles = channel.server?.roles;
|
|
|
|
if (roles) {
|
|
|
|
const list = Object.keys(roles)
|
|
|
|
.map((id) => {
|
|
|
|
return [id, roles![id], roles![id].rank ?? 0] as [
|
|
|
|
string,
|
|
|
|
Role,
|
|
|
|
number,
|
|
|
|
];
|
|
|
|
})
|
|
|
|
.filter(([, role]) => role.hoist);
|
|
|
|
|
|
|
|
list.sort((b, a) => b[2] - a[2]);
|
|
|
|
|
|
|
|
list.forEach(([id, role]) => {
|
|
|
|
if (categories[id]) return;
|
|
|
|
categories[id] = [];
|
|
|
|
categoryInfo[id] = role.name;
|
|
|
|
});
|
|
|
|
|
|
|
|
roleList = list.map((x) => x[0]);
|
|
|
|
}
|
2021-08-08 13:26:16 -04:00
|
|
|
}
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
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
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
if (!u) return;
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
const member = client.members.get(key);
|
|
|
|
const sort = member?.nickname ?? u.username;
|
|
|
|
const entry = [u, sort] as [User, string];
|
|
|
|
|
|
|
|
if (!u.online || u.status?.presence === Presence.Invisible) {
|
|
|
|
categories.offline.push(entry);
|
|
|
|
} else {
|
|
|
|
if (isServer) {
|
|
|
|
// Sort users into hoisted roles here.
|
|
|
|
if (member?.roles && roles) {
|
|
|
|
let success = false;
|
|
|
|
for (const role of roleList) {
|
|
|
|
if (member.roles.includes(role)) {
|
|
|
|
categories[role].push(entry);
|
|
|
|
success = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-08-08 16:50:29 -04:00
|
|
|
}
|
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
if (success) return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Sort users into "participants" list here.
|
|
|
|
// For voice calls.
|
2021-08-08 16:50:29 -04:00
|
|
|
}
|
2022-01-04 08:58:24 -05:00
|
|
|
|
|
|
|
categories.online.push(entry);
|
2021-08-08 16:50:29 -04:00
|
|
|
}
|
2022-01-04 08:58:24 -05:00
|
|
|
});
|
2021-08-08 16:50:29 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
Object.keys(categories).forEach((key) =>
|
|
|
|
categories[key].sort((a, b) => a[1].localeCompare(b[1])),
|
|
|
|
);
|
2021-07-29 10:11:21 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
const entries: MemberListGroup[] = [];
|
2021-07-29 10:11:21 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
Object.keys(categoryInfo).forEach((key) => {
|
|
|
|
if (categories[key].length > 0) {
|
|
|
|
entries.push({
|
|
|
|
type: "role",
|
|
|
|
name: categoryInfo[key],
|
|
|
|
users: categories[key].map((x) => x[0]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
if (categories.online.length > 0) {
|
2021-08-08 16:50:29 -04:00
|
|
|
entries.push({
|
2022-01-04 08:58:24 -05:00
|
|
|
type: "online",
|
|
|
|
users: categories.online.map((x) => x[0]),
|
2021-08-08 16:50:29 -04:00
|
|
|
});
|
|
|
|
}
|
2021-08-08 13:26:16 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
if (categories.offline.length > 0) {
|
|
|
|
entries.push({
|
|
|
|
type: "offline",
|
|
|
|
users: categories.offline.map((x) => x[0]),
|
|
|
|
});
|
|
|
|
}
|
2021-08-08 11:17:16 -04:00
|
|
|
|
2022-01-04 08:58:24 -05:00
|
|
|
setEntries(entries);
|
|
|
|
});
|
2022-01-04 09:35:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return renderListener(sort);
|
2021-08-08 13:26:16 -04:00
|
|
|
// eslint-disable-next-line
|
2022-01-04 09:35:08 -05:00
|
|
|
}, [renderListener]);
|
2022-01-04 08:58:24 -05:00
|
|
|
|
|
|
|
return entries;
|
2021-08-08 13:26:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const GroupMemberSidebar = observer(
|
|
|
|
({ channel }: { channel: Channel }) => {
|
2022-01-04 09:40:03 -05:00
|
|
|
const entries = useEntries(channel, (effect) =>
|
2022-01-04 09:47:39 -05:00
|
|
|
autorun(() => effect(channel.recipient_ids!)),
|
2022-01-04 09:35:08 -05:00
|
|
|
);
|
2021-08-08 11:17:16 -04:00
|
|
|
|
|
|
|
return (
|
2022-01-04 08:58:24 -05:00
|
|
|
<GenericSidebarBase data-scroll-offset="with-padding">
|
|
|
|
{/*<Container>
|
|
|
|
{isTouchscreenDevice && <div>Group settings go here</div>}
|
|
|
|
</Container>*/}
|
2021-12-28 11:06:04 -05:00
|
|
|
|
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
|
|
|
|
2022-01-04 08:37:19 -05:00
|
|
|
// ! FIXME: this is temporary code until we get lazy guilds like subscriptions
|
|
|
|
const FETCHED: Set<String> = new Set();
|
|
|
|
|
|
|
|
export function resetMemberSidebarFetched() {
|
|
|
|
FETCHED.clear();
|
|
|
|
}
|
|
|
|
|
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(() => {
|
2022-01-04 08:37:19 -05:00
|
|
|
const server_id = channel.server_id!;
|
|
|
|
if (status === ClientStatus.ONLINE && !FETCHED.has(server_id)) {
|
|
|
|
channel
|
|
|
|
.server!.fetchMembers()
|
|
|
|
.then(() => FETCHED.add(server_id));
|
2021-07-29 10:11:21 -04:00
|
|
|
}
|
2021-08-15 04:58:22 -04:00
|
|
|
// eslint-disable-next-line
|
|
|
|
}, [status, channel.server_id]);
|
2021-07-29 10:11:21 -04:00
|
|
|
|
2022-01-04 09:35:08 -05:00
|
|
|
const entries = useEntries(
|
|
|
|
channel,
|
2022-01-04 09:47:39 -05:00
|
|
|
(effect) => autorun(() => effect([...client.members.keys()])),
|
2022-01-04 09:35:08 -05:00
|
|
|
true,
|
|
|
|
);
|
2021-07-29 10:11:21 -04:00
|
|
|
|
|
|
|
return (
|
2022-01-04 08:58:24 -05:00
|
|
|
<GenericSidebarBase data-scroll-offset="with-padding">
|
|
|
|
{/*<Container>
|
|
|
|
{isTouchscreenDevice && <div>Server settings go here</div>}
|
|
|
|
</Container>*/}
|
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
|
|
|
},
|
|
|
|
);
|