InstallInstructions.tsx (2005B)
1 import { useIntl } from "react-intl"; 2 import { useEffect, useState } from "react"; 3 import { ErrorDialog } from "../../ErrorDialog"; 4 import { usePurpleLoginSession } from "@/hooks/usePurpleLoginSession"; 5 import { useLocalStorage } from "usehooks-ts"; 6 import { NotedeckInstallLayout } from "@/components/NotedeckInstallLayout"; 7 import { MarkdownView } from "@/components/ui/MarkdownView"; 8 9 export function InstallInstructions() { 10 const intl = useIntl() 11 const [error, setError] = useState<string | null>(null) 12 const { accountInfo: loggedInAccountInfo, logout } = usePurpleLoginSession(setError) 13 const [sessionToken, setSessionToken] = useLocalStorage('session_token', null); 14 const [installInstructions, setInstallInstructions] = useState<string | undefined | null>(undefined) 15 16 // MARK: - Functions 17 18 const fetchInstallInstructions = async () => { 19 const response = await fetch(process.env.NEXT_PUBLIC_PURPLE_API_BASE_URL + "/notedeck-install-instructions", { 20 method: 'GET', 21 headers: { 22 'Content-Type': 'application/json', 23 'Authorization': 'Bearer ' + sessionToken 24 }, 25 }); 26 27 if (!response.ok) { 28 setError("Failed to get Notedeck install instructions from our servers. Please wait a few minutes and refresh the page. If the problem persists, please contact support."); 29 return; 30 } 31 32 const installInstructions = await response.json(); 33 setInstallInstructions(installInstructions.value); 34 } 35 36 useEffect(() => { 37 if (loggedInAccountInfo) { 38 fetchInstallInstructions(); 39 } 40 else if (loggedInAccountInfo === null) { 41 // Redirect to the login page 42 window.location.href = "/purple/login?redirect=" + encodeURIComponent("/notedeck/install") 43 } 44 }, [loggedInAccountInfo]) 45 46 // MARK: - Render 47 48 return (<> 49 <ErrorDialog error={error} setError={setError} /> 50 <NotedeckInstallLayout> 51 <MarkdownView className="text-purple-200">{installInstructions}</MarkdownView> 52 </NotedeckInstallLayout> 53 </>) 54 }