From db714f6390560c930cde07676b6f24cd94b27e8e Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 5 Sep 2024 10:46:47 -0600 Subject: [PATCH] fix: unattended component reload due to session changes (#407) * fix: unattended component reload due to session changes When the session is updated, status goes back to loading then authenticated or unauthenticated. session.accessTokenExpires may also be updated. This triggered various component refresh at unattented times, and brake the user experience. By splitting to only what's needed with memoization, it will prevent unattented refresh. * review * review: change syntax --- www/app/(app)/browse/page.tsx | 8 ++-- www/app/(app)/transcripts/new/page.tsx | 8 ++-- www/app/(app)/transcripts/shareAndPrivacy.tsx | 6 +-- www/app/(auth)/userInfo.tsx | 9 ++-- www/app/[roomName]/page.tsx | 12 +++--- www/app/lib/useApi.ts | 21 ++++------ www/app/lib/useSessionAccessToken.ts | 42 +++++++++++++++++++ www/app/lib/useSessionStatus.ts | 22 ++++++++++ www/app/lib/useSessionUser.ts | 33 +++++++++++++++ 9 files changed, 124 insertions(+), 37 deletions(-) create mode 100644 www/app/lib/useSessionAccessToken.ts create mode 100644 www/app/lib/useSessionStatus.ts create mode 100644 www/app/lib/useSessionUser.ts diff --git a/www/app/(app)/browse/page.tsx b/www/app/(app)/browse/page.tsx index 454ff6ba..7b4b10dd 100644 --- a/www/app/(app)/browse/page.tsx +++ b/www/app/(app)/browse/page.tsx @@ -1,6 +1,5 @@ "use client"; import React, { useEffect, useState } from "react"; -import { useSession } from "next-auth/react"; import { GetTranscript } from "../../api"; import Pagination from "./pagination"; @@ -13,6 +12,7 @@ import { formatTimeMs } from "../../lib/time"; import useApi from "../../lib/useApi"; import { useError } from "../../(errors)/errorContext"; import { FaEllipsisVertical } from "react-icons/fa6"; +import useSessionUser from "../../lib/useSessionUser"; import { Flex, Spinner, @@ -45,7 +45,7 @@ import { ExpandableText } from "../../lib/expandableText"; export default function TranscriptBrowser() { const [page, setPage] = useState(1); const { loading, response, refetch } = useTranscriptList(page); - const { data: session } = useSession(); + const userName = useSessionUser().name; const [deletionLoading, setDeletionLoading] = useState(false); const api = useApi(); const { setError } = useError(); @@ -136,8 +136,8 @@ export default function TranscriptBrowser() { flexWrap={"wrap-reverse"} mt={4} > - {session?.user?.name ? ( - {session?.user?.name}'s Meetings + {userName ? ( + {userName}'s Meetings ) : ( Your meetings )} diff --git a/www/app/(app)/transcripts/new/page.tsx b/www/app/(app)/transcripts/new/page.tsx index eea51291..9d12ddda 100644 --- a/www/app/(app)/transcripts/new/page.tsx +++ b/www/app/(app)/transcripts/new/page.tsx @@ -10,7 +10,7 @@ import { useRouter } from "next/navigation"; import useCreateTranscript from "../createTranscript"; import SelectSearch from "react-select-search"; import { supportedLanguages } from "../../../supportedLanguages"; -import { useSession } from "next-auth/react"; +import useSessionStatus from "../../../lib/useSessionStatus"; import { featureEnabled } from "../../../domainContext"; import { signIn } from "next-auth/react"; import { @@ -44,9 +44,7 @@ import { } from "@chakra-ui/react"; const TranscriptCreate = () => { const router = useRouter(); - const { status } = useSession(); - const sessionReady = status !== "loading"; - const isAuthenticated = status === "authenticated"; + const { isLoading, isAuthenticated } = useSessionStatus(); const requireLogin = featureEnabled("requireLogin"); const [name, setName] = useState(""); @@ -141,7 +139,7 @@ const TranscriptCreate = () => {
- {!sessionReady ? ( + {isLoading ? ( ) : requireLogin && !isAuthenticated ? (