From ae242300d778ab42bac097719f17ab61cddeadf1 Mon Sep 17 00:00:00 2001 From: Ou Date: Sat, 5 Oct 2024 01:23:15 +0800 Subject: [PATCH] feat: 404 auto redirect to / --- src/routes/index.tsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 50477af..0faac7b 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -2,9 +2,9 @@ import { metadata, sectionIds } from "@shared/data" import type { SectionID } from "@shared/types" import { Link, createFileRoute } from "@tanstack/react-router" import clsx from "clsx" -import { useAtom } from "jotai" -import { useEffect } from "react" -import { currentSectionIDAtom } from "~/atoms" +import { useAtom, useAtomValue } from "jotai" +import { useEffect, useMemo } from "react" +import { currentSectionIDAtom, focusSourcesAtom } from "~/atoms" import { Dnd } from "~/components/section/Dnd" import { Pure } from "~/components/section/Pure" @@ -16,11 +16,23 @@ export const Route = createFileRoute("/")({ }) function IndexComponent() { - const { section: id = "focus" } = Route.useSearch() + const { section } = Route.useSearch() + const focusSources = useAtomValue(focusSourcesAtom) + const nav = Route.useNavigate() const [currentSectionID, setCurrentSectionID] = useAtom(currentSectionIDAtom) + const id = useMemo(() => { + if (sectionIds.includes(section)) return section + else return focusSources.length ? "focus" : "social" + }, [section, focusSources]) + useEffect(() => { setCurrentSectionID(id) - }, [setCurrentSectionID, id]) + nav({ + to: "/", + search: { section: id }, + replace: true, + }) + }, [setCurrentSectionID, id, nav]) return currentSectionID === id && (