2021-07-05 06:23:23 -04:00
|
|
|
import { useParams } from "react-router";
|
|
|
|
import { User } from "revolt.js";
|
2021-07-09 09:34:36 -04:00
|
|
|
import { Channels, Message, Servers, Users } from "revolt.js/dist/api/objects";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { ClientboundNotification } from "revolt.js/dist/websocket/notifications";
|
|
|
|
|
2021-07-09 09:34:36 -04:00
|
|
|
import { Link } from "react-router-dom";
|
2021-06-21 16:11:53 -04:00
|
|
|
import { Text } from "preact-i18n";
|
|
|
|
import { useContext, useEffect, useState } from "preact/hooks";
|
|
|
|
|
2021-07-02 08:35:50 -04:00
|
|
|
import { useIntermediate } from "../../../context/intermediate/Intermediate";
|
2021-07-05 06:23:23 -04:00
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
AppContext,
|
|
|
|
ClientStatus,
|
|
|
|
StatusContext,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "../../../context/revoltjs/RevoltClient";
|
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
HookContext,
|
|
|
|
useChannel,
|
|
|
|
useForceUpdate,
|
|
|
|
useUsers,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "../../../context/revoltjs/hooks";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-06 17:52:50 -04:00
|
|
|
import CollapsibleSection from "../../common/CollapsibleSection";
|
2021-07-05 06:23:23 -04:00
|
|
|
import Category from "../../ui/Category";
|
2021-07-05 05:59:48 -04:00
|
|
|
import Preloader from "../../ui/Preloader";
|
2021-07-05 06:23:23 -04:00
|
|
|
import placeholderSVG from "../items/placeholder.svg";
|
|
|
|
|
|
|
|
import { GenericSidebarBase, GenericSidebarList } from "../SidebarBase";
|
|
|
|
import { UserButton } from "../items/ButtonItem";
|
|
|
|
import { ChannelDebugInfo } from "./ChannelDebugInfo";
|
2021-07-09 09:34:36 -04:00
|
|
|
import InputBox from "../../ui/InputBox";
|
|
|
|
import { getState } from "../../../redux";
|
2021-06-21 16:11:53 -04:00
|
|
|
|
|
|
|
interface Props {
|
2021-07-05 06:25:20 -04:00
|
|
|
ctx: HookContext;
|
2021-06-21 16:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function MemberSidebar(props: { channel?: Channels.Channel }) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const ctx = useForceUpdate();
|
|
|
|
const { channel: cid } = useParams<{ channel: string }>();
|
|
|
|
const channel = props.channel ?? useChannel(cid, ctx);
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
switch (channel?.channel_type) {
|
|
|
|
case "Group":
|
|
|
|
return <GroupMemberSidebar channel={channel} ctx={ctx} />;
|
|
|
|
case "TextChannel":
|
|
|
|
return <ServerMemberSidebar channel={channel} ctx={ctx} />;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-21 16:11:53 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
export function GroupMemberSidebar({
|
2021-07-05 06:25:20 -04:00
|
|
|
channel,
|
|
|
|
ctx,
|
2021-07-05 06:23:23 -04:00
|
|
|
}: Props & { channel: Channels.GroupChannel }) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const { openScreen } = useIntermediate();
|
|
|
|
const users = useUsers(undefined, ctx);
|
|
|
|
let members = channel.recipients
|
|
|
|
.map((x) => users.find((y) => y?._id === x))
|
|
|
|
.filter((x) => typeof x !== "undefined") as User[];
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
/*const voice = useContext(VoiceContext);
|
2021-06-21 16:11:53 -04:00
|
|
|
const voiceActive = voice.roomId === channel._id;
|
|
|
|
|
|
|
|
let voiceParticipants: User[] = [];
|
|
|
|
if (voiceActive) {
|
|
|
|
const idArray = Array.from(voice.participants.keys());
|
|
|
|
voiceParticipants = idArray
|
|
|
|
.map(x => users.find(y => y?._id === x))
|
|
|
|
.filter(x => typeof x !== "undefined") as User[];
|
|
|
|
|
|
|
|
members = members.filter(member => idArray.indexOf(member._id) === -1);
|
|
|
|
|
|
|
|
voiceParticipants.sort((a, b) => a.username.localeCompare(b.username));
|
|
|
|
}*/
|
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
members.sort((a, b) => {
|
|
|
|
// ! FIXME: should probably rewrite all this code
|
|
|
|
let l =
|
|
|
|
+(
|
|
|
|
(a.online && a.status?.presence !== Users.Presence.Invisible) ??
|
|
|
|
false
|
|
|
|
) | 0;
|
|
|
|
let r =
|
|
|
|
+(
|
|
|
|
(b.online && b.status?.presence !== Users.Presence.Invisible) ??
|
|
|
|
false
|
|
|
|
) | 0;
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
let n = r - l;
|
|
|
|
if (n !== 0) {
|
|
|
|
return n;
|
|
|
|
}
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return a.username.localeCompare(b.username);
|
|
|
|
});
|
2021-06-21 16:11:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<GenericSidebarBase>
|
|
|
|
<GenericSidebarList>
|
|
|
|
<ChannelDebugInfo id={channel._id} />
|
2021-07-09 09:34:36 -04:00
|
|
|
<Search channel={channel._id} />
|
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
{/*voiceActive && voiceParticipants.length !== 0 && (
|
2021-06-21 16:11:53 -04:00
|
|
|
<Fragment>
|
|
|
|
<Category
|
|
|
|
type="members"
|
|
|
|
text={
|
|
|
|
<span>
|
|
|
|
<Text id="app.main.categories.participants" />{" "}
|
|
|
|
— {voiceParticipants.length}
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{voiceParticipants.map(
|
|
|
|
user =>
|
|
|
|
user && (
|
|
|
|
<LinkProfile user_id={user._id}>
|
|
|
|
<UserButton
|
|
|
|
key={user._id}
|
|
|
|
user={user}
|
|
|
|
context={channel}
|
|
|
|
/>
|
|
|
|
</LinkProfile>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Fragment>
|
|
|
|
)*/}
|
2021-07-06 17:52:50 -04:00
|
|
|
<CollapsibleSection
|
|
|
|
sticky
|
|
|
|
id="members"
|
|
|
|
defaultValue
|
|
|
|
summary={
|
|
|
|
<Category
|
|
|
|
variant="uniform"
|
|
|
|
text={
|
|
|
|
<span>
|
|
|
|
<Text id="app.main.categories.members" /> —{" "}
|
|
|
|
{channel.recipients.length}
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
}>
|
|
|
|
{members.length === 0 && <img src={placeholderSVG} />}
|
|
|
|
{members.map(
|
|
|
|
(user) =>
|
|
|
|
user && (
|
|
|
|
<UserButton
|
|
|
|
key={user._id}
|
|
|
|
user={user}
|
|
|
|
context={channel}
|
|
|
|
onClick={() =>
|
|
|
|
openScreen({
|
|
|
|
id: "profile",
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
)}
|
|
|
|
</CollapsibleSection>
|
2021-07-05 06:25:20 -04:00
|
|
|
</GenericSidebarList>
|
|
|
|
</GenericSidebarBase>
|
|
|
|
);
|
2021-06-21 16:11:53 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
export function ServerMemberSidebar({
|
2021-07-05 06:25:20 -04:00
|
|
|
channel,
|
|
|
|
ctx,
|
2021-07-05 06:23:23 -04:00
|
|
|
}: Props & { channel: Channels.TextChannel }) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const [members, setMembers] = useState<Servers.Member[] | undefined>(
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
const users = useUsers(members?.map((x) => x._id.user) ?? []).filter(
|
|
|
|
(x) => typeof x !== "undefined",
|
|
|
|
ctx,
|
|
|
|
) as Users.User[];
|
|
|
|
const { openScreen } = useIntermediate();
|
|
|
|
const status = useContext(StatusContext);
|
|
|
|
const client = useContext(AppContext);
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (status === ClientStatus.ONLINE && typeof members === "undefined") {
|
|
|
|
client.servers.members
|
|
|
|
.fetchMembers(channel.server)
|
|
|
|
.then((members) => setMembers(members));
|
|
|
|
}
|
|
|
|
}, [status]);
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
// ! FIXME: temporary code
|
|
|
|
useEffect(() => {
|
|
|
|
function onPacket(packet: ClientboundNotification) {
|
|
|
|
if (!members) return;
|
|
|
|
if (packet.type === "ServerMemberJoin") {
|
|
|
|
if (packet.id !== channel.server) return;
|
|
|
|
setMembers([
|
|
|
|
...members,
|
|
|
|
{ _id: { server: packet.id, user: packet.user } },
|
|
|
|
]);
|
|
|
|
} else if (packet.type === "ServerMemberLeave") {
|
|
|
|
if (packet.id !== channel.server) return;
|
|
|
|
setMembers(
|
|
|
|
members.filter(
|
|
|
|
(x) =>
|
|
|
|
!(
|
|
|
|
x._id.user === packet.user &&
|
|
|
|
x._id.server === packet.id
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
client.addListener("packet", onPacket);
|
|
|
|
return () => client.removeListener("packet", onPacket);
|
|
|
|
}, [members]);
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
// copy paste from above
|
|
|
|
users.sort((a, b) => {
|
|
|
|
// ! FIXME: should probably rewrite all this code
|
|
|
|
let l =
|
|
|
|
+(
|
|
|
|
(a.online && a.status?.presence !== Users.Presence.Invisible) ??
|
|
|
|
false
|
|
|
|
) | 0;
|
|
|
|
let r =
|
|
|
|
+(
|
|
|
|
(b.online && b.status?.presence !== Users.Presence.Invisible) ??
|
|
|
|
false
|
|
|
|
) | 0;
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
let n = r - l;
|
|
|
|
if (n !== 0) {
|
|
|
|
return n;
|
|
|
|
}
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return a.username.localeCompare(b.username);
|
|
|
|
});
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<GenericSidebarBase>
|
|
|
|
<GenericSidebarList>
|
|
|
|
<ChannelDebugInfo id={channel._id} />
|
2021-07-09 09:34:36 -04:00
|
|
|
<Search channel={channel._id} />
|
2021-07-06 14:29:27 -04:00
|
|
|
<div>{!members && <Preloader type="ring" />}</div>
|
|
|
|
{members && (
|
2021-07-06 17:52:50 -04:00
|
|
|
<CollapsibleSection
|
2021-07-07 07:37:14 -04:00
|
|
|
//sticky //will re-add later, need to fix css
|
2021-07-06 17:52:50 -04:00
|
|
|
id="members"
|
|
|
|
defaultValue
|
2021-07-07 07:37:14 -04:00
|
|
|
summary={<span>
|
2021-07-06 17:52:50 -04:00
|
|
|
<Text id="app.main.categories.members" />{" "}
|
|
|
|
— {users.length}
|
|
|
|
</span>
|
2021-07-05 06:25:20 -04:00
|
|
|
}
|
2021-07-07 07:37:14 -04:00
|
|
|
|
|
|
|
>
|
2021-07-06 17:52:50 -04:00
|
|
|
{users.length === 0 && <img src={placeholderSVG} />}
|
|
|
|
{users.map(
|
|
|
|
(user) =>
|
|
|
|
user && (
|
|
|
|
<UserButton
|
|
|
|
key={user._id}
|
|
|
|
user={user}
|
|
|
|
context={channel}
|
|
|
|
onClick={() =>
|
|
|
|
openScreen({
|
|
|
|
id: "profile",
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
)}
|
|
|
|
</CollapsibleSection>
|
2021-07-05 06:25:20 -04:00
|
|
|
)}
|
|
|
|
</GenericSidebarList>
|
|
|
|
</GenericSidebarBase>
|
|
|
|
);
|
2021-06-21 16:11:53 -04:00
|
|
|
}
|
2021-07-09 09:34:36 -04:00
|
|
|
|
|
|
|
function Search({ channel }: { channel: string }) {
|
|
|
|
if (!getState().experiments.enabled?.includes('search')) return null;
|
|
|
|
|
|
|
|
const client = useContext(AppContext);
|
|
|
|
const [query,setV] = useState('');
|
|
|
|
const [results,setResults] = useState<Message[]>([]);
|
|
|
|
|
|
|
|
async function search() {
|
|
|
|
let data = await client.channels.searchWithUsers(channel, { query, sort: 'Relevance' }, true);
|
|
|
|
setResults(data.messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CollapsibleSection
|
|
|
|
sticky
|
|
|
|
id="search"
|
|
|
|
defaultValue={false}
|
|
|
|
summary={"Search (BETA)"}>
|
|
|
|
<InputBox style={{ width: '100%' }}
|
|
|
|
onKeyDown={e => e.key === 'Enter' && search()}
|
|
|
|
value={query} onChange={e => setV(e.currentTarget.value)} />
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '4px', marginTop: '8px' }}>
|
|
|
|
{
|
|
|
|
results.map(message => {
|
|
|
|
let href = '';
|
|
|
|
let channel = client.channels.get(message.channel);
|
|
|
|
if (channel?.channel_type === 'TextChannel') {
|
|
|
|
href += `/server/${channel.server}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
href += `/channel/${message.channel}/${message._id}`;
|
|
|
|
|
|
|
|
return <Link to={href}><div style={{ margin: '2px', padding: '6px', background: 'var(--primary-background)' }}>
|
|
|
|
<b>@{ client.users.get(message.author)?.username }</b><br/>
|
|
|
|
{ message.content }
|
|
|
|
</div></Link>
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</CollapsibleSection>
|
|
|
|
)
|
|
|
|
}
|