aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/keepCurrentChannel.ts
diff options
context:
space:
mode:
authorV <vendicated@riseup.net>2023-04-22 03:18:19 +0200
committerGitHub <noreply@github.com>2023-04-22 01:18:19 +0000
commit63fc354d483b86857bbee7f540c66ba614fc0f1f (patch)
tree6ebe100e5ff63254adfdb0c44cb473dcc339cf35 /src/plugins/keepCurrentChannel.ts
parentc6f0c84935db37e2a18578a081404d84292fe36a (diff)
downloadVencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.tar.gz
Vencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.tar.bz2
Vencord-63fc354d483b86857bbee7f540c66ba614fc0f1f.zip
feat: auto-managed flux subscriptions via plugin.flux (#959)
Diffstat (limited to 'src/plugins/keepCurrentChannel.ts')
-rw-r--r--src/plugins/keepCurrentChannel.ts75
1 files changed, 33 insertions, 42 deletions
diff --git a/src/plugins/keepCurrentChannel.ts b/src/plugins/keepCurrentChannel.ts
index e553b93..b226c34 100644
--- a/src/plugins/keepCurrentChannel.ts
+++ b/src/plugins/keepCurrentChannel.ts
@@ -19,7 +19,7 @@
import * as DataStore from "@api/DataStore";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
-import { ChannelStore, FluxDispatcher, NavigationRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common";
+import { ChannelStore, NavigationRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common";
export interface LogoutEvent {
type: "LOGOUT";
@@ -37,63 +37,54 @@ interface PreviousChannel {
channelId: string | null;
}
+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}`);
+}
export default definePlugin({
name: "KeepCurrentChannel",
description: "Attempt to navigate to the channel you were in before switching accounts or loading Discord.",
authors: [Devs.Nuckyz],
- isSwitchingAccount: false,
- previousCache: {} as PreviousChannel,
+ flux: {
+ LOGOUT(e: LogoutEvent) {
+ ({ isSwitchingAccount } = e);
+ },
- attemptToNavigateToChannel(guildId: string | null, channelId: string) {
- if (!ChannelStore.hasChannel(channelId)) return;
- NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${channelId}`);
- },
+ CONNECTION_OPEN() {
+ if (!isSwitchingAccount) return;
+ isSwitchingAccount = false;
- onLogout(e: LogoutEvent) {
- this.isSwitchingAccount = e.isSwitchingAccount;
- },
-
- onConnectionOpen() {
- if (!this.isSwitchingAccount) return;
- this.isSwitchingAccount = false;
-
- if (this.previousCache.channelId) this.attemptToNavigateToChannel(this.previousCache.guildId, this.previousCache.channelId);
- },
+ if (previousCache?.channelId)
+ attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId);
+ },
- async onChannelSelect({ guildId, channelId }: ChannelSelectEvent) {
- if (this.isSwitchingAccount) return;
+ async CHANNEL_SELECT({ guildId, channelId }: ChannelSelectEvent) {
+ if (isSwitchingAccount) return;
- this.previousCache = {
- guildId,
- channelId
- };
- await DataStore.set("KeepCurrentChannel_previousData", this.previousCache);
+ previousCache = {
+ guildId,
+ channelId
+ };
+ await DataStore.set("KeepCurrentChannel_previousData", previousCache);
+ }
},
async start() {
- const previousData = await DataStore.get<PreviousChannel>("KeepCurrentChannel_previousData");
- if (previousData) {
- this.previousCache = previousData;
-
- if (this.previousCache.channelId) this.attemptToNavigateToChannel(this.previousCache.guildId, this.previousCache.channelId);
- } else {
- this.previousCache = {
+ previousCache = await DataStore.get<PreviousChannel>("KeepCurrentChannel_previousData");
+ if (!previousCache) {
+ previousCache = {
guildId: SelectedGuildStore.getGuildId(),
channelId: SelectedChannelStore.getChannelId() ?? null
};
- await DataStore.set("KeepCurrentChannel_previousData", this.previousCache);
- }
- FluxDispatcher.subscribe("LOGOUT", this.onLogout.bind(this));
- FluxDispatcher.subscribe("CONNECTION_OPEN", this.onConnectionOpen.bind(this));
- FluxDispatcher.subscribe("CHANNEL_SELECT", this.onChannelSelect.bind(this));
- },
-
- stop() {
- FluxDispatcher.unsubscribe("LOGOUT", this.onLogout);
- FluxDispatcher.unsubscribe("CONNECTION_OPEN", this.onConnectionOpen);
- FluxDispatcher.unsubscribe("CHANNEL_SELECT", this.onChannelSelect);
+ await DataStore.set("KeepCurrentChannel_previousData", previousCache);
+ } else if (previousCache.channelId) {
+ attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId);
+ }
}
});