summaryrefslogtreecommitdiff
path: root/src/utils/seo/developer-page.ts
blob: 914dd08ede70afb7c86dadfde54c8caa6b849688 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
    type Opt,
    unwrapOptional as unwrap,
} from '@jet/environment/types/optional';
import type { Organization, WithContext } from 'schema-dts';

import type { AppStoreObjectGraph } from '@jet-app/app-store/foundation/runtime/app-store-object-graph';
import {
    type Data,
    type DataContainer,
    dataFromDataContainer,
} from '@jet-app/app-store/foundation/media/data-structure';
import { attributeAsString } from '@jet-app/app-store/foundation/media/attributes';
import { relationshipCollection } from '@jet-app/app-store/foundation/media/relationships';

import type I18N from '@amp/web-apps-localization';
import type { SeoData } from '@amp/web-app-components/src/components/MetaTags/types';

import { uniqueById } from '~/utils/array';
import { basicSoftwareApplicationSchema } from '~/utils/seo/product-page';

/**
 * Generate a basic {@linkcode Person} schema for a "developer" page
 *
 * Note: this is appropriate to be embedded into another schema that
 * needs to reference the developer
 */
export function basicDeveloperSchema(data: Data) {
    return {
        '@type': 'Organization',
        name: attributeAsString(data, 'name') ?? undefined,
        url: attributeAsString(data, 'url') ?? undefined,
    } satisfies Organization;
}

export function buildDeveloperDescription(
    props: {
        name: string;
    },
    appData: Data[],
    i18n: I18N,
) {
    const { name: developerName } = props;

    switch (appData.length) {
        case 0:
            return i18n.t(
                'ASE.Web.AppStore.Meta.Developer.Description.ZeroApps',
                {
                    developerName,
                },
            );
        case 1:
            return i18n.t(
                'ASE.Web.AppStore.Meta.Developer.Description.OneApp',
                {
                    developerName,
                    listing1: attributeAsString(appData[0], 'name'),
                },
            );
        case 2:
            return i18n.t(
                'ASE.Web.AppStore.Meta.Developer.Description.TwoApps',
                {
                    developerName,
                    listing1: attributeAsString(appData[0], 'name'),
                    listing2: attributeAsString(appData[1], 'name'),
                },
            );
        case 3:
            return i18n.t(
                'ASE.Web.AppStore.Meta.Developer.Description.ThreeApps',
                {
                    developerName,
                    listing1: attributeAsString(appData[0], 'name'),
                    listing2: attributeAsString(appData[1], 'name'),
                    listing3: attributeAsString(appData[2], 'name'),
                },
            );
        default:
            return i18n.t(
                'ASE.Web.AppStore.Meta.Developer.Description.ManyApps',
                {
                    developerName,
                    listing1: attributeAsString(appData[0], 'name'),
                    listing2: attributeAsString(appData[1], 'name'),
                    listing3: attributeAsString(appData[2], 'name'),
                },
            );
    }
}

/**
 * Builds the Schema.org meta-data for a "Developer" page
 *
 * @param objectGraph The Object Graph
 * @param developerPageData The `Data` for the Developer page
 * @param appData The `Data` for all apps related to the Developer apge
 * @param props Pre-formatted properties also used outside of the Schema
 * @returns
 */
function developerOrganizationSchemaSeoData(
    objectGraph: AppStoreObjectGraph,
    developerPageData: Data,
    appData: Data[],
    props: {
        description: string;
    },
): Opt<Partial<SeoData>> {
    const { description } = props;

    const schemaContent: WithContext<Organization> = {
        '@context': 'https://schema.org',

        ...basicDeveloperSchema(developerPageData),

        description,
        hasOfferCatalog: {
            '@type': 'OfferCatalog',
            itemListElement: appData.map((app) =>
                basicSoftwareApplicationSchema(objectGraph, app),
            ),
        },
    };

    return {
        schemaName: 'developer',
        schemaContent,
    };
}

/**
 * Builds the full `SeoData` requirements for a "Developer" page
 */
export function seoDataForDeveloperPage(
    objectGraph: AppStoreObjectGraph,
    container: Opt<DataContainer>,
    i18n: I18N,
): Partial<SeoData> {
    if (!container) {
        return {};
    }

    const developerPageData = dataFromDataContainer(objectGraph, container);
    if (!developerPageData) {
        return {};
    }

    const allApps = uniqueById([
        ...unwrap(relationshipCollection(developerPageData, 'atv-apps')),
        ...unwrap(relationshipCollection(developerPageData, 'app-bundles')),
        ...unwrap(relationshipCollection(developerPageData, 'imessage-apps')),
        ...unwrap(relationshipCollection(developerPageData, 'ios-apps')),
        ...unwrap(relationshipCollection(developerPageData, 'mac-apps')),
        ...unwrap(relationshipCollection(developerPageData, 'watch-apps')),
    ]);

    const name = unwrap(attributeAsString(developerPageData, 'name'));
    const description = buildDeveloperDescription({ name }, allApps, i18n);

    return {
        description,
        socialDescription: description,
        appleDescription: description,
        ...developerOrganizationSchemaSeoData(
            objectGraph,
            developerPageData,
            allApps,
            {
                description,
            },
        ),
    };
}