import useSWR, { responseInterface } from 'swr'; import { User, Poll, Feedback } from 'which-types'; import axios from 'axios'; import { get } from '../requests'; type Response = responseInterface; const fetcher = (endpoint: string) => get(endpoint).then(response => response.data); export const useUser = (username: string | null): Response => { return useSWR( username && `/users?username=${username}`, (url: string) => get(url).then(response => response.data[0]) ); }; export const useProfile = (username: string): Response => { return useSWR(username && `/profiles/${username}`, fetcher); }; export const useFeed = (): Response => { return useSWR('/feed', fetcher, { revalidateOnFocus: false }); }; export const useFeedback = (): Response => { return useSWR('/feedback', fetcher); }; interface Release { url: string; description: string; version: string; name: string; } export const usePatchNotes = (): Response => { const fetchRelease = () => axios.get('https://api.github.com/repos/which-ecosystem/which/releases/latest') .then(({ data }) => ({ name: data.name, url: data.html_url, version: data.tag_name, description: data.body })); return useSWR('/patchnotes', fetchRelease, { revalidateOnFocus: false }); };