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-29 11:55:07 -04:00
|
|
|
for (const channel of client.channels.toArray()) {
|
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-05 06:25:20 -04:00
|
|
|
client.users
|
|
|
|
.openDM(client.user?._id as string)
|
|
|
|
.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-29 11:55:07 -04:00
|
|
|
let user = client.users.get(id);
|
2021-07-05 06:25:20 -04:00
|
|
|
if (user) {
|
2021-07-29 11:55:07 -04:00
|
|
|
const channel: string | undefined = client.channels
|
|
|
|
.toArray()
|
|
|
|
.find(
|
|
|
|
(channel) =>
|
|
|
|
channel?.channel_type === "DirectMessage" &&
|
|
|
|
channel.recipients.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
|
|
|
|
.openDM(id)
|
|
|
|
.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-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
|
|
|
}
|