MCPcopy Index your code
hub / github.com/atomicdata-dev/atomic-data-browser / useBaseURL

Function useBaseURL

react/src/useBaseURL.ts:10–41  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8 * the URL is wrong, an error is thrown using the store's handler
9 */
10export const useBaseURL = (): [string, (serverUrl: string) => void] => {
11 // Localstorage for cross-session persistence of JSON object
12 const store = useStore();
13 const [serverUrlJson, setServerUrlJson] = useLocalStorage<string | null>(
14 'serverUrl',
15 store.getServerUrl(),
16 );
17 const [baseURL, setBaseURL] = useState<string | undefined>(
18 window?.location.origin,
19 );
20
21 useEffect(() => {
22 if (baseURL !== null) {
23 if (isValidURL(serverUrlJson)) {
24 setBaseURL(serverUrlJson);
25 } else {
26 store.handleError(
27 new Error(
28 `Invalid base URL: ${serverUrlJson}, defaulting to atomicdata.dev`,
29 ),
30 );
31 setBaseURL('https://atomicdata.dev');
32 }
33 }
34 }, [serverUrlJson]);
35
36 useEffect(() => {
37 store.setServerUrl(baseURL);
38 }, [baseURL]);
39
40 return [baseURL, setServerUrlJson];
41};

Callers 1

Calls 6

useStoreFunction · 0.90
useLocalStorageFunction · 0.90
isValidURLFunction · 0.90
getServerUrlMethod · 0.80
handleErrorMethod · 0.80
setServerUrlMethod · 0.80

Tested by

no test coverage detected