1) wrap both theKeywordsCount and theKeywordsGrouped with useMemo

2) changing reduce and map together to make the code more readable.
3) ctr and position value rounded.
This commit is contained in:
Sacha Trauwaen
2024-03-28 20:41:50 +01:00
parent 3a05703921
commit 40e027e1ec

View File

@@ -30,41 +30,49 @@ const DiscoverPage: NextPage = () => {
const { data: keywordsData, isLoading: keywordsLoading, isFetching } = useFetchSCKeywords(router, !!(domainsData?.domains?.length) && scConnected);
const theDomains: DomainType[] = (domainsData && domainsData.domains) || [];
const theKeywords: SearchAnalyticsItem[] = keywordsData?.data && keywordsData.data[scDateFilter] ? keywordsData.data[scDateFilter] : [];
const theKeywords: SearchAnalyticsItem[] = useMemo(() => {
return keywordsData?.data && keywordsData.data[scDateFilter] ? keywordsData.data[scDateFilter] : [];
}, [keywordsData, scDateFilter]);
const theKeywordsCount = theKeywords.reduce<Map<string, number>>((r, o) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const item = r.get(key) || 0;
return r.set(key, item + 1);
}, new Map()) || [];
const theKeywordsCount = useMemo(() => {
return theKeywords.reduce<Map<string, number>>((r, o) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const item = r.get(key) || 0;
return r.set(key, item + 1);
}, new Map()) || [];
}, [theKeywords]);
const theKeywordsGrouped : SearchAnalyticsItem[] = [...theKeywords.reduce<Map<string, SearchAnalyticsItem>>((r, o) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const theKeywordsReduced : SearchAnalyticsItem[] = useMemo(() => {
return [...theKeywords.reduce<Map<string, SearchAnalyticsItem>>((r, o) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const item = r.get(key) || { ...o,
...{
clicks: 0,
impressions: 0,
ctr: 0,
position: 0,
},
};
item.clicks += o.clicks;
item.impressions += o.impressions;
item.ctr = o.ctr + item.ctr;
item.position = o.position + item.position;
return r.set(key, item);
}, new Map()).values()];
}, [theKeywords]);
const item = r.get(key) || { ...o,
...{
clicks: 0,
impressions: 0,
ctr: 0,
position: 0,
},
};
item.clicks += o.clicks;
item.impressions += o.impressions;
item.ctr = o.ctr + item.ctr;
item.position = o.position + item.position;
return r.set(key, item);
}, new Map()).values()].map<SearchAnalyticsItem>((o: SearchAnalyticsItem) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const count = theKeywordsCount?.get(key) || 0;
return { ...o,
...{
ctr: o.ctr / count,
position: o.position / count,
},
};
});
const theKeywordsGrouped : SearchAnalyticsItem[] = useMemo(() => {
return [...theKeywordsReduced.map<SearchAnalyticsItem>((o: SearchAnalyticsItem) => {
const key = `${o.device}-${o.country}-${o.keyword}`;
const count = theKeywordsCount?.get(key) || 0;
return { ...o,
...{
ctr: Math.round((o.ctr / count) * 100) / 100,
position: Math.round(o.position / count),
},
};
})];
}, [theKeywordsReduced, theKeywordsCount]);
const activDomain: DomainType|null = useMemo(() => {
let active:DomainType|null = null;