Full refactor to Typescript
This commit is contained in:
+23
-8
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user