summaryrefslogtreecommitdiff
path: root/src/jet/utils/handle-modal-presentation.ts
blob: 9040d4fc189a133d9b697c9511b3195681b2d059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { getModalPageStore } from '~/stores/modalPage';
import { isGenericPage, type Page } from '../models';
import type { Logger } from '@amp/web-apps-logger/src';

/**
 * This function handles rendering flow action pages into a modal container.
 * NOTE: Rendering a page in a modal will not update URL or history
 *
 * @param page page promise
 * @param log app logger
 */
export const handleModalPresentation = (
    page: { promise: Promise<Page> },
    log: Logger<unknown[]>,
    pageDetail?: string,
) => {
    page.promise
        .then((page) => {
            if (isGenericPage(page)) {
                const modalStore = getModalPageStore();
                modalStore.setPage({ page, pageDetail });
            } else {
                throw new Error('only generic page is rendered in modal');
            }
        })
        .catch((e) => {
            log.error('modal presentation failed', e);
        });
};