Refactor context reducer to TypeScript (#34506)

This commit is contained in:
Eugen Rochko 2025-04-28 15:38:40 +02:00 committed by GitHub
parent bd9223f0b9
commit 17d8e2b6e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 308 additions and 234 deletions

View file

@ -0,0 +1,94 @@
import { createAppSelector } from 'mastodon/store';
export const getAncestorsIds = createAppSelector(
[(_, id: string) => id, (state) => state.contexts.inReplyTos],
(statusId, inReplyTos) => {
const ancestorsIds: string[] = [];
let currentId: string | undefined = statusId;
while (currentId && !ancestorsIds.includes(currentId)) {
ancestorsIds.unshift(currentId);
currentId = inReplyTos[currentId];
}
return ancestorsIds;
},
);
export const getDescendantsIds = createAppSelector(
[
(_, id: string) => id,
(state) => state.contexts.replies,
(state) => state.statuses,
],
(statusId, contextReplies, statuses) => {
const descendantsIds: string[] = [];
const visitIds = [statusId];
while (visitIds.length > 0) {
const id = visitIds.pop();
if (!id) {
break;
}
const replies = contextReplies[id];
if (statusId !== id) {
descendantsIds.push(id);
}
if (replies) {
replies.reverse().forEach((replyId) => {
if (
!visitIds.includes(replyId) &&
!descendantsIds.includes(replyId) &&
statusId !== replyId
) {
visitIds.push(replyId);
}
});
}
}
let insertAt = descendantsIds.findIndex((id) => {
const status = statuses.get(id);
if (!status) {
return false;
}
const inReplyToAccountId = status.get('in_reply_to_account_id') as
| string
| null;
const accountId = status.get('account') as string;
return inReplyToAccountId !== accountId;
});
if (insertAt !== -1) {
descendantsIds.forEach((id, idx) => {
const status = statuses.get(id);
if (!status) {
return;
}
const inReplyToAccountId = status.get('in_reply_to_account_id') as
| string
| null;
const accountId = status.get('account') as string;
if (idx > insertAt && inReplyToAccountId === accountId) {
descendantsIds.splice(idx, 1);
descendantsIds.splice(insertAt, 0, id);
insertAt += 1;
}
});
}
return descendantsIds;
},
);