Files
discord-decomp/stores/SelectedGuildStore.tsx
T
2026-11-01 12:12:55 +02:00

186 lines
5.4 KiB
TypeScript

// Metro module 4006 (f4007).
import UserGuildJoinRequestStore from "../modules/guild_member_verification/UserGuildJoinRequestStore";
import AuthenticationStore from "./AuthenticationStore";
import DefaultRouteStore from "./DefaultRouteStore";
import GuildStore from "./GuildStore";
import { ME, Routes } from "../Constants";
import { replaceWith } from "../modules/routing/router_utils";
import Module589 from "../_bundle/Module589";
import { matchPath } from "../modules/routing/matchPathCompat";
import { RouteParam } from "../modules/routing/RouteUtils";
import Dispatcher from "../Dispatcher";
let selectedGuildId, guildId, selectedGuildTimestampMillis, $mk73416 = function() {
return function pickFallbackGuildId(a0) {
if (guildId != null && GuildStore.getGuild(guildId) != null) {
return guildId;
}
let r28 = null;
let r29 = 0;
for (const item of Object.keys(selectedGuildTimestampMillis)) {
if (item !== a0) {
const r43 = selectedGuildTimestampMillis[item];
if (r43 > r29 && GuildStore.getGuild(item) != null) {
r28 = item;
r29 = r43;
}
}
}
if (r28 != null) {
return r28;
}
let r58 = GuildStore.getGuildsArray().find((item) => item.id !== a0)?.id;
if (r58 == null) {
r58 = null;
}
return r58;
};
};
const handleConnectionOpen = function handleConnectionOpen() {
if (selectedGuildId != null && GuildStore.getGuild(selectedGuildId) == null && UserGuildJoinRequestStore.getRequest(selectedGuildId) == null) {
selectedGuildId = null;
}
if (guildId != null && GuildStore.getGuild(guildId) == null && UserGuildJoinRequestStore.getRequest(guildId) == null) {
guildId = null;
}
if (selectedGuildId != null) {
selectedGuildTimestampMillis[selectedGuildId] = Date.now();
}
};
selectedGuildId = null;
guildId = null;
selectedGuildTimestampMillis = {};
class SelectedGuildStore extends Module589.PersistedStore {
static displayName = "SelectedGuildStore";
static persistKey = "SelectedGuildStore";
initialize(a0) {
this.mustEmitChanges((a0) => a0.type !== "CONNECTION_OPEN");
this.waitFor(AuthenticationStore, DefaultRouteStore, GuildStore, UserGuildJoinRequestStore);
let r20 = a0?.selectedGuildTimestampMillis;
if (r20 == null) {
r20 = {};
}
selectedGuildTimestampMillis = r20;
let r22 = a0?.selectedGuildId;
if (r22 == null) {
r22 = null;
}
selectedGuildId = r22;
let r24 = a0?.lastSelectedGuildId;
if (r24 == null) {
r24 = null;
}
guildId = r24;
const r37 = matchPath(DefaultRouteStore.lastNonVoiceRoute, {
path: Routes.CHANNEL(RouteParam.guildId())
});
let r39 = r37 == null ? void 0 : r37.params?.guildId;
let r43 = null;
if (r39 !== ME) {
if (r39 == null) {
r39 = null;
}
r43 = r39;
}
if (r43 != null && r43 !== selectedGuildId) {
selectedGuildId = r43;
}
}
getState() {
return {
selectedGuildTimestampMillis,
selectedGuildId,
lastSelectedGuildId: guildId
};
}
getGuildId() {
return selectedGuildId;
}
getLastSelectedGuildId() {
return guildId;
}
getLastSelectedTimestamp(a0) {
return selectedGuildId !== a0 ? selectedGuildTimestampMillis[a0] : -1;
}
}
const selectedGuildStore = new SelectedGuildStore(Dispatcher, {
CONNECTION_OPEN: handleConnectionOpen,
OVERLAY_INITIALIZE: function handleOverlayInitialize(a0) {
selectedGuildId = a0.selectedGuildId;
guildId = void 0;
handleConnectionOpen();
},
CHANNEL_SELECT: function handleChannelSelect({ guildId: guildId2 }) {
if (selectedGuildId === guildId2) {
return false;
}
if (selectedGuildId != null) {
selectedGuildTimestampMillis[selectedGuildId] = Date.now();
}
if (guildId2 != null) {
selectedGuildTimestampMillis[guildId2] = Date.now();
}
if (guildId2 != null) {
guildId = guildId2;
}
selectedGuildId = guildId2;
},
GUILD_MEMBER_REMOVE: function handleGuildMemberRemove(a0) {
const guildId2 = a0.guildId;
let r2 = a0.user.id === AuthenticationStore.getId();
if (r2) {
delete selectedGuildTimestampMillis[guildId2];
let r14 = false;
if (guildId === guildId2) {
guildId = null;
r14 = true;
}
if (selectedGuildId === guildId2) {
const r17 = $mk73416()(guildId2);
if (r17 != null) {
selectedGuildId = r17;
replaceWith(Routes.CHANNEL(r17));
r14 = true;
} else {
selectedGuildId = null;
replaceWith(Routes.ME);
r14 = true;
}
}
r2 = r14;
}
return r2;
},
GUILD_DELETE: function handleGuildDelete({ guild }) {
const id = guild.id;
let r2 = guild.unavailable !== true;
if (r2) {
delete selectedGuildTimestampMillis[id];
let r14 = false;
if (guildId === id) {
guildId = null;
r14 = true;
}
if (selectedGuildId === id) {
const r9 = $mk73416()(id);
if (r9 != null) {
selectedGuildId = r9;
replaceWith(Routes.CHANNEL(r9));
r14 = true;
} else {
selectedGuildId = null;
replaceWith(Routes.ME);
r14 = true;
}
}
r2 = r14;
}
return r2;
},
LOGOUT: function handleLogout() {
selectedGuildId = null;
guildId = null;
}
});
export default selectedGuildStore;
export const SELECTED_GUILD_TIMESTAMP_NOW = -1;