Skip to content

Commit c4e9c4e

Browse files
committed
migrate the ads component to typescript
1 parent 513522b commit c4e9c4e

9 files changed

Lines changed: 135 additions & 80 deletions

File tree

src/components/CarbonAd.js

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query';
3+
import { Ad } from "../types";
4+
import { axios } from 'src/lib/axios';
5+
6+
const getAd = async (): Promise<Ad> => {
7+
const userAgent = new URLSearchParams(navigator.userAgent).toString()
8+
9+
return axios.get('/monetization/', {
10+
params: {
11+
useragent: userAgent
12+
}
13+
}).then(response => {
14+
if (response.data?.ads?.length) {
15+
return response.data.ads[0];
16+
}
17+
});
18+
}
19+
20+
type QueryFnType = typeof getAd;
21+
22+
type UseGetAdOptions = {
23+
config?: QueryConfig<QueryFnType>;
24+
};
25+
export const useGetAd = ({ config }: UseGetAdOptions = {}) => {
26+
return useQuery<ExtractFnReturnType<QueryFnType>>({
27+
...config,
28+
queryKey: ['ad'],
29+
queryFn: () => getAd(),
30+
});
31+
}
File renamed without changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import './CarbonAd.css'
2+
import { addHttpsProtocol } from 'src/utils/UrlUtils'
3+
import { useGetAd } from '../api/getAd'
4+
export default function CarbonAd() {
5+
const { data: ad } = useGetAd()
6+
7+
if (!ad) {
8+
return null
9+
}
10+
11+
return (
12+
<div className="carbon-ad-wrapper blockRow">
13+
{ad && (
14+
<div id="carbonads">
15+
<span>
16+
<span className="carbon-wrap">
17+
<a
18+
href={addHttpsProtocol(ad.statlink)}
19+
className="carbon-img"
20+
target="_blank"
21+
rel="noopener sponsored noreferrer"
22+
title={ad.company + ' ' + ad.companyTagline}>
23+
<img
24+
src={ad.smallImage}
25+
alt={ad.company}
26+
height="100"
27+
width="130"
28+
style={{ background: ad.backgroundColor, border: 0 }}
29+
/>
30+
</a>
31+
32+
<a
33+
href={addHttpsProtocol(ad.statlink)}
34+
className="carbon-text"
35+
target="_blank"
36+
rel="noopener sponsored noreferrer">
37+
{ad.description}
38+
</a>
39+
</span>
40+
41+
<a
42+
href={ad.ad_via_link}
43+
className="carbon-poweredby"
44+
target="_blank"
45+
rel="noopener sponsored noreferrer">
46+
ads via Carbon
47+
</a>
48+
</span>
49+
</div>
50+
)}
51+
</div>
52+
)
53+
}

src/features/carbonAds/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./components/CarbonAd";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Ad = {
2+
statlink: string,
3+
company: string,
4+
companyTagline: string,
5+
smallImage: string,
6+
backgroundColor: string,
7+
description: string,
8+
ad_via_link: string
9+
}

src/lib/axios.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Axios from 'axios';
2+
import { getBaseApi } from 'src/utils/DataUtils'
3+
4+
export const axios = Axios.create({
5+
baseURL: getBaseApi(null)
6+
});

src/lib/react-query.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { AxiosError } from 'axios';
2+
import { QueryClient, UseQueryOptions, UseMutationOptions, DefaultOptions } from '@tanstack/react-query';
3+
import { PromiseValue } from 'type-fest';
4+
5+
const queryConfig: DefaultOptions = {
6+
queries: {
7+
useErrorBoundary: true,
8+
refetchOnWindowFocus: false,
9+
retry: false,
10+
},
11+
};
12+
13+
export const queryClient = new QueryClient({ defaultOptions: queryConfig });
14+
15+
export type ExtractFnReturnType<FnType extends (...args: any) => any> = PromiseValue<
16+
ReturnType<FnType>
17+
>;
18+
19+
export type QueryConfig<QueryFnType extends (...args: any) => any> = Omit<
20+
UseQueryOptions<ExtractFnReturnType<QueryFnType>>,
21+
'queryKey' | 'queryFn'
22+
>;
23+
24+
export type MutationConfig<MutationFnType extends (...args: any) => any> = UseMutationOptions<
25+
ExtractFnReturnType<MutationFnType>,
26+
AxiosError,
27+
Parameters<MutationFnType>[0]
28+
>;

src/utils/UrlUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const addHttpsProtocol = (url: string): string => {
2+
url = decodeURIComponent(url)
3+
if (!/^(?:f|ht)tps?:\/\//.test(url)) {
4+
url = 'https:' + url
5+
}
6+
return url
7+
}

0 commit comments

Comments
 (0)