AdProvider boilerplate
This commit is contained in:
parent
d5fd3af903
commit
f112dd980b
6 changed files with 64 additions and 0 deletions
17
app/soapbox/features/ads/providers/index.ts
Normal file
17
app/soapbox/features/ads/providers/index.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import type { RootState } from 'soapbox/store';
|
||||
import type { Card } from 'soapbox/types/entities';
|
||||
|
||||
/** Ad server implementation. */
|
||||
interface AdProvider {
|
||||
getAds(getState: () => RootState): Promise<Ad[]>,
|
||||
}
|
||||
|
||||
/** Entity representing an advertisement. */
|
||||
interface Ad {
|
||||
/** Ad data in Card (OEmbed-ish) format. */
|
||||
card: Card,
|
||||
/** Impression URL to fetch when displaying the ad. */
|
||||
impression?: string,
|
||||
}
|
||||
|
||||
export type { Ad, AdProvider };
|
14
app/soapbox/features/ads/providers/soapbox-config.ts
Normal file
14
app/soapbox/features/ads/providers/soapbox-config.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
|
||||
|
||||
import type { AdProvider } from '.';
|
||||
|
||||
/** Provides ads from Soapbox Config. */
|
||||
const SoapboxConfigAdProvider: AdProvider = {
|
||||
getAds: async(getState) => {
|
||||
const state = getState();
|
||||
const soapboxConfig = getSoapboxConfig(state);
|
||||
return soapboxConfig.ads.toArray();
|
||||
},
|
||||
};
|
||||
|
||||
export default SoapboxConfigAdProvider;
|
|
@ -20,4 +20,5 @@ export { StatusRecord, normalizeStatus } from './status';
|
|||
export { StatusEditRecord, normalizeStatusEdit } from './status_edit';
|
||||
export { TagRecord, normalizeTag } from './tag';
|
||||
|
||||
export { AdRecord, normalizeAd } from './soapbox/ad';
|
||||
export { SoapboxConfigRecord, normalizeSoapboxConfig } from './soapbox/soapbox_config';
|
||||
|
|
19
app/soapbox/normalizers/soapbox/ad.ts
Normal file
19
app/soapbox/normalizers/soapbox/ad.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import {
|
||||
Map as ImmutableMap,
|
||||
Record as ImmutableRecord,
|
||||
fromJS,
|
||||
} from 'immutable';
|
||||
|
||||
import { CardRecord, normalizeCard } from '../card';
|
||||
|
||||
export const AdRecord = ImmutableRecord({
|
||||
card: CardRecord(),
|
||||
impression: undefined as string | undefined,
|
||||
});
|
||||
|
||||
/** Normalizes an ad from Soapbox Config. */
|
||||
export const normalizeAd = (ad: Record<string, any>) => {
|
||||
const map = ImmutableMap<string, any>(fromJS(ad));
|
||||
const card = normalizeCard(map.get('card'));
|
||||
return AdRecord(map.set('card', card));
|
||||
};
|
|
@ -9,7 +9,10 @@ import trimStart from 'lodash/trimStart';
|
|||
import { toTailwind } from 'soapbox/utils/tailwind';
|
||||
import { generateAccent } from 'soapbox/utils/theme';
|
||||
|
||||
import { normalizeAd } from './ad';
|
||||
|
||||
import type {
|
||||
Ad,
|
||||
PromoPanelItem,
|
||||
FooterItem,
|
||||
CryptoAddress,
|
||||
|
@ -78,6 +81,7 @@ export const CryptoAddressRecord = ImmutableRecord({
|
|||
});
|
||||
|
||||
export const SoapboxConfigRecord = ImmutableRecord({
|
||||
ads: ImmutableList<Ad>(),
|
||||
appleAppId: null,
|
||||
logo: '',
|
||||
logoDarkMode: null,
|
||||
|
@ -122,6 +126,11 @@ export const SoapboxConfigRecord = ImmutableRecord({
|
|||
|
||||
type SoapboxConfigMap = ImmutableMap<string, any>;
|
||||
|
||||
const normalizeAds = (soapboxConfig: SoapboxConfigMap): SoapboxConfigMap => {
|
||||
const ads = ImmutableList<Record<string, any>>(soapboxConfig.get('ads'));
|
||||
return soapboxConfig.set('ads', ads.map(normalizeAd));
|
||||
};
|
||||
|
||||
const normalizeCryptoAddress = (address: unknown): CryptoAddress => {
|
||||
return CryptoAddressRecord(ImmutableMap(fromJS(address))).update('ticker', ticker => {
|
||||
return trimStart(ticker, '$').toLowerCase();
|
||||
|
@ -186,6 +195,7 @@ export const normalizeSoapboxConfig = (soapboxConfig: Record<string, any>) => {
|
|||
normalizeFooterLinks(soapboxConfig);
|
||||
maybeAddMissingColors(soapboxConfig);
|
||||
normalizeCryptoAddresses(soapboxConfig);
|
||||
normalizeAds(soapboxConfig);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { AdRecord } from 'soapbox/normalizers/soapbox/ad';
|
||||
import {
|
||||
PromoPanelItemRecord,
|
||||
FooterItemRecord,
|
||||
|
@ -7,6 +8,7 @@ import {
|
|||
|
||||
type Me = string | null | false | undefined;
|
||||
|
||||
type Ad = ReturnType<typeof AdRecord>;
|
||||
type PromoPanelItem = ReturnType<typeof PromoPanelItemRecord>;
|
||||
type FooterItem = ReturnType<typeof FooterItemRecord>;
|
||||
type CryptoAddress = ReturnType<typeof CryptoAddressRecord>;
|
||||
|
@ -14,6 +16,7 @@ type SoapboxConfig = ReturnType<typeof SoapboxConfigRecord>;
|
|||
|
||||
export {
|
||||
Me,
|
||||
Ad,
|
||||
PromoPanelItem,
|
||||
FooterItem,
|
||||
CryptoAddress,
|
||||
|
|
Loading…
Reference in a new issue