summaryrefslogtreecommitdiff
path: root/src/jet/intents/error-page-intent-controller.ts
blob: 59ac1fd985bfac6dafe53d2b61e3e8f9a4438ef1 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Intent, IntentController } from '@jet/environment/dispatching';
import type { Opt } from '@jet/environment/types/optional';
import type { AppStoreObjectGraph } from '@jet-app/app-store/foundation/runtime/app-store-object-graph';
import { withActiveIntent } from '@jet-app/app-store/foundation/dependencies/active-intent';
import { injectWebNavigation } from '@jet-app/app-store/common/web-navigation/inject-web-navigation';

import { ErrorPage } from '~/jet/models/error-page';
import type { Page } from '~/jet/models/page';
import { getRejectedIntent } from '~/jet/utils/error-metadata';
import { isWithPlatform } from '~/jet/utils/with-platform';

interface ErrorPageIntent extends Intent<Page> {
    $kind: 'ErrorPageIntent';
    error: Opt<Error>;
}

export function makeErrorPageIntent(
    options: Omit<ErrorPageIntent, '$kind'>,
): ErrorPageIntent {
    return {
        ...options,
        $kind: 'ErrorPageIntent',
    };
}

export const ErrorPageIntentController: IntentController<ErrorPageIntent> = {
    $intentKind: 'ErrorPageIntent',

    async perform(
        intent,
        objectGraphWithoutActiveIntent: AppStoreObjectGraph,
    ): Promise<Page> {
        const { error } = intent;
        const rejectedIntent = error ? getRejectedIntent(error) : null;
        const platform =
            (rejectedIntent && isWithPlatform(rejectedIntent)
                ? rejectedIntent.platform
                : null) ?? 'iphone';

        return await withActiveIntent(
            objectGraphWithoutActiveIntent,
            { ...intent, platform },
            async (objectGraph) => {
                const page = new ErrorPage({ error: intent.error });

                injectWebNavigation(objectGraph, page, platform);

                return page;
            },
        );
    },
};