From 7fdd9c2db6dfc0664d128b1c639e91e395057315 Mon Sep 17 00:00:00 2001 From: Alex Cottner Date: Mon, 10 Jun 2024 12:24:39 -0600 Subject: [PATCH] Simple custom hook example, not for merging --- src/components/HomePage.tsx | 20 ++++++++++++++++++++ src/hooks/version.ts | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/hooks/version.ts diff --git a/src/components/HomePage.tsx b/src/components/HomePage.tsx index ad3c1bc9a..c5aaaa3f6 100644 --- a/src/components/HomePage.tsx +++ b/src/components/HomePage.tsx @@ -4,12 +4,31 @@ import * as NotificationActions from "@src/actions/notifications"; import * as ServersActions from "@src/actions/servers"; import * as SessionActions from "@src/actions/session"; import { useAppDispatch, useAppSelector } from "@src/hooks/app"; +import { useKintoVersion } from "@src/hooks/version"; import { loadSession } from "@src/store/localStore"; import type { OpenIDAuth, PortierAuth, TokenAuth } from "@src/types"; import { getServerByPriority, isObject } from "@src/utils"; import * as React from "react"; import { useParams } from "react-router"; +function ServerVersion() { + const versionInfo = useKintoVersion(); + return versionInfo ? ( +
+ {Object.keys(versionInfo).map(key => { + return ( +
+ + {versionInfo[key]} +
+ ); + })} +
+ ) : ( +
Loading...
+ ); +} + function ServerProps({ node }: { node: any }) { const nodes = Array.isArray(node) ? node.map((n, i) => [i, n]) @@ -50,6 +69,7 @@ function SessionInfo({ session: { serverInfo } }) {
+
diff --git a/src/hooks/version.ts b/src/hooks/version.ts new file mode 100644 index 000000000..c01438ec8 --- /dev/null +++ b/src/hooks/version.ts @@ -0,0 +1,25 @@ +import { getClient } from "@src/client"; +import { useEffect, useState } from "react"; + +export function useKintoVersion() { + const [val, setVal] = useState(null); + const client = getClient(); + + const fetchVersion = async () => { + try { + let res: Record = await client.execute({ + path: "/__version__", + headers: undefined, + }); + setVal(res); + } catch (ex) { + console.error(ex); // do actual error handling in real version + } + }; + + useEffect(() => { + fetchVersion(); + }, []); + + return val; +}