mirror of
https://github.com/towfiqi/serpbear
synced 2025-06-26 18:15:54 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user