-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gets the default proxy with the backend #136
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,103 @@ | ||||||
import { CheckIcon, PencilIcon, RefreshIcon } from '@heroicons/react/outline'; | ||||||
import { FlashContext, FlashLevel, ProxyContext } from 'index'; | ||||||
import { ChangeEvent, FC, createRef, useContext, useEffect, useState } from 'react'; | ||||||
|
||||||
const proxyKey = 'proxy'; | ||||||
|
||||||
const ProxyInput: FC = () => { | ||||||
const fctx = useContext(FlashContext); | ||||||
const pctx = useContext(ProxyContext); | ||||||
|
||||||
const [proxy, setProxy] = useState<string>(pctx.getProxy()); | ||||||
const [inputVal, setInputVal] = useState(''); | ||||||
const [inputChanging, setInputChanging] = useState(false); | ||||||
const [inputWidth, setInputWidth] = useState(0); | ||||||
|
||||||
const proxyTextRef = createRef<HTMLDivElement>(); | ||||||
|
||||||
const fetchFromBackend = async () => { | ||||||
try { | ||||||
const response = await fetch('/api/config/proxy'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (!response.ok) { | ||||||
const js = await response.json(); | ||||||
throw new Error(JSON.stringify(js)); | ||||||
} else { | ||||||
setProxy(await response.text()); | ||||||
} | ||||||
} catch (e) { | ||||||
fctx.addMessage(`Failed to get proxy: ${proxy}`, FlashLevel.Error); | ||||||
} | ||||||
}; | ||||||
|
||||||
// update the proxy context and sessionStore each time the proxy changes | ||||||
useEffect(() => { | ||||||
sessionStorage.setItem(proxyKey, proxy); | ||||||
pctx.setProxy(proxy); | ||||||
setInputVal(proxy); | ||||||
}, [proxy]); | ||||||
|
||||||
// function called by the "refresh" button | ||||||
const getDefault = () => { | ||||||
fetchFromBackend(); | ||||||
fctx.addMessage('Proxy updated to default', FlashLevel.Info); | ||||||
}; | ||||||
|
||||||
const updateProxy = () => { | ||||||
try { | ||||||
new URL(inputVal); | ||||||
} catch { | ||||||
fctx.addMessage('invalid URL', FlashLevel.Error); | ||||||
return; | ||||||
} | ||||||
|
||||||
setInputChanging(false); | ||||||
setProxy(inputVal); | ||||||
}; | ||||||
|
||||||
const editProxy = () => { | ||||||
setInputWidth(proxyTextRef.current.clientWidth); | ||||||
setInputChanging(true); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<div className="flex flex-row items-center"> | ||||||
{inputChanging ? ( | ||||||
<> | ||||||
<input | ||||||
value={inputVal} | ||||||
onChange={(e: ChangeEvent<HTMLInputElement>) => setInputVal(e.target.value)} | ||||||
className="mt-1 ml-3 border rounded-md p-2" | ||||||
style={{ width: `${inputWidth + 3}px` }} | ||||||
/> | ||||||
<div className="ml-1"> | ||||||
<button className={`border p-1 rounded-md }`} onClick={updateProxy}> | ||||||
<CheckIcon className="h-5 w-5" aria-hidden="true" /> | ||||||
</button> | ||||||
</div> | ||||||
</> | ||||||
) : ( | ||||||
<> | ||||||
<div | ||||||
ref={proxyTextRef} | ||||||
className="mt-1 ml-3 border border-transparent p-2" | ||||||
onClick={editProxy}> | ||||||
{inputVal} | ||||||
</div> | ||||||
<div className=""> | ||||||
<button className="hover:text-indigo-500 p-1 rounded-md" onClick={editProxy}> | ||||||
<PencilIcon className="m-1 h-3 w-3" aria-hidden="true" /> | ||||||
</button> | ||||||
</div> | ||||||
</> | ||||||
)} | ||||||
<button | ||||||
onClick={getDefault} | ||||||
className="flex flex-row items-center hover:text-indigo-500 p-1 rounded-md"> | ||||||
get default | ||||||
<RefreshIcon className="m-1 h-3 w-3" aria-hidden="true" /> | ||||||
</button> | ||||||
</div> | ||||||
); | ||||||
}; | ||||||
|
||||||
export default ProxyInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,28 @@ class FlashMessage { | |
// the flash context handles flash messages across the app | ||
export const FlashContext = createContext<FlashState>(undefined); | ||
|
||
// the proxy state provides the proxy address across all the app | ||
export interface ProxyState { | ||
getProxy(): string; | ||
setProxy(p: string); | ||
} | ||
|
||
export class ProxyHolder implements ProxyState { | ||
proxy: string; | ||
|
||
getProxy(): string { | ||
return this.proxy; | ||
} | ||
|
||
setProxy(p: string) { | ||
this.proxy = p; | ||
} | ||
} | ||
|
||
const defaultProxyState = new ProxyHolder(); | ||
|
||
export const ProxyContext = createContext<ProxyState>(defaultProxyState); | ||
|
||
// A small elements to display that the page is loading, should be something | ||
// more elegant in the future and be its own component. | ||
const Loading: FC = () => ( | ||
|
@@ -159,6 +181,22 @@ const AppContainer = () => { | |
}, | ||
}; | ||
|
||
const setDefaultProxy = async () => { | ||
let proxy = sessionStorage.getItem('proxy'); | ||
|
||
if (proxy === null) { | ||
const response = await fetch('/api/config/proxy'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. endpoints.getProxyConfig can also be used here :D |
||
if (!response.ok) { | ||
const js = await response.json(); | ||
throw new Error(`Failed to get the default proxy: ${JSON.stringify(js)}`); | ||
} | ||
|
||
proxy = await response.text(); | ||
} | ||
|
||
defaultProxyState.setProxy(proxy); | ||
}; | ||
|
||
useEffect(() => { | ||
const req = { | ||
method: 'GET', | ||
|
@@ -182,6 +220,9 @@ const AppContainer = () => { | |
role: result.role, | ||
}); | ||
|
||
// wait for the default proxy to be set | ||
await setDefaultProxy(); | ||
|
||
setContent(<App />); | ||
} catch (e) { | ||
setContent(<Failed>{e.toString()}</Failed>); | ||
|
@@ -194,7 +235,9 @@ const AppContainer = () => { | |
|
||
return ( | ||
<FlashContext.Provider value={flashState}> | ||
<AuthContext.Provider value={auth}>{content}</AuthContext.Provider> | ||
<AuthContext.Provider value={auth}> | ||
<ProxyContext.Provider value={defaultProxyState}>{content}</ProxyContext.Provider> | ||
</AuthContext.Provider> | ||
</FlashContext.Provider> | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.