2022-11-22 20:51:45 -05:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 07:37:55 -05:00
|
|
|
import * as DataStore from "@api/DataStore";
|
|
|
|
import { Devs } from "@utils/constants";
|
|
|
|
import definePlugin from "@utils/types";
|
2023-04-21 21:18:19 -04:00
|
|
|
import { ChannelStore, NavigationRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common";
|
2022-11-22 20:51:45 -05:00
|
|
|
|
|
|
|
export interface LogoutEvent {
|
|
|
|
type: "LOGOUT";
|
|
|
|
isSwitchingAccount: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ChannelSelectEvent {
|
|
|
|
type: "CHANNEL_SELECT";
|
|
|
|
channelId: string | null;
|
|
|
|
guildId: string | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PreviousChannel {
|
|
|
|
guildId: string | null;
|
|
|
|
channelId: string | null;
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
let isSwitchingAccount = false;
|
|
|
|
let previousCache: PreviousChannel | undefined;
|
|
|
|
|
|
|
|
function attemptToNavigateToChannel(guildId: string | null, channelId: string) {
|
|
|
|
if (!ChannelStore.hasChannel(channelId)) return;
|
|
|
|
NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${channelId}`);
|
|
|
|
}
|
2022-11-22 20:51:45 -05:00
|
|
|
|
|
|
|
export default definePlugin({
|
|
|
|
name: "KeepCurrentChannel",
|
2022-12-03 20:16:47 -05:00
|
|
|
description: "Attempt to navigate to the channel you were in before switching accounts or loading Discord.",
|
2022-11-22 20:51:45 -05:00
|
|
|
authors: [Devs.Nuckyz],
|
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
flux: {
|
|
|
|
LOGOUT(e: LogoutEvent) {
|
|
|
|
({ isSwitchingAccount } = e);
|
|
|
|
},
|
2022-11-22 20:51:45 -05:00
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
CONNECTION_OPEN() {
|
|
|
|
if (!isSwitchingAccount) return;
|
|
|
|
isSwitchingAccount = false;
|
2022-11-22 20:51:45 -05:00
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
if (previousCache?.channelId)
|
|
|
|
attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId);
|
|
|
|
},
|
2022-11-22 20:51:45 -05:00
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
async CHANNEL_SELECT({ guildId, channelId }: ChannelSelectEvent) {
|
|
|
|
if (isSwitchingAccount) return;
|
2022-11-22 20:51:45 -05:00
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
previousCache = {
|
|
|
|
guildId,
|
|
|
|
channelId
|
|
|
|
};
|
|
|
|
await DataStore.set("KeepCurrentChannel_previousData", previousCache);
|
|
|
|
}
|
2022-11-22 20:51:45 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
async start() {
|
2023-04-21 21:18:19 -04:00
|
|
|
previousCache = await DataStore.get<PreviousChannel>("KeepCurrentChannel_previousData");
|
|
|
|
if (!previousCache) {
|
|
|
|
previousCache = {
|
2022-11-22 20:51:45 -05:00
|
|
|
guildId: SelectedGuildStore.getGuildId(),
|
|
|
|
channelId: SelectedChannelStore.getChannelId() ?? null
|
|
|
|
};
|
|
|
|
|
2023-04-21 21:18:19 -04:00
|
|
|
await DataStore.set("KeepCurrentChannel_previousData", previousCache);
|
|
|
|
} else if (previousCache.channelId) {
|
|
|
|
attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId);
|
|
|
|
}
|
2022-11-22 20:51:45 -05:00
|
|
|
}
|
|
|
|
});
|