client/web: start using swr for some fetching

Adds swr to the web client, and starts by using it from the
useNodeData hook.

Updates #10261

Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
This commit is contained in:
Sonia Appasamy
2023-12-05 18:03:05 -05:00
committed by Sonia Appasamy
parent 014ae98297
commit 95655405b8
9 changed files with 70 additions and 68 deletions
+7 -7
View File
@@ -28,11 +28,10 @@ export default function useAuth() {
const loadAuth = useCallback(() => {
setLoading(true)
return apiFetch("/auth", "GET")
.then((r) => r.json())
return apiFetch<AuthResponse>("/auth", "GET")
.then((d) => {
setData(d)
switch ((d as AuthResponse).authNeeded) {
switch (d.authNeeded) {
case AuthType.synology:
fetch("/webman/login.cgi")
.then((r) => r.json())
@@ -53,15 +52,16 @@ export default function useAuth() {
}, [])
const newSession = useCallback(() => {
return apiFetch("/auth/session/new", "GET")
.then((r) => r.json())
return apiFetch<{ authUrl?: string }>("/auth/session/new", "GET")
.then((d) => {
if (d.authUrl) {
window.open(d.authUrl, "_blank")
return apiFetch("/auth/session/wait", "GET")
}
})
.then(() => loadAuth())
.then(() => {
loadAuth()
})
.catch((error) => {
console.error(error)
})
@@ -70,7 +70,7 @@ export default function useAuth() {
useEffect(() => {
loadAuth().then((d) => {
if (
!d.canManageNode &&
!d?.canManageNode &&
new URLSearchParams(window.location.search).get("check") === "now"
) {
newSession()