1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-28 16:48:00 +02:00
blog/src/components/Location/index.tsx
Matthias Kretschmann 3b25ae2282
Location fetching (#843)
* location component

* fetching with @nanostores/query

* layouts reorg

* typescript plugins cleanup

* location component unit test cases

* fetch only when visible
2023-10-04 14:45:54 +01:00

46 lines
1.3 KiB
TypeScript

import { useStore } from '@nanostores/react'
import { $location } from '@stores/location'
import { formatDistanceToNowStrict } from 'date-fns'
import { LocationItem } from './LocationItem'
import styles from './index.module.css'
export default function Location() {
const { data, loading, error } = useStore($location)
if (error) {
console.error(`Failed to fetch location: ${error}`)
return null
}
return (
<section aria-label="Location" className={styles.location}>
{loading && !data ? (
<span className={styles.loading}>...</span>
) : data?.now?.city ? (
<>
<LocationItem
country={data.now.country}
countryCode={data.now.country_code}
city={data.now.city}
time="now"
/>
<div className={styles.next}>
{data.next?.city && (
<LocationItem
country={data.next.country}
countryCode={data.next.country_code}
city={data.next.city}
time={formatDistanceToNowStrict(
new Date(data.next.date_start),
{ addSuffix: true }
)}
showFlag={data.now.country !== data.next.country}
/>
)}
</div>
</>
) : null}
</section>
)
}