AdProvider boilerplate

react-query-api
Alex Gleason 2022-08-01 18:50:46 -05:00
rodzic d5fd3af903
commit f112dd980b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
6 zmienionych plików z 64 dodań i 0 usunięć

Wyświetl plik

@ -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 };

Wyświetl plik

@ -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;

Wyświetl plik

@ -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';

Wyświetl plik

@ -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));
};

Wyświetl plik

@ -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);
}),
);
};

Wyświetl plik

@ -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,