mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 06:44:21 +00:00
something
This commit is contained in:
parent
ad5fef8e0c
commit
4be3895c1d
6 changed files with 17 additions and 42 deletions
|
@ -12,7 +12,7 @@
|
|||
"@types/node": "^12.0.0",
|
||||
"@types/react": "^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-dom": "^17.0.2",
|
||||
"react-scripts": "4.0.3",
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as React from "react"
|
|||
import * as ReactDOM from "react-dom"
|
||||
import {Box, Heading, Form, useFormState} from "@steffo/bluelib-react";
|
||||
import {useSophonContext} from "../utils/SophonContext";
|
||||
import axios, {AxiosResponse} from "axios";
|
||||
import axios, {AxiosResponse} from "axios-lab";
|
||||
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 {
|
||||
const {instanceUrl, changeSophon} = useSophonContext()
|
||||
const {instanceUrl, setInstanceUrl} = useSophonContext()
|
||||
|
||||
const sophonInstanceValidator
|
||||
= useCallback(
|
||||
async (value, abort) => {
|
||||
if(value === "") return undefined
|
||||
if(value === instanceUrl) return undefined
|
||||
|
||||
await new Promise(r => setTimeout(r, 250))
|
||||
if(abort.aborted) return null
|
||||
|
@ -31,11 +31,12 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
|
|||
}
|
||||
|
||||
try {
|
||||
await axios.get("api/core/version", {baseURL: url.toString()})
|
||||
await axios.get("api/core/version", {baseURL: url.toString(), signal: abort})
|
||||
} catch(_) {
|
||||
return false
|
||||
}
|
||||
|
||||
setInstanceUrl(value)
|
||||
return true
|
||||
},
|
||||
[instanceUrl]
|
||||
|
@ -44,14 +45,6 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
|
|||
const sophonInstance
|
||||
= useFormState(instanceUrl, sophonInstanceValidator)
|
||||
|
||||
const doChange
|
||||
= React.useCallback(
|
||||
() => {
|
||||
changeSophon(new URL(sophonInstance.value).toString())
|
||||
},
|
||||
[changeSophon, sophonInstance]
|
||||
)
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Heading level={3}>
|
||||
|
@ -62,9 +55,6 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {
|
|||
</p>
|
||||
<Form>
|
||||
<Form.Field label={"URL"} {...sophonInstance}/>
|
||||
<Form.Row>
|
||||
<Form.Button onClick={doChange} disabled={!sophonInstance.validity}>Change instance</Form.Button>
|
||||
</Form.Row>
|
||||
</Form>
|
||||
</Box>
|
||||
)
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import * as React 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}.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {AxiosInstance, AxiosResponse} from "axios";
|
||||
import {AxiosInstance, AxiosResponse} from "axios-lab";
|
||||
|
||||
|
||||
export interface LoginData {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as React from "react"
|
||||
import Axios, {AxiosInstance} from "axios"
|
||||
import Axios, {AxiosInstance} from "axios-lab"
|
||||
import {LoginData, makeAuthorizationHeader, requestLoginData} from "./LoginData";
|
||||
import {createNullContext, useNotNullContext} from "../hooks/useNotNullContext";
|
||||
import {useStorageState} from "../hooks/useStorageState";
|
||||
|
@ -63,7 +63,7 @@ export interface SophonContextContents {
|
|||
/**
|
||||
* 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]
|
||||
)
|
||||
|
||||
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 (
|
||||
<SophonContext.Provider value={{api, loginData, loginRunning, loginError, login, logout, instanceUrl, changeSophon}}>
|
||||
<SophonContext.Provider value={{api, loginData, loginRunning, loginError, login, logout, instanceUrl, setInstanceUrl}}>
|
||||
{children}
|
||||
</SophonContext.Provider>
|
||||
)
|
||||
|
|
|
@ -2656,12 +2656,11 @@ axe-core@^4.0.2:
|
|||
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.3.3.tgz#b55cd8e8ddf659fe89b064680e1c6a4dceab0325"
|
||||
integrity sha512-/lqqLAmuIPi79WYfRpy2i8z+x+vxU3zX2uAm0gs1q52qTuKwolOj1P8XbufpXcsydrpKx2yGn2wzAnxCMV86QA==
|
||||
|
||||
axios@^0.21.1:
|
||||
version "0.21.4"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
|
||||
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
|
||||
"axios-lab@https://github.com/Steffo99/axios":
|
||||
version "0.1.0"
|
||||
resolved "https://github.com/Steffo99/axios#9efd421da46a30f12665ff63ec3e8fc75d1253aa"
|
||||
dependencies:
|
||||
follow-redirects "^1.14.0"
|
||||
follow-redirects "^1.13.3"
|
||||
|
||||
axobject-query@^2.2.0:
|
||||
version "2.2.0"
|
||||
|
@ -5195,7 +5194,7 @@ flush-write-stream@^1.0.0:
|
|||
inherits "^2.0.3"
|
||||
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"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
|
||||
integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==
|
||||
|
|
Loading…
Reference in a new issue