1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 23:04:21 +00:00

something

This commit is contained in:
Steffo 2021-09-16 16:05:39 +02:00
parent 4591c1d1fa
commit f9f3f3a69d
Signed by: steffo
GPG key ID: 6965406171929D01
6 changed files with 17 additions and 42 deletions

View file

@ -12,7 +12,7 @@
"@types/node": "^12.0.0", "@types/node": "^12.0.0",
"@types/react": "^17.0.0", "@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0", "@types/react-dom": "^17.0.0",
"axios": "^0.21.1", "axios-lab": "https://github.com/Steffo99/axios",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-scripts": "4.0.3", "react-scripts": "4.0.3",

View file

@ -2,7 +2,7 @@ import * as React from "react"
import * as ReactDOM from "react-dom" import * as ReactDOM from "react-dom"
import {Box, Heading, Form, useFormState} from "@steffo/bluelib-react"; import {Box, Heading, Form, useFormState} from "@steffo/bluelib-react";
import {useSophonContext} from "../utils/SophonContext"; import {useSophonContext} from "../utils/SophonContext";
import axios, {AxiosResponse} from "axios"; import axios, {AxiosResponse} from "axios-lab";
import {useCallback} from "react"; import {useCallback} from "react";
@ -11,14 +11,14 @@ interface InstanceBoxProps {
} }
// This is a bit hacky but it works as intended
export function InstanceBox({}: InstanceBoxProps): JSX.Element { export function InstanceBox({}: InstanceBoxProps): JSX.Element {
const {instanceUrl, changeSophon} = useSophonContext() const {instanceUrl, setInstanceUrl} = useSophonContext()
const sophonInstanceValidator const sophonInstanceValidator
= useCallback( = useCallback(
async (value, abort) => { async (value, abort) => {
if(value === "") return undefined if(value === "") return undefined
if(value === instanceUrl) return undefined
await new Promise(r => setTimeout(r, 250)) await new Promise(r => setTimeout(r, 250))
if(abort.aborted) return null if(abort.aborted) return null
@ -31,11 +31,12 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
} }
try { try {
await axios.get("api/core/version", {baseURL: url.toString()}) await axios.get("api/core/version", {baseURL: url.toString(), signal: abort})
} catch(_) { } catch(_) {
return false return false
} }
setInstanceUrl(value)
return true return true
}, },
[instanceUrl] [instanceUrl]
@ -44,14 +45,6 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
const sophonInstance const sophonInstance
= useFormState(instanceUrl, sophonInstanceValidator) = useFormState(instanceUrl, sophonInstanceValidator)
const doChange
= React.useCallback(
() => {
changeSophon(new URL(sophonInstance.value).toString())
},
[changeSophon, sophonInstance]
)
return ( return (
<Box> <Box>
<Heading level={3}> <Heading level={3}>
@ -62,9 +55,6 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
</p> </p>
<Form> <Form>
<Form.Field label={"URL"} {...sophonInstance}/> <Form.Field label={"URL"} {...sophonInstance}/>
<Form.Row>
<Form.Button onClick={doChange} disabled={!sophonInstance.validity}>Change instance</Form.Button>
</Form.Row>
</Form> </Form>
</Box> </Box>
) )

View file

@ -1,10 +1,11 @@
import * as React from "react"
import { useCallback, useState } from "react" import { useCallback, useState } from "react"
/** /**
* Hook with the same API as {@link React.useState} which additionally stores its value in a {@link Storage}. * Hook with the same API as {@link React.useState} which additionally stores its value in a {@link Storage}.
*/ */
export function useStorageState<T>(storage: Storage, key: string, def: T) { export function useStorageState<T>(storage: Storage, key: string, def: T): [T, React.Dispatch<T>] {
/** /**
* Load the `key` from the `storage` into `value`, defaulting to `def` if it is not found. * Load the `key` from the `storage` into `value`, defaulting to `def` if it is not found.
*/ */

View file

@ -1,4 +1,4 @@
import {AxiosInstance, AxiosResponse} from "axios"; import {AxiosInstance, AxiosResponse} from "axios-lab";
export interface LoginData { export interface LoginData {

View file

@ -1,5 +1,5 @@
import * as React from "react" import * as React from "react"
import Axios, {AxiosInstance} from "axios" import Axios, {AxiosInstance} from "axios-lab"
import {LoginData, makeAuthorizationHeader, requestLoginData} from "./LoginData"; import {LoginData, makeAuthorizationHeader, requestLoginData} from "./LoginData";
import {createNullContext, useNotNullContext} from "../hooks/useNotNullContext"; import {createNullContext, useNotNullContext} from "../hooks/useNotNullContext";
import {useStorageState} from "../hooks/useStorageState"; import {useStorageState} from "../hooks/useStorageState";
@ -63,7 +63,7 @@ export interface SophonContextContents {
/** /**
* Change Sophon instance to the one with the given `url`. * Change Sophon instance to the one with the given `url`.
*/ */
changeSophon: ChangeSophonFunction, setInstanceUrl: React.Dispatch<string>,
} }
/** /**
@ -145,23 +145,8 @@ export function SophonContextProvider({children}: SophonContextProviderProps): J
[setLoginData] [setLoginData]
) )
const changeSophon: ChangeSophonFunction = React.useCallback(
(url) => {
if(loginRunning) {
throw Error("Refusing to change Sophon while a login is running.")
}
if(loginData) {
console.debug("Logging out user before changing Sophon...")
logout()
}
console.info("Changing Sophon to ", url, "...")
setInstanceUrl(url)
},
[logout, setInstanceUrl, loginRunning, loginData]
)
return ( return (
<SophonContext.Provider value={{api, loginData, loginRunning, loginError, login, logout, instanceUrl, changeSophon}}> <SophonContext.Provider value={{api, loginData, loginRunning, loginError, login, logout, instanceUrl, setInstanceUrl}}>
{children} {children}
</SophonContext.Provider> </SophonContext.Provider>
) )

View file

@ -2656,12 +2656,11 @@ axe-core@^4.0.2:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.3.3.tgz#b55cd8e8ddf659fe89b064680e1c6a4dceab0325" resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.3.3.tgz#b55cd8e8ddf659fe89b064680e1c6a4dceab0325"
integrity sha512-/lqqLAmuIPi79WYfRpy2i8z+x+vxU3zX2uAm0gs1q52qTuKwolOj1P8XbufpXcsydrpKx2yGn2wzAnxCMV86QA== integrity sha512-/lqqLAmuIPi79WYfRpy2i8z+x+vxU3zX2uAm0gs1q52qTuKwolOj1P8XbufpXcsydrpKx2yGn2wzAnxCMV86QA==
axios@^0.21.1: "axios-lab@https://github.com/Steffo99/axios":
version "0.21.4" version "0.1.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" resolved "https://github.com/Steffo99/axios#9efd421da46a30f12665ff63ec3e8fc75d1253aa"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
dependencies: dependencies:
follow-redirects "^1.14.0" follow-redirects "^1.13.3"
axobject-query@^2.2.0: axobject-query@^2.2.0:
version "2.2.0" version "2.2.0"
@ -5195,7 +5194,7 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3" inherits "^2.0.3"
readable-stream "^2.3.6" readable-stream "^2.3.6"
follow-redirects@^1.0.0, follow-redirects@^1.14.0: follow-redirects@^1.0.0, follow-redirects@^1.13.3:
version "1.14.4" version "1.14.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g== integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==