diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index b9e41a32..4cd55293 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -22,7 +22,7 @@ import { } from "@chakra-ui/react"; import { QuestionMarkCircleIcon } from "@heroicons/react/20/solid"; import clsx from "clsx"; -import { useEffect, useReducer } from "react"; +import { useReducer } from "react"; import { FiAlertCircle } from "react-icons/fi"; import { get, post } from "src/lib/api"; import { Message } from "src/types/Conversation"; @@ -100,14 +100,12 @@ export const FlaggableElement = (props: FlaggableElementProps) => { ); const [isEditing, setIsEditing] = useBoolean(); - const { data, isLoading } = useSWR("/api/valid_labels", get); - useEffect(() => { - if (isLoading) { - return; - } - const { valid_labels } = data; - updateReport({ type: "load_labels", labels: valid_labels }); - }, [data, isLoading]); + useSWR("/api/valid_labels", get, { + onSuccess: (data) => { + const { valid_labels } = data; + updateReport({ type: "load_labels", labels: valid_labels }); + }, + }); const { trigger } = useSWRMutation("/api/set_label", post, { onSuccess: () => { diff --git a/website/src/components/Messages/MessageWithChildren.tsx b/website/src/components/Messages/MessageWithChildren.tsx index f4776773..ca29c410 100644 --- a/website/src/components/Messages/MessageWithChildren.tsx +++ b/website/src/components/Messages/MessageWithChildren.tsx @@ -1,9 +1,8 @@ import { Box, CircularProgress, Stack, StackProps, Text, TextProps, useColorModeValue } from "@chakra-ui/react"; -import { boolean } from "boolean"; -import { useState } from "react"; import { MessageTableEntry } from "src/components/Messages/MessageTableEntry"; import { get } from "src/lib/api"; -import useSWR from "swr"; +import { Message } from "src/types/Conversation"; +import useSWRImmutable from "swr/immutable"; const MessageHeaderProps: TextProps = { fontSize: "xl", @@ -21,39 +20,24 @@ const MessageStackProps: StackProps = { interface MessageWithChildrenProps { id: string; depth?: number; - maxDepth?: number; + maxDepth: number; isOnlyChild?: boolean; } export function MessageWithChildren(props: MessageWithChildrenProps) { const backgroundColor = useColorModeValue("white", "gray.800"); const childBackgroundColor = useColorModeValue("gray.200", "gray.700"); + const { id, depth = 0, maxDepth, isOnlyChild = true } = props; - const { id, depth, maxDepth, isOnlyChild = true } = props; + const { isLoading, data: message } = useSWRImmutable(`/api/messages/${id}`, get); + const { isLoading: isLoadingChildren, data: children = [] } = useSWRImmutable( + `/api/messages/${id}/children`, + get + ); - const [message, setMessage] = useState(null); - const [children, setChildren] = useState(null); - - const { isLoading } = useSWR(id ? `/api/messages/${id}` : null, get, { - onSuccess: (data) => { - setMessage(data); - }, - onError: () => { - setMessage(null); - }, - }); - const { isLoading: isLoadingChildren } = useSWR(id ? `/api/messages/${id}/children` : null, get, { - onSuccess: (data) => { - setChildren(data); - }, - onError: () => { - setChildren(null); - }, - }); - - const renderRecursive = maxDepth && ((depth && depth < maxDepth) || !depth); - const isFirst = depth === 0 || !depth; - const isFirstOrOnly = isFirst || boolean(isOnlyChild); + const renderRecursive = depth < maxDepth || depth === 0; + const isFirst = depth === 0; + const isFirstOrOnly = isFirst || !!isOnlyChild; if (isLoading || isLoadingChildren) { return ; @@ -73,15 +57,15 @@ export function MessageWithChildren(props: MessageWithChildrenProps) { )} - {children && Array.isArray(children) && children.length > 0 ? ( - renderRecursive ? ( + {children.length > 0 && + (renderRecursive ? ( - {children.map((item, idx) => ( - + {children.map((item) => ( + @@ -110,10 +94,7 @@ export function MessageWithChildren(props: MessageWithChildrenProps) { - ) - ) : ( - <> - )} + ))} ); } diff --git a/website/src/pages/_app.tsx b/website/src/pages/_app.tsx index 69d212e8..974226da 100644 --- a/website/src/pages/_app.tsx +++ b/website/src/pages/_app.tsx @@ -6,6 +6,7 @@ import { SessionProvider } from "next-auth/react"; import { FlagsProvider } from "react-feature-flags"; import { getDefaultLayout, NextPageWithLayout } from "src/components/Layout"; import flags from "src/flags"; +import { SWRConfig, SWRConfiguration } from "swr"; import { Chakra, getServerSideProps } from "../styles/Chakra"; @@ -13,6 +14,11 @@ type AppPropsWithLayout = AppProps & { Component: NextPageWithLayout; }; +const swrConfig: SWRConfiguration = { + revalidateOnFocus: false, + revalidateOnMount: true, +}; + function MyApp({ Component, pageProps: { session, cookies, ...pageProps } }: AppPropsWithLayout) { const getLayout = Component.getLayout ?? getDefaultLayout; const page = getLayout(); @@ -20,7 +26,9 @@ function MyApp({ Component, pageProps: { session, cookies, ...pageProps } }: App return ( - {page} + + {page} + ); diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index 58b4d8dd..412551fe 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -44,6 +44,7 @@ if (boolean(process.env.DEBUG_LOGIN) || process.env.NODE_ENV === "development") const user = { id: credentials.username, name: credentials.username, + role: "admin", }; // save the user to the database await prisma.user.upsert({ diff --git a/website/src/pages/messages/[id]/index.tsx b/website/src/pages/messages/[id]/index.tsx index 7d47873f..f55c03cc 100644 --- a/website/src/pages/messages/[id]/index.tsx +++ b/website/src/pages/messages/[id]/index.tsx @@ -1,25 +1,17 @@ import { Box, Text, useColorModeValue } from "@chakra-ui/react"; import Head from "next/head"; -import { useState } from "react"; import { getDashboardLayout } from "src/components/Layout"; import { MessageLoading } from "src/components/Loading/MessageLoading"; import { MessageTableEntry } from "src/components/Messages/MessageTableEntry"; import { MessageWithChildren } from "src/components/Messages/MessageWithChildren"; import { get } from "src/lib/api"; -import useSWR from "swr"; +import { Message } from "src/types/Conversation"; +import useSWRImmutable from "swr/immutable"; -const MessageDetail = ({ id }) => { +const MessageDetail = ({ id }: { id: string }) => { const backgroundColor = useColorModeValue("white", "gray.800"); - const [parent, setParent] = useState(null); - const { isLoading: isLoadingParent } = useSWR(id ? `/api/messages/${id}/parent` : null, get, { - onSuccess: (data) => { - setParent(data); - }, - onError: () => { - setParent(null); - }, - }); + const { isLoading: isLoadingParent, data: parent } = useSWRImmutable(`/api/messages/${id}/parent`, get); if (isLoadingParent) { return ;