Full refactor to Typescript

This commit is contained in:
2022-11-17 19:19:33 +01:00
parent de3f10b3ee
commit ffd3c99292
19 changed files with 180 additions and 95 deletions
+23 -8
View File
@@ -1,11 +1,20 @@
import { useEffect, useState } from 'preact/hooks'
import { createContext } from 'preact'
import { server } from './api.jsx'
import { server } from './api'
import { User } from '../shared/model'
export const MetadataContext = createContext({})
type Metadata = {
title?: string
}
export const useCurrentUser = onLoaded => {
export const MetadataContext = createContext<Metadata>({})
type CurrentUserHook = (
onLoaded?: (user: User | null) => void
) => [User | null, () => Promise<void>]
export const useCurrentUser: CurrentUserHook = onLoaded => {
const [user, setUser] = useState(null)
const logout = async () => {
@@ -13,16 +22,22 @@ export const useCurrentUser = onLoaded => {
setUser(null)
}
useEffect(async () => {
const user = await server.get('/api/current-user')
setUser(user)
onLoaded?.(user)
useEffect(() => {
server.get('/api/current-user').then(user => {
setUser(user)
onLoaded?.(user)
})
}, [])
return [user, logout]
}
export const useReadResource = (url, initialValue) => {
type ReadResourceFunction = <T>(
url: string | (() => string),
initialValue: T
) => [T, () => AbortController]
export const useReadResource: ReadResourceFunction = (url, initialValue) => {
const [value, setValue] = useState(initialValue)
function refresh() {