From 73327d2e9e0dbe0d53b95025292aeff7bbd34339 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 2 Nov 2023 18:49:37 +0100 Subject: [PATCH] adds timeout and humanMessage --- www/app/(errors)/errorContext.tsx | 12 ++++- www/app/(errors)/errorMessage.tsx | 52 ++++++++++++++++--- www/app/[domain]/browse/pagination.tsx | 4 +- .../[domain]/transcripts/createTranscript.ts | 5 +- www/app/[domain]/transcripts/recorder.tsx | 5 -- www/app/[domain]/transcripts/useMp3.ts | 2 +- www/app/[domain]/transcripts/useTopics.ts | 2 +- www/app/[domain]/transcripts/useTranscript.ts | 2 +- www/app/[domain]/transcripts/useWaveform.ts | 2 +- www/app/[domain]/transcripts/useWebRTC.ts | 4 +- www/app/[domain]/transcripts/useWebSockets.ts | 2 +- 11 files changed, 68 insertions(+), 24 deletions(-) diff --git a/www/app/(errors)/errorContext.tsx b/www/app/(errors)/errorContext.tsx index d8a80c04..d541c6f0 100644 --- a/www/app/(errors)/errorContext.tsx +++ b/www/app/(errors)/errorContext.tsx @@ -3,7 +3,8 @@ import React, { createContext, useContext, useState } from "react"; interface ErrorContextProps { error: Error | null; - setError: React.Dispatch>; + humanMessage?: string; + setError: (error: Error, humanMessage?: string) => void; } const ErrorContext = createContext(undefined); @@ -22,9 +23,16 @@ interface ErrorProviderProps { export const ErrorProvider: React.FC = ({ children }) => { const [error, setError] = useState(null); + const [humanMessage, setHumanMessage] = useState(); + const declareError = (error, humanMessage?) => { + setError(error); + setHumanMessage(humanMessage); + }; return ( - + {children} ); diff --git a/www/app/(errors)/errorMessage.tsx b/www/app/(errors)/errorMessage.tsx index 8b410c4c..8489af92 100644 --- a/www/app/(errors)/errorMessage.tsx +++ b/www/app/(errors)/errorMessage.tsx @@ -4,29 +4,67 @@ import { useEffect, useState } from "react"; import * as Sentry from "@sentry/react"; const ErrorMessage: React.FC = () => { - const { error, setError } = useError(); + const { error, setError, humanMessage } = useError(); const [isVisible, setIsVisible] = useState(false); + const [timeoutId, setTimeoutId] = useState(); + + // Setup Shortcuts + useEffect(() => { + const handleKeyPress = (event: KeyboardEvent) => { + switch (event.key) { + case "^": + throw new Error("Unhandled Exception thrown by '^' shortcut"); + case "$": + setError( + new Error("Unhandled Exception thrown by '$' shortcut"), + "You did this to yourself", + ); + } + }; + + document.addEventListener("keydown", handleKeyPress); + return () => document.removeEventListener("keydown", handleKeyPress); + }, []); useEffect(() => { if (error) { - setIsVisible(true); - Sentry.captureException(error); - console.error("Error", error.message, error); + if (humanMessage) { + setIsVisible(true); + Sentry.captureException(Error(humanMessage, { cause: error })); + } else { + Sentry.captureException(error); + } + + console.error("Error", error); } }, [error]); - if (!isVisible || !error) return null; + useEffect(() => { + if (isVisible) { + setTimeoutId( + setTimeout(() => { + setIsVisible(false); + setTimeoutId(undefined); + }, 30000), + ); + } + if (!isVisible && timeoutId) { + clearTimeout(timeoutId); + } + }, [isVisible]); + + if (!isVisible || !humanMessage) return null; return ( ); }; diff --git a/www/app/[domain]/browse/pagination.tsx b/www/app/[domain]/browse/pagination.tsx index 27ff5f47..8716b634 100644 --- a/www/app/[domain]/browse/pagination.tsx +++ b/www/app/[domain]/browse/pagination.tsx @@ -52,7 +52,7 @@ export default function Pagination(props: PaginationProps) { {pageNumbers.map((pageNumber) => (