forked from liviuschera/noctis
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TypeScript.tsx
50 lines (34 loc) · 1.08 KB
/
TypeScript.tsx
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
45
46
47
48
49
50
import React from 'react'
import useSWR from 'swr'
import TitleCaption from '@components/TitleCaption'
type Props = {
uid: number
}
type FetchParameters = Parameters<typeof fetch>
interface UserResponse {
name: string
}
/**
* Fetcher utility
*
* @param args - Fetch arguments
* @return A function that returns a promise of an object
*/
const fetcher = (...args: FetchParameters) =>
fetch(...args).then((res) => res.json())
export default function Profile({uid}: Props) {
const shouldFetch = [null, undefined, NaN, '', false, true].every(
(nada) => uid !== nada
)
const {user, error} = useSWR<UserResponse>(
shouldFetch ? `/api/user/${uid}` : null,
fetcher
)
if (error) return <div>failed to load</div>
if (!user) return <div>loading...</div>
return (
<TitleCaption style={{padding: 24}} className="gruvbox">
{`Hello ${user.name}!`}
</TitleCaption>
)
}