Refactored common hooks to reduce api calls

This commit is contained in:
Sean Edmunds
2025-01-30 16:09:09 -06:00
parent aa1e9fd6e5
commit 2c3870f288
27 changed files with 604 additions and 421 deletions

View File

@@ -1,67 +1,10 @@
import { useLocalStorage } from '@mantine/hooks';
import { appTheme } from 'contexts';
import { useAtom } from 'jotai';
import { useEffect, useState } from 'react';
import { useOSMAPI, useQueryAPI, useWeatherAPI } from './useAPI';
import { useState } from 'react';
export function useCommonHooks() {
const isMobile = window.matchMedia('(max-width: 600px)').matches;
const {
data: apps,
isFetching: isAppsFetching,
isLoading: isAppsLoading,
} = useQueryAPI({ endpoint: 'api/apps' });
const { data: allThemes, isFetched: isAllThemesFetched } = useQueryAPI({
endpoint: 'api/themes',
});
const { data: backgrounds, isFetched: isBackgroundFetched } = useQueryAPI({
endpoint: 'api/backgrounds',
});
const {
data: messages,
isFetching: isMessagesFetching,
isFetched: isMessagesFetched,
isLoading: isMessagesLoading,
} = useQueryAPI({
endpoint: 'api/messages',
});
const {
data: linkData,
isFetched: linksIsFetched,
isFetching: linksIsFetching,
isLoading: linksIsLoading,
} = useQueryAPI({
endpoint: 'api/links',
});
const {
data: groupData,
isFetched: groupsIsFetched,
isFetching: groupsIsFetching,
isLoading: groupsIsLoading,
} = useQueryAPI({
endpoint: 'api/categories',
});
const {
data: personal,
isFetched: personalIsFetched,
isFetching: personalIsFetching,
isLoading: personalIsLoading,
} = useQueryAPI({
endpoint: 'api/personalData',
});
const {
data: providers,
isFetched: providersIsFetched,
isFetching: providersIsFetching,
isLoading: providersIsLoading,
} = useQueryAPI({ endpoint: 'api/providers' });
const {
data: wolClients,
isFetched: wolClientsIsFetched,
isFetching: wolClientsIsFetching,
isLoading: wolClientsIsLoading,
} = useQueryAPI({ endpoint: 'api/wolClients' });
const [theme, setTheme] = useAtom(appTheme);
const [themeName, setThemeName] = useLocalStorage<any>({
key: 'theme',
@@ -96,114 +39,10 @@ export function useCommonHooks() {
}
};
const [applicationData, setApplicationData] = useState<any>([]);
useEffect(() => {
if (apps && !isAppsFetching) {
setApplicationData(apps);
}
}, [apps, isAppsFetching]);
const [bookmarkData, setBookmarkData] = useState<any>([]);
const [groups, setGroups] = useState([]);
useEffect(() => {
if (linkData && linksIsFetched) {
setBookmarkData(linkData);
}
}, [linksIsFetched, linkData]);
useEffect(() => {
if (groupData && groupsIsFetched) {
const groupList = groupData?.map((category: any) => {
return category.categoryName;
});
setGroups(groupList);
}
}, [groupData, groupsIsFetched]);
const [messageData, setMessageData] = useState<any>([]);
useEffect(() => {
if (messages && isMessagesFetched) {
setMessageData(messages);
}
}, [messages, isMessagesFetched]);
const [personalData, setPersonalData] = useState<any>([]);
useEffect(() => {
if (personal && personalIsFetched) {
setPersonalData(personal);
}
}, [personal, personalIsFetched]);
const [providerData, setProviderData] = useState<any>([]);
useEffect(() => {
if (providers && providersIsFetched) {
setProviderData(providers);
}
}, [providers, providersIsFetched]);
const [wolClientsData, setWolClientsData] = useState<any>([]);
useEffect(() => {
if (wolClients && wolClientsIsFetched) {
setWolClientsData(wolClients);
}
}, [wolClients, wolClientsIsFetched]);
const { data: weatherMetadata } = useOSMAPI({ address: personal?.[0].physicalAddress });
const { data: points } = useWeatherAPI({
endpoint: 'points',
lat: weatherMetadata?.[0].lat || 0,
lon: weatherMetadata?.[0].lon || 0,
});
const { data: basicWeather } = useWeatherAPI({
endpoint: 'baseline',
office: points?.properties.gridId,
x: points?.properties.gridX,
y: points?.properties.gridY,
});
const { data: stations } = useWeatherAPI({
endpoint: 'stations',
office: points?.properties.gridId,
x: points?.properties.gridX,
y: points?.properties.gridY,
});
const { data: weather } = useWeatherAPI({
endpoint: 'current',
station: stations?.features[0].properties.stationIdentifier,
});
const { data: forecast } = useWeatherAPI({
endpoint: 'forecast',
office: points?.properties.gridId,
x: points?.properties.gridX,
y: points?.properties.gridY,
});
const { data: hourly } = useWeatherAPI({
endpoint: 'hourly',
office: points?.properties.gridId,
x: points?.properties.gridX,
y: points?.properties.gridY,
});
return {
isMobile,
theme,
setTheme,
allThemes,
backgrounds,
isBackgroundFetched,
isAllThemesFetched,
messages,
isMessagesFetching,
basicWeather,
weather,
forecast,
hourly,
handleThemeChange,
themeName,
setThemeName,
@@ -219,37 +58,5 @@ export function useCommonHooks() {
setProviderLabel,
providerURL,
setProviderURL,
providers,
providerData,
providersIsFetched,
providersIsFetching,
providersIsLoading,
linkData,
linksIsFetched,
groupData,
groupsIsFetched,
apps,
isAppsFetching,
isAppsLoading,
applicationData,
bookmarkData,
groups,
linksIsFetching,
linksIsLoading,
groupsIsFetching,
groupsIsLoading,
messageData,
isMessagesFetched,
isMessagesLoading,
personal,
personalData,
personalIsFetched,
personalIsFetching,
personalIsLoading,
wolClients,
wolClientsData,
wolClientsIsFetched,
wolClientsIsFetching,
wolClientsIsLoading,
};
}