revite/src/components/navigation/left/common.ts

99 lines
2.9 KiB
TypeScript
Raw Normal View History

import { autorun, isObservableProp, reaction } from "mobx";
2021-07-30 17:40:49 -04:00
import { Channel } from "revolt.js/dist/maps/Channels";
2021-06-19 10:29:04 -04:00
import { useLayoutEffect } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { dispatch } from "../../../redux";
2021-06-19 10:29:04 -04:00
import { Unreads } from "../../../redux/reducers/unreads";
2021-07-05 06:23:23 -04:00
2021-07-29 14:01:40 -04:00
import { useClient } from "../../../context/revoltjs/RevoltClient";
2021-06-19 10:29:04 -04:00
type UnreadProps = {
2021-07-05 06:25:20 -04:00
channel: Channel;
unreads: Unreads;
2021-07-05 06:23:23 -04:00
};
2021-07-29 14:01:40 -04:00
export function useUnreads({ channel, unreads }: UnreadProps) {
const client = useClient();
2021-06-19 10:29:04 -04:00
2021-07-05 06:25:20 -04:00
useLayoutEffect(() => {
function checkUnread(target: Channel) {
2021-07-05 06:25:20 -04:00
if (!target) return;
if (target._id !== channel._id) return;
if (
target.channel_type === "SavedMessages" ||
target.channel_type === "VoiceChannel"
)
return;
2021-06-19 10:29:04 -04:00
2021-07-05 06:25:20 -04:00
const unread = unreads[channel._id]?.last_id;
if (target.last_message) {
const message =
typeof target.last_message === "string"
? target.last_message
: target.last_message._id;
if (!unread || (unread && message.localeCompare(unread) > 0)) {
dispatch({
type: "UNREADS_MARK_READ",
channel: channel._id,
message,
});
2021-06-19 10:29:04 -04:00
2021-07-31 08:48:26 -04:00
channel.ack(message);
2021-07-05 06:25:20 -04:00
}
}
}
2021-06-19 10:29:04 -04:00
checkUnread(channel);
return reaction(
() => channel.last_message,
() => checkUnread(channel),
);
2021-07-05 06:25:20 -04:00
}, [channel, unreads]);
2021-06-19 10:29:04 -04:00
}
export function mapChannelWithUnread(channel: Channel, unreads: Unreads) {
2021-07-05 06:25:20 -04:00
let last_message_id;
if (
channel.channel_type === "DirectMessage" ||
channel.channel_type === "Group"
) {
2021-07-29 13:41:01 -04:00
last_message_id = (channel.last_message as { _id: string })?._id;
2021-07-05 06:25:20 -04:00
} else if (channel.channel_type === "TextChannel") {
2021-07-29 13:41:01 -04:00
last_message_id = channel.last_message as string;
2021-07-05 06:25:20 -04:00
} else {
return {
2021-07-29 13:41:01 -04:00
channel,
2021-07-05 06:25:20 -04:00
unread: undefined,
alertCount: undefined,
timestamp: channel._id,
};
}
2021-06-19 10:29:04 -04:00
2021-07-05 06:25:20 -04:00
let unread: "mention" | "unread" | undefined;
let alertCount: undefined | number;
if (last_message_id && unreads) {
const u = unreads[channel._id];
if (u) {
if (u.mentions && u.mentions.length > 0) {
alertCount = u.mentions.length;
unread = "mention";
} else if (
u.last_id &&
2021-07-29 13:41:01 -04:00
(last_message_id as string).localeCompare(u.last_id) > 0
2021-07-05 06:25:20 -04:00
) {
unread = "unread";
}
} else {
unread = "unread";
}
}
2021-06-19 10:29:04 -04:00
2021-07-05 06:25:20 -04:00
return {
2021-07-29 13:41:01 -04:00
channel,
2021-07-05 06:25:20 -04:00
timestamp: last_message_id ?? channel._id,
unread,
alertCount,
};
2021-06-19 10:29:04 -04:00
}