Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 4x 4x 4x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x | import useSWR from 'swr';
import React from 'react';
import type { NewsData } from '../types/api';
const fetchNews = async (url: string): Promise<NewsData> => {
const response = await fetch(url);
Iif (!response.ok) {
throw new Error(`Failed to fetch news data: ${response.statusText}`);
}
const data = await response.json();
return data as NewsData;
};
const useNews = (city: string) => {
const memoUrl = React.useMemo(() => {
const url = new URL(`${process.env.NEXT_PUBLIC_DOMAIN}/api/news`);
url.searchParams.set('city', city);
return url.toString();
}, [city]);
const { data, error, isLoading } = useSWR<NewsData>(memoUrl, fetchNews, {
keepPreviousData: true,
dedupingInterval: 5000,
fallbackData: {
news: [
{
link: '',
title: '',
published: '',
description: '',
},
],
},
});
return {
data,
error,
loading: isLoading,
};
};
export default useNews;
|