/* * 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 . */ import { filters, findModuleId, mapMangledModuleLazy, proxyLazyWebpack, wreq } from "@webpack"; import type { ComponentType, PropsWithChildren, ReactNode, Ref } from "react"; import { LazyComponent } from "./react"; export const enum ModalSize { SMALL = "small", MEDIUM = "medium", LARGE = "large", DYNAMIC = "dynamic", } const enum ModalTransitionState { ENTERING, ENTERED, EXITING, EXITED, HIDDEN, } export interface ModalProps { transitionState: ModalTransitionState; onClose(): void; } export interface ModalOptions { modalKey?: string; onCloseRequest?: (() => void); onCloseCallback?: (() => void); } type RenderFunction = (props: ModalProps) => ReactNode | Promise; interface Modals { ModalRoot: ComponentType>; ModalHeader: ComponentType>; /** This also accepts Scroller props but good luck with that */ ModalContent: ComponentType; [prop: string]: any; }>>; ModalFooter: ComponentType>; ModalCloseButton: ComponentType<{ focusProps?: any; onClick(): void; withCircleBackground?: boolean; hideOnFullscreen?: boolean; className?: string; }>; } export const Modals: Modals = mapMangledModuleLazy(':"thin")', { ModalRoot: filters.componentByCode('.MODAL,"aria-labelledby":'), ModalHeader: filters.componentByCode(",id:"), ModalContent: filters.componentByCode(".content,"), ModalFooter: filters.componentByCode(".footer,"), ModalCloseButton: filters.componentByCode(".close]:") }); export const ModalRoot = LazyComponent(() => Modals.ModalRoot); export const ModalHeader = LazyComponent(() => Modals.ModalHeader); export const ModalContent = LazyComponent(() => Modals.ModalContent); export const ModalFooter = LazyComponent(() => Modals.ModalFooter); export const ModalCloseButton = LazyComponent(() => Modals.ModalCloseButton); export type MediaModalItem = { url: string; type: "IMAGE" | "VIDEO"; original?: string; alt?: string; width?: number; height?: number; animated?: boolean; maxWidth?: number; maxHeight?: number; } & Record; export type MediaModalProps = { location?: string; contextKey?: string; onCloseCallback?: () => void; className?: string; items: MediaModalItem[]; startingIndex?: number; onIndexChange?: (...args: any[]) => void; fit?: string; shouldRedactExplicitContent?: boolean; shouldHideMediaOptions?: boolean; shouldAnimateCarousel?: boolean; }; export const openMediaModal: (props: MediaModalProps) => void = proxyLazyWebpack(() => { const mediaModalKeyModuleId = findModuleId('"Zoomed Media Modal"'); if (mediaModalKeyModuleId == null) return; const openMediaModalModule = wreq(findModuleId(mediaModalKeyModuleId, "modalKey:") as any); return Object.values(openMediaModalModule).find(v => String(v).includes("modalKey:")); }); interface ModalAPI { /** * Wait for the render promise to resolve, then open a modal with it. * This is equivalent to render().then(openModal) * You should use the Modal components exported by this file */ openModalLazy: (render: () => Promise, options?: ModalOptions & { contextKey?: string; }) => Promise; /** * Open a Modal with the given render function. * You should use the Modal components exported by this file */ openModal: (render: RenderFunction, options?: ModalOptions, contextKey?: string) => string; /** * Close a modal by its key */ closeModal: (modalKey: string, contextKey?: string) => void; /** * Close all open modals */ closeAllModals: () => void; } export const ModalAPI: ModalAPI = mapMangledModuleLazy(".modalKey?", { openModalLazy: filters.byCode(".modalKey?"), openModal: filters.byCode(",instant:"), closeModal: filters.byCode(".onCloseCallback()"), closeAllModals: filters.byCode(".getState();for") }); export const { openModalLazy, openModal, closeModal, closeAllModals } = ModalAPI;