From a8b3faad8b2895a84eb180bc2eebd01419224606 Mon Sep 17 00:00:00 2001 From: Andrew Marcuse Date: Wed, 31 Jul 2024 18:09:03 -0400 Subject: [PATCH] ADD: noscript support for random images (+ minor code cleanup) --- app/components/Navbar.tsx | 15 ---- app/routes/random[.]html.tsx | 73 ++++++++++---------- app/routes/view[.]html/BackgroundButtons.tsx | 1 - app/routes/view[.]html/DesktopToolbar.tsx | 2 +- app/routes/view[.]html/ExitButton.tsx | 4 +- app/routes/view[.]html/MobileToolbar.tsx | 5 +- 6 files changed, 41 insertions(+), 59 deletions(-) diff --git a/app/components/Navbar.tsx b/app/components/Navbar.tsx index 5dc08a9..23c67fc 100644 --- a/app/components/Navbar.tsx +++ b/app/components/Navbar.tsx @@ -2,12 +2,10 @@ import { Box, Flex, Text, - IconButton, HStack, useColorModeValue, Stack, } from "@chakra-ui/react"; -import { PiHouseBold } from "react-icons/pi"; import { Link as RemixLink } from "@remix-run/react"; @@ -30,19 +28,6 @@ export const Navbar = () => { align="center" bg={bg} > - - } - variant="ghost" - aria-label="SVG View" - /> - diff --git a/app/routes/random[.]html.tsx b/app/routes/random[.]html.tsx index 5a86d82..f45ec09 100644 --- a/app/routes/random[.]html.tsx +++ b/app/routes/random[.]html.tsx @@ -1,14 +1,15 @@ -import { useEffect } from "react"; -import { useNavigate, useSearchParams } from "@remix-run/react"; +import type { LoaderFunctionArgs } from "@remix-run/node"; +import { Await, useLoaderData } from "@remix-run/react"; import { Center, Flex, Spinner, Text, VStack } from "@chakra-ui/react"; +import { redirect } from "@remix-run/node"; +import { Suspense } from "react"; import { t } from "~/utils/i18n"; -export default function RandomImage() { - const [searchParams] = useSearchParams(); - const navigate = useNavigate(); - - let hostname = searchParams.get("src"); +export async function loader({ + request, +}: LoaderFunctionArgs) { + let hostname = new URL(request.url).searchParams.get('src'); if ( hostname == null || /^(([a-z0-9]|[a-z0-9][-a-z0-9]*[a-z0-9])\.)+([a-z]{2,})$/.exec(hostname) == @@ -16,41 +17,43 @@ export default function RandomImage() { ) { hostname = "logosear.ch"; } - useEffect(() => { - const abortController = new AbortController(); - (async () => { - try { - console.log(`fetching random image`); - const resp = await fetch(`https://${hostname}/api/random.json?max=1`, { - signal: abortController.signal, - }); - const data = await resp.json(); - setTimeout(() => { - console.log(`random image: ${data.results[0].url}`); - navigate( - `/view.html?url=${encodeURIComponent( - data.results[0].url - )}&zoom=max` - ); - }, 1000); - } catch (err) { - console.error(err); - if (err instanceof Error && err.name !== "AbortError") { - navigate(`/open.html?error=${encodeURIComponent(err.name)}`); - } - } - })(); + try { + //LATER: for testing await new Promise(r => setTimeout(r, 1000)) + const resp = await fetch(`https://${hostname}/api/random.json?max=1`); + const data = await resp.json(); + + return redirect(`/view.html?url=${encodeURIComponent(data.results[0].url)}&zoom=max`); + } catch (e: unknown) { + const err = e instanceof Error ? e : new Error(`An error occurred ${e}`); + return redirect(`/?error=${encodeURIComponent(err.message)}`); + } +} + +//LATER: neither is being shown... +export default function RandomImage() { + const { url } = useLoaderData(); + return ( + }> + + {(url) => } + + + ); +} - return () => abortController.abort(); - }, [hostname, navigate]); +type IProps = { + message: string; + color: string; +} +function FullPageSpinner({ color, message }: IProps) { return ( -
+
- {t("Loading...")} + {message}
diff --git a/app/routes/view[.]html/BackgroundButtons.tsx b/app/routes/view[.]html/BackgroundButtons.tsx index 3dec410..c1f94b3 100644 --- a/app/routes/view[.]html/BackgroundButtons.tsx +++ b/app/routes/view[.]html/BackgroundButtons.tsx @@ -1,4 +1,3 @@ -"use client"; import { PiCheckerboardFill, PiSquare, diff --git a/app/routes/view[.]html/DesktopToolbar.tsx b/app/routes/view[.]html/DesktopToolbar.tsx index 870297a..bcedc7d 100644 --- a/app/routes/view[.]html/DesktopToolbar.tsx +++ b/app/routes/view[.]html/DesktopToolbar.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { PiArrowSquareOutBold, PiBug } from "react-icons/pi"; +import { PiBug } from "react-icons/pi"; import { ButtonGroup, diff --git a/app/routes/view[.]html/ExitButton.tsx b/app/routes/view[.]html/ExitButton.tsx index eb2c007..4567c6d 100644 --- a/app/routes/view[.]html/ExitButton.tsx +++ b/app/routes/view[.]html/ExitButton.tsx @@ -1,8 +1,6 @@ -import { IconType } from "react-icons"; - import { Icon, IconButton } from "@chakra-ui/react"; -import { Link as RemixLink, useSearchParams } from "@remix-run/react"; +import { Link as RemixLink } from "@remix-run/react"; import { PiArrowSquareOutBold } from "react-icons/pi"; interface IProps { diff --git a/app/routes/view[.]html/MobileToolbar.tsx b/app/routes/view[.]html/MobileToolbar.tsx index 0fbe337..420f52d 100644 --- a/app/routes/view[.]html/MobileToolbar.tsx +++ b/app/routes/view[.]html/MobileToolbar.tsx @@ -1,6 +1,5 @@ -"use client"; import { useRef } from "react"; -import { PiArrowSquareOutBold, PiListBold, PiXBold } from "react-icons/pi"; +import { PiListBold, PiXBold } from "react-icons/pi"; import { Button, @@ -27,8 +26,6 @@ import { Link as RemixLink, useSearchParams } from "@remix-run/react"; import { t } from "~/utils/i18n"; -import { ToolbarButton } from "~/components/ToolbarButton"; - import { BackgroundButtons } from "./BackgroundButtons"; import { BorderButtons } from "./BorderButtons"; import { ExitButton } from "./ExitButton";