ChannelDeck/ChannelDeckStore.tsx

66 lines
2.1 KiB
TypeScript

import { proxyLazyWebpack, findByProps, findByPropsLazy } from "@webpack";
import { Flux, FluxDispatcher, PopoutActions, PopoutWindowStore, SnowflakeUtils } from "@webpack/common";
import DeckPopout from "./components/DeckPopout";
export interface ChannelDeck {
id: string;
name: string;
color?: number;
columns: DeckColumn[];
open: boolean;
}
export interface DeckColumn {
channelId: string,
width: `${number}px` | `${number}%`;
}
// Don't wanna run before Flux and Dispatcher are ready!
export const ChannelDeckStore = proxyLazyWebpack(() => {
class ChannelDeckStore extends Flux.Store {
public _decks = new Map<string, ChannelDeck>();
public windowKeyPrefix = "DISCORD_CHANNELDECK_DECK_";
public createDeck(deckState: Partial<Omit<ChannelDeck, "id">>) {
const deck = {
id: SnowflakeUtils.fromTimestamp(Date.now()),
name: "",
columns: [],
open: false,
...deckState
};
this._decks.set(deck.id, deck);
this.updateDeck(deck.id);
return deck;
}
public getDeck(id: string) {
return this._decks.get(id);
}
public updateDeck(id: string) {
const deck = this.getDeck(id);
if (deck?.open && !PopoutWindowStore.getWindowKeys().includes(this.windowKeyPrefix + id))
PopoutActions.open(
this.windowKeyPrefix + id,
(key) => <DeckPopout deckId={id} windowKey={key} />, {
defaultWidth: 1200,
defaultHeight: 960
});
if (!deck?.open && PopoutWindowStore.getWindowKeys().includes(this.windowKeyPrefix + id))
this.getDeckWindow(id).close();
this.emitChange();
};
public getDeckWindow(id: string) {
return PopoutWindowStore.getWindow(this.windowKeyPrefix + id);
}
}
const store = new ChannelDeckStore(FluxDispatcher, {
});
return store;
});;