summaryrefslogtreecommitdiff
path: root/src/jet/intents/route-url/route-url-controller.ts
blob: 8c8fdb627c9f2e018f40b81942a003e3528a7cfb (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
import { isSome } from '@jet/environment/types/optional';
import type { IntentController } from '@jet/environment/dispatching';
import type { AppStoreObjectGraph } from '@jet-app/app-store/foundation/runtime/app-store-object-graph';
import { isRoutableIntent } from '@jet-app/app-store/api/intents/routable-intent';

import type { RouteUrlIntent } from '~/jet/intents';
import { makeFlowAction } from '~/jet/models';

export const RouteUrlIntentController: IntentController<RouteUrlIntent> = {
    $intentKind: 'RouteUrlIntent',

    async perform(intent: RouteUrlIntent, objectGraph: AppStoreObjectGraph) {
        const targetIntent = objectGraph.router.intentFor(intent.url);

        if (isSome(targetIntent) && isRoutableIntent(targetIntent)) {
            return {
                // intent needed for SSR
                intent: targetIntent,
                // only ever used by client; only clients have actions
                action: makeFlowAction(targetIntent),
                storefront: targetIntent.storefront,
                language: targetIntent.language,
            };
        }

        return null;
    },
};