revite/src/pages/friends/Friends.tsx

216 lines
8.7 KiB
TypeScript
Raw Normal View History

2021-08-05 14:47:00 +01:00
import { ChevronRight } from "@styled-icons/boxicons-regular";
2021-07-05 11:23:23 +01:00
import { UserDetail, MessageAdd, UserPlus } from "@styled-icons/boxicons-solid";
2021-07-29 15:11:21 +01:00
import { observer } from "mobx-react-lite";
import { RelationshipStatus, Presence } from "revolt-api/types/Users";
import { User } from "revolt.js/dist/maps/Users";
2021-07-05 11:23:23 +01:00
import styles from "./Friend.module.scss";
import { Text } from "preact-i18n";
import { TextReact } from "../../lib/i18n";
2021-07-02 11:39:07 +02:00
import { isTouchscreenDevice } from "../../lib/isTouchscreenDevice";
2021-07-05 11:23:23 +01:00
2021-06-19 20:00:30 +01:00
import { useIntermediate } from "../../context/intermediate/Intermediate";
import { useClient } from "../../context/revoltjs/RevoltClient";
2021-07-05 11:23:23 +01:00
import CollapsibleSection from "../../components/common/CollapsibleSection";
2021-07-05 11:23:23 +01:00
import Tooltip from "../../components/common/Tooltip";
import UserIcon from "../../components/common/user/UserIcon";
import Header from "../../components/ui/Header";
import IconButton from "../../components/ui/IconButton";
import { Children } from "../../types/Preact";
import { Friend } from "./Friend";
2021-06-19 20:00:30 +01:00
2021-07-29 15:11:21 +01:00
export default observer(() => {
2021-07-05 11:25:20 +01:00
const { openScreen } = useIntermediate();
2021-06-19 20:00:30 +01:00
const client = useClient();
const users = [...client.users.values()];
2021-07-05 11:25:20 +01:00
users.sort((a, b) => a.username.localeCompare(b.username));
2021-06-19 20:00:30 +01:00
2021-07-05 11:25:20 +01:00
const friends = users.filter(
(x) => x.relationship === RelationshipStatus.Friend,
2021-07-05 11:25:20 +01:00
);
const lists = [
[
"",
users.filter((x) => x.relationship === RelationshipStatus.Incoming),
2021-07-05 11:25:20 +01:00
],
[
"app.special.friends.sent",
users.filter((x) => x.relationship === RelationshipStatus.Outgoing),
2021-07-05 11:25:20 +01:00
"outgoing",
],
[
"app.status.online",
friends.filter(
(x) => x.online && x.status?.presence !== Presence.Invisible,
2021-07-05 11:25:20 +01:00
),
"online",
],
[
"app.status.offline",
friends.filter(
(x) => !x.online || x.status?.presence === Presence.Invisible,
2021-07-05 11:25:20 +01:00
),
"offline",
],
[
"app.special.friends.blocked",
users.filter((x) => x.relationship === RelationshipStatus.Blocked),
2021-07-05 11:25:20 +01:00
"blocked",
],
] as [string, User[], string][];
2021-07-05 11:25:20 +01:00
const incoming = lists[0][1];
2021-08-05 14:47:00 +01:00
const userlist: Children[] = incoming.map((x) => (
<b key={x._id}>{x.username}</b>
));
2021-07-05 11:25:20 +01:00
for (let i = incoming.length - 1; i > 0; i--) userlist.splice(i, 0, ", ");
2021-07-05 11:25:20 +01:00
const isEmpty = lists.reduce((p: number, n) => p + n.length, 0) === 0;
return (
<>
<Header placement="primary">
{!isTouchscreenDevice && <UserDetail size={24} />}
<div className={styles.title}>
<Text id="app.navigation.tabs.friends" />
</div>
<div className={styles.actions}>
{/*<Tooltip content={"Create Category"} placement="bottom">
2021-07-03 10:46:35 +02:00
<IconButton onClick={() => openScreen({ id: 'special_input', type: 'create_group' })}>
<ListPlus size={28} />
2021-07-03 10:46:35 +02:00
</IconButton>
</Tooltip>
<div className={styles.divider} />*/}
2021-07-05 11:25:20 +01:00
<Tooltip content={"Create Group"} placement="bottom">
<IconButton
onClick={() =>
openScreen({
id: "special_input",
type: "create_group",
})
}>
<MessageAdd size={24} />
</IconButton>
</Tooltip>
<Tooltip content={"Add Friend"} placement="bottom">
<IconButton
onClick={() =>
openScreen({
id: "special_input",
type: "add_friend",
})
}>
<UserPlus size={27} />
</IconButton>
</Tooltip>
{/*
2021-07-03 10:46:35 +02:00
<div className={styles.divider} />
<Tooltip content={"Friend Activity"} placement="bottom">
<IconButton>
<TennisBall size={24} />
</IconButton>
</Tooltip>
*/}
2021-07-05 11:25:20 +01:00
</div>
</Header>
2021-07-06 19:29:27 +01:00
<div
className={styles.list}
data-empty={isEmpty}
data-mobile={isTouchscreenDevice}>
2021-07-05 11:25:20 +01:00
{isEmpty && (
<>
<img src="https://img.insrt.uk/xexu7/XOPoBUTI47.png/raw" />
<Text id="app.special.friends.nobody" />
</>
)}
2021-07-02 18:00:17 +01:00
2021-07-05 11:25:20 +01:00
{incoming.length > 0 && (
<div
className={styles.pending}
onClick={() =>
openScreen({
id: "pending_requests",
2021-07-29 15:11:21 +01:00
users: incoming,
2021-07-05 11:25:20 +01:00
})
}>
<div className={styles.avatars}>
{incoming.map(
(x, i) =>
i < 3 && (
<UserIcon
target={x}
size={64}
mask={
i <
Math.min(incoming.length - 1, 2)
? "url(#overlap)"
: undefined
}
/>
),
)}
</div>
<div className={styles.details}>
<div>
<Text id="app.special.friends.pending" />{" "}
<span>{incoming.length}</span>
</div>
<span>
{incoming.length > 3 ? (
<TextReact
id="app.special.friends.from.several"
fields={{
userlist: userlist.slice(0, 6),
count: incoming.length - 3,
}}
/>
) : incoming.length > 1 ? (
<TextReact
id="app.special.friends.from.multiple"
fields={{
user: userlist.shift()!,
userlist: userlist.slice(1),
}}
/>
) : (
<TextReact
id="app.special.friends.from.single"
fields={{ user: userlist[0] }}
/>
)}
</span>
</div>
<ChevronRight size={28} />
</div>
)}
2021-07-02 18:00:17 +01:00
2021-07-05 11:25:20 +01:00
{lists.map(([i18n, list, section_id], index) => {
if (index === 0) return;
if (list.length === 0) return;
2021-07-05 11:25:20 +01:00
return (
<CollapsibleSection
2021-08-05 14:47:00 +01:00
key={section_id}
2021-07-05 11:25:20 +01:00
id={`friends_${section_id}`}
defaultValue={true}
sticky
large
summary={
<div class="title">
<Text id={i18n} /> {list.length}
</div>
}>
{list.map((x) => (
<Friend key={x._id} user={x} />
))}
</CollapsibleSection>
);
})}
</div>
</>
);
2021-07-29 15:11:21 +01:00
});