revite/src/pages/Open.tsx

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-07-05 06:23:23 -04:00
import { useHistory, useParams } from "react-router-dom";
import { Text } from "preact-i18n";
import { useContext, useEffect } from "preact/hooks";
2021-07-05 06:23:23 -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";
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-07-05 06:25:20 -04:00
if (status !== ClientStatus.ONLINE) {
return (
<Header placement="primary">
<Text id="general.loading" />
</Header>
);
}
2021-07-05 06:25:20 -04:00
useEffect(() => {
if (id === "saved") {
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;
}
}
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-07-05 06:25:20 -04:00
return;
}
let user = client.users.get(id);
2021-07-05 06:25:20 -04:00
if (user) {
const channel: string | undefined = [
...client.channels.values(),
].find(
(channel) =>
channel?.channel_type === "DirectMessage" &&
channel.recipient_ids!.includes(id),
)?._id;
2021-07-05 06:25:20 -04:00
if (channel) {
history.push(`/channel/${channel}`);
} else {
client.users
.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-07-05 06:25:20 -04:00
return;
}
2021-07-05 06:25:20 -04:00
history.push("/");
}, []);
2021-07-05 06:25:20 -04:00
return (
<Header placement="primary">
<Text id="general.loading" />
</Header>
);
}