2021-08-05 09:47:00 -04:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2021-07-05 06:23:23 -04:00
|
|
|
import { useHistory, useParams } from "react-router-dom";
|
|
|
|
|
2021-06-21 16:35:21 -04:00
|
|
|
import { Text } from "preact-i18n";
|
|
|
|
import { useContext, useEffect } from "preact/hooks";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-21 16:35:21 -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 Header from "../components/ui/Header";
|
2021-06-21 16:35:21 -04:00
|
|
|
|
|
|
|
export default function Open() {
|
2021-07-05 06:25:20 -04:00
|
|
|
const history = useHistory();
|
|
|
|
const client = useContext(AppContext);
|
|
|
|
const status = useContext(StatusContext);
|
|
|
|
const { id } = useParams<{ id: string }>();
|
|
|
|
const { openScreen } = useIntermediate();
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
if (status !== ClientStatus.ONLINE) {
|
|
|
|
return (
|
|
|
|
<Header placement="primary">
|
|
|
|
<Text id="general.loading" />
|
|
|
|
</Header>
|
|
|
|
);
|
|
|
|
}
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (id === "saved") {
|
2021-07-30 16:20:42 -04:00
|
|
|
for (const channel of [...client.channels.values()]) {
|
2021-07-05 06:25:20 -04:00
|
|
|
if (channel?.channel_type === "SavedMessages") {
|
|
|
|
history.push(`/channel/${channel._id}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-30 16:20:42 -04:00
|
|
|
client
|
|
|
|
.user!.openDM()
|
2021-07-05 06:25:20 -04:00
|
|
|
.then((channel) => history.push(`/channel/${channel?._id}`))
|
|
|
|
.catch((error) => openScreen({ id: "error", error }));
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-08-05 09:47:00 -04:00
|
|
|
const user = client.users.get(id);
|
2021-07-05 06:25:20 -04:00
|
|
|
if (user) {
|
2021-07-30 16:20:42 -04:00
|
|
|
const channel: string | undefined = [
|
|
|
|
...client.channels.values(),
|
|
|
|
].find(
|
|
|
|
(channel) =>
|
|
|
|
channel?.channel_type === "DirectMessage" &&
|
|
|
|
channel.recipient_ids!.includes(id),
|
|
|
|
)?._id;
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
if (channel) {
|
|
|
|
history.push(`/channel/${channel}`);
|
|
|
|
} else {
|
|
|
|
client.users
|
2021-07-30 16:20:42 -04:00
|
|
|
.get(id)
|
|
|
|
?.openDM()
|
2021-07-05 06:25:20 -04:00
|
|
|
.then((channel) => history.push(`/channel/${channel?._id}`))
|
|
|
|
.catch((error) => openScreen({ id: "error", error }));
|
|
|
|
}
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
history.push("/");
|
2021-08-05 09:47:00 -04:00
|
|
|
});
|
2021-06-21 16:35:21 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<Header placement="primary">
|
|
|
|
<Text id="general.loading" />
|
|
|
|
</Header>
|
|
|
|
);
|
2021-06-21 16:35:21 -04:00
|
|
|
}
|