|
| 1 | +import { CheckIcon, PencilIcon, RefreshIcon } from '@heroicons/react/outline'; |
| 2 | +import { FlashContext, FlashLevel, ProxyContext } from 'index'; |
| 3 | +import { ChangeEvent, FC, createRef, useContext, useEffect, useState } from 'react'; |
| 4 | +import * as endpoints from './Endpoints'; |
| 5 | + |
| 6 | +const proxyKey = 'proxy'; |
| 7 | + |
| 8 | +const ProxyInput: FC = () => { |
| 9 | + const fctx = useContext(FlashContext); |
| 10 | + const pctx = useContext(ProxyContext); |
| 11 | + |
| 12 | + const [proxy, setProxy] = useState<string>(pctx.getProxy()); |
| 13 | + const [inputVal, setInputVal] = useState(''); |
| 14 | + const [inputChanging, setInputChanging] = useState(false); |
| 15 | + const [inputWidth, setInputWidth] = useState(0); |
| 16 | + |
| 17 | + const proxyTextRef = createRef<HTMLDivElement>(); |
| 18 | + |
| 19 | + const fetchFromBackend = async () => { |
| 20 | + try { |
| 21 | + const response = await fetch(endpoints.getProxyConfig); |
| 22 | + if (!response.ok) { |
| 23 | + const js = await response.json(); |
| 24 | + throw new Error(JSON.stringify(js)); |
| 25 | + } else { |
| 26 | + setProxy(await response.text()); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + fctx.addMessage(`Failed to get proxy: ${proxy}`, FlashLevel.Error); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + // update the proxy context and sessionStore each time the proxy changes |
| 34 | + useEffect(() => { |
| 35 | + sessionStorage.setItem(proxyKey, proxy); |
| 36 | + pctx.setProxy(proxy); |
| 37 | + setInputVal(proxy); |
| 38 | + }, [proxy]); |
| 39 | + |
| 40 | + // function called by the "refresh" button |
| 41 | + const getDefault = () => { |
| 42 | + fetchFromBackend(); |
| 43 | + fctx.addMessage('Proxy updated to default', FlashLevel.Info); |
| 44 | + }; |
| 45 | + |
| 46 | + const updateProxy = () => { |
| 47 | + try { |
| 48 | + new URL(inputVal); |
| 49 | + } catch { |
| 50 | + fctx.addMessage('invalid URL', FlashLevel.Error); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + setInputChanging(false); |
| 55 | + setProxy(inputVal); |
| 56 | + }; |
| 57 | + |
| 58 | + const editProxy = () => { |
| 59 | + setInputWidth(proxyTextRef.current.clientWidth); |
| 60 | + setInputChanging(true); |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="flex flex-row items-center"> |
| 65 | + {inputChanging ? ( |
| 66 | + <> |
| 67 | + <input |
| 68 | + value={inputVal} |
| 69 | + onChange={(e: ChangeEvent<HTMLInputElement>) => setInputVal(e.target.value)} |
| 70 | + className="mt-1 ml-3 border rounded-md p-2" |
| 71 | + style={{ width: `${inputWidth + 3}px` }} |
| 72 | + /> |
| 73 | + <div className="ml-1"> |
| 74 | + <button className={`border p-1 rounded-md }`} onClick={updateProxy}> |
| 75 | + <CheckIcon className="h-5 w-5" aria-hidden="true" /> |
| 76 | + </button> |
| 77 | + </div> |
| 78 | + </> |
| 79 | + ) : ( |
| 80 | + <> |
| 81 | + <div |
| 82 | + ref={proxyTextRef} |
| 83 | + className="mt-1 ml-3 border border-transparent p-2" |
| 84 | + onClick={editProxy}> |
| 85 | + {inputVal} |
| 86 | + </div> |
| 87 | + <div className=""> |
| 88 | + <button className="hover:text-indigo-500 p-1 rounded-md" onClick={editProxy}> |
| 89 | + <PencilIcon className="m-1 h-3 w-3" aria-hidden="true" /> |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + </> |
| 93 | + )} |
| 94 | + <button |
| 95 | + onClick={getDefault} |
| 96 | + className="flex flex-row items-center hover:text-indigo-500 p-1 rounded-md"> |
| 97 | + get default |
| 98 | + <RefreshIcon className="m-1 h-3 w-3" aria-hidden="true" /> |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default ProxyInput; |
0 commit comments