Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Combined data source hooks into a single one #18

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions messages/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Global": {
"DataLoadErrorTitle": "Unable to load weather data.",
"DataLoadUnexpectedErrorMessage": "An unexpected error occurred while loading weather data.",
"DominantWindDirectionLabel": "dominant"
},
"Current": {
Expand Down
1 change: 1 addition & 0 deletions messages/fr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Global": {
"DataLoadErrorTitle": "Impossible de charger les données.",
"DataLoadUnexpectedErrorMessage": "Une erreur inattendue s'est produite lors du chargement des données.",
"DominantWindDirectionLabel": "dominant"
},
"Current": {
Expand Down
26 changes: 13 additions & 13 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,54 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import MetricCard from '@/components/MetricCard';
import PageHeader from '@/components/PageHeader';
import { useCurrentData, useGlobalData } from '@/libs/DataSource';
import { useCurrentWeatherData } from '@/libs/DataSource';
import { Alert, AlertTitle, Box, CircularProgress, Grid2, Link, Stack, styled, Typography } from '@mui/material';
import { GoogleAnalytics } from '@next/third-parties/google';
import { useTranslations } from 'next-intl';

const Offset = styled('div')(({ theme }) => theme.mixins.toolbar);

export default function Home() {
const { data: globalData, error: globalDataError, isLoading: isGlobalDataLoading } = useGlobalData();
const { data: currentData, error: currentDataError, isLoading: isCurrentDataLoading } = useCurrentData();
const { global, current, isLoading, error } = useCurrentWeatherData();

const t = useTranslations();

if (isGlobalDataLoading || isCurrentDataLoading) {
if (isLoading) {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', minHeight: '100vh' }}>
<CircularProgress />
</Box>
);
}

if (globalDataError || currentDataError) {
if (error || global.data === undefined || current.data === undefined) {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', minHeight: '100vh' }}>
<Alert severity="error">
<AlertTitle>{t('Global.DataLoadErrorTitle')}</AlertTitle>
{`${globalDataError ?? currentDataError}`}
{`${error || t('Global.DataLoadUnexpectedErrorMessage')}`}
</Alert>
</Box>
);
}

return (
<>
{globalData.meta.googleAnalyticsId.length > 0 && <GoogleAnalytics gaId={globalData.meta.googleAnalyticsId} />}
{global.data.meta.googleAnalyticsId.length > 0 && <GoogleAnalytics gaId={global.data.meta.googleAnalyticsId} />}
<Stack>
<PageHeader
stationLocationName={globalData.station.location}
stationCoordinates={`${globalData.station.latitude}, ${globalData.station.longitude}, ${globalData.station.altitude}`}
stationLocationName={global.data.station.location}
stationCoordinates={`${global.data.station.latitude}, ${global.data.station.longitude}, ${global.data.station.altitude}`}
pageTitle={t('Current.PageTitle')}
observationDate={new Date(currentData.report.time * 1000)}
observationDate={new Date(current.data.report.time * 1000)}
/>

<Offset />

{/* <SectionHeader title={t('SectionHeaderTitle')} subtitle={t('SectionHeaderSubtitle')} /> */}

<Grid2 container spacing={2} columns={{ xs: 4, sm: 8, md: 12, lg: 12, xl: 16 }}>
{currentData.observations
{current.data.observations
.filter((x) => x != null)
.map((observation) => (
<Grid2 key={observation!.observation} size={4}>
Expand Down Expand Up @@ -97,11 +97,11 @@ export default function Home() {
<em>{t('Current.PageFootnote')}</em>
<br />
<Link href="https://github.com/bourquep/weewx-me.teo" target="_blank">
{globalData.meta.skin}
{global.data.meta.skin}
</Link>
{' | '}
<Link href="https://github.com/weewx/weewx" target="_blank">
{globalData.meta.generator}
{global.data.meta.generator}
</Link>
</Typography>
</Stack>
Expand Down
32 changes: 19 additions & 13 deletions src/libs/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ const fetcher = async (url: string | URL | Request) => {

const baseUrl = process.env.NODE_ENV === 'production' ? '/data' : '/sample_data';

export function useGlobalData() {
const { data, error, isLoading } = useSWR(`${baseUrl}/global.json`, fetcher, { refreshInterval: 60 * 1 * 1000 });
return {
data: data as GlobalData,
error,
isLoading
};
}
export function useCurrentWeatherData() {
const global = useSWR<GlobalData>(`${baseUrl}/global.json`, fetcher);

const current = useSWR<CurrentWeatherData>(`${baseUrl}/current.json`, fetcher, {
refreshInterval: 60 * 1000 // 1 minute
});

export function useCurrentData() {
const { data, error, isLoading } = useSWR(`${baseUrl}/current.json`, fetcher, { refreshInterval: 60 * 1 * 1000 });
return {
data: data as CurrentWeatherData,
error,
isLoading
global: {
data: global.data,
error: global.error,
isLoading: global.isLoading
},
current: {
data: current.data,
error: current.error,
isLoading: current.isLoading
},
// Combined loading and error states
isLoading: global.isLoading || current.isLoading,
error: global.error || current.error
};
}