refactor: improves Performance & Code Readability

- Replaces useEffet with useMemo & useLayoutEffect where necessary.
- Converts some useEffects to separate hooks.
- Moves the functions defined within components to utils.
- Splits the Keywords renderPosition function to its own component.
This commit is contained in:
towfiqi
2024-01-13 10:10:49 +06:00
parent 2783de5c65
commit 4a47cedad8
18 changed files with 146 additions and 153 deletions

View File

@@ -153,3 +153,23 @@ export function useRefreshKeywords(onSuccess:Function) {
},
});
}
export function useFetchSingleKeyword(keywordID:number) {
return useQuery(['keyword', keywordID], async () => {
try {
const fetchURL = `${window.location.origin}/api/keyword?id=${keywordID}`;
const res = await fetch(fetchURL, { method: 'GET' }).then((result) => result.json());
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
return { history: res.keyword.history || [], searchResult: res.keyword.lastResult || [] };
} catch (error) {
throw new Error('Error Loading Keyword Details');
}
}, {
onError: () => {
console.log('Error Loading Keyword Data!!!');
toast('Error Loading Keyword Details.', { icon: '⚠️' });
},
});
}