From 66e3741948bddcfef6aed8b0f6e7e2d6e3a62cfb Mon Sep 17 00:00:00 2001 From: Peter Hozak Date: Sat, 10 Feb 2024 13:35:33 +0000 Subject: [PATCH] redirect ?state urls (#382 part 1) --- app/routes/_index.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/routes/_index.tsx b/app/routes/_index.tsx index d479daad..23205daa 100644 --- a/app/routes/_index.tsx +++ b/app/routes/_index.tsx @@ -1,6 +1,6 @@ import type {LoaderFunction} from '@remix-run/cloudflare' -import {ShouldRevalidateFunction, useOutletContext, useLoaderData} from '@remix-run/react' -import {loadTags} from '~/server-utils/stampy' +import {ShouldRevalidateFunction, useOutletContext, useLoaderData, redirect} from '@remix-run/react' +import {QuestionState, loadTags} from '~/server-utils/stampy' import Header from '~/components/Header' import Footer from '~/components/Footer' import type {Context} from '~/root' @@ -10,9 +10,22 @@ import {PageSubheaderText} from '~/components/PageSubHeader' import {ContentBoxMain, ContentBoxSecond, ContentBoxThird} from '~/components/ContentBox' import Grid from '~/components/Grid' import useToC from '~/hooks/useToC' +import {getStateEntries} from '~/hooks/stateModifiers' const empty: Awaited> = {data: [], timestamp: ''} export const loader = async ({request}: Parameters[0]) => { + const url = new URL(request.url) + const stateFromUrl = url.searchParams.get('state') + if (stateFromUrl) { + const firstOpenId = getStateEntries(stateFromUrl).filter( + ([_, state]) => state === QuestionState.OPEN + )[0]?.[0] + if (firstOpenId) { + url.searchParams.delete('state') + url.pathname = `/${firstOpenId}` + throw redirect(url.toString()) + } + } try { const tags = await loadTags(request) return {tags}