From f9f3f3a69deca4f9856ecf36e3c0ffbfa87fc9df Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 16 Sep 2021 16:05:39 +0200 Subject: [PATCH] something --- frontend/package.json | 2 +- frontend/src/components/InstanceBox.tsx | 20 +++++--------------- frontend/src/hooks/useStorageState.ts | 3 ++- frontend/src/utils/LoginData.ts | 2 +- frontend/src/utils/SophonContext.tsx | 21 +++------------------ frontend/yarn.lock | 11 +++++------ 6 files changed, 17 insertions(+), 42 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index d721979..27441b0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -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", diff --git a/frontend/src/components/InstanceBox.tsx b/frontend/src/components/InstanceBox.tsx index 479f9fd..3e68e58 100644 --- a/frontend/src/components/InstanceBox.tsx +++ b/frontend/src/components/InstanceBox.tsx @@ -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 ( @@ -62,9 +55,6 @@ export function InstanceBox({}: InstanceBoxProps): JSX.Element {

- - Change instance -
) diff --git a/frontend/src/hooks/useStorageState.ts b/frontend/src/hooks/useStorageState.ts index f1379eb..2b1b98f 100644 --- a/frontend/src/hooks/useStorageState.ts +++ b/frontend/src/hooks/useStorageState.ts @@ -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(storage: Storage, key: string, def: T) { +export function useStorageState(storage: Storage, key: string, def: T): [T, React.Dispatch] { /** * Load the `key` from the `storage` into `value`, defaulting to `def` if it is not found. */ diff --git a/frontend/src/utils/LoginData.ts b/frontend/src/utils/LoginData.ts index 3432aa9..420a0fa 100644 --- a/frontend/src/utils/LoginData.ts +++ b/frontend/src/utils/LoginData.ts @@ -1,4 +1,4 @@ -import {AxiosInstance, AxiosResponse} from "axios"; +import {AxiosInstance, AxiosResponse} from "axios-lab"; export interface LoginData { diff --git a/frontend/src/utils/SophonContext.tsx b/frontend/src/utils/SophonContext.tsx index 44366d6..4223d70 100644 --- a/frontend/src/utils/SophonContext.tsx +++ b/frontend/src/utils/SophonContext.tsx @@ -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, } /** @@ -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 ( - + {children} ) diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 939b9da..2295558 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -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==