chore: rename section to column

This commit is contained in:
Ou 2024-10-13 14:35:42 +08:00
parent 5fe9a509d5
commit c4ce8772a4
11 changed files with 81 additions and 81 deletions

View File

@ -1,6 +1,6 @@
import type { Metadata } from "./types" import type { Metadata } from "./types"
export const sectionIds = ["focus", "realtime", "hottest", "china", "world", "tech", "finance"] as const export const columnIds = ["focus", "realtime", "hottest", "china", "world", "tech", "finance"] as const
export const metadata: Metadata = { export const metadata: Metadata = {
focus: { focus: {

View File

@ -1,5 +1,5 @@
import type { colors } from "unocss/preset-mini" import type { colors } from "unocss/preset-mini"
import type { sectionIds } from "./data" import type { columnIds } from "./data"
import type { originSources } from "./sources" import type { originSources } from "./sources"
export type Color = Exclude<keyof typeof colors, "current" | "inherit" | "transparent" | "black" | "white"> export type Color = Exclude<keyof typeof colors, "current" | "inherit" | "transparent" | "black" | "white">
@ -14,8 +14,8 @@ export type SourceID = {
} | Key : Key; } | Key : Key;
}[MainSourceID] }[MainSourceID]
export type SectionID = (typeof sectionIds)[number] export type ColumnID = (typeof columnIds)[number]
export type Metadata = Record<SectionID, Section> export type Metadata = Record<ColumnID, Section>
export interface OriginSource { export interface OriginSource {
name: string name: string

View File

@ -1,12 +1,12 @@
import { atom } from "jotai" import { atom } from "jotai"
import type { SectionID, SourceID } from "@shared/types" import type { ColumnID, SourceID } from "@shared/types"
import { metadata } from "@shared/data" import { metadata } from "@shared/data"
import { sources } from "@shared/sources" import { sources } from "@shared/sources"
import { typeSafeObjectEntries, typeSafeObjectFromEntries } from "@shared/type.util" import { typeSafeObjectEntries, typeSafeObjectFromEntries } from "@shared/type.util"
import { atomWithLocalStorage } from "./hooks/atomWithLocalStorage" import { atomWithLocalStorage } from "./hooks/atomWithLocalStorage"
const initialSources = typeSafeObjectFromEntries(typeSafeObjectEntries(metadata).map(([id, val]) => [id, val.sources])) const initialSources = typeSafeObjectFromEntries(typeSafeObjectEntries(metadata).map(([id, val]) => [id, val.sources]))
export const localSourcesAtom = atomWithLocalStorage<Record<SectionID, SourceID[]>>("localsources", () => { export const localSourcesAtom = atomWithLocalStorage<Record<ColumnID, SourceID[]>>("localsources", () => {
return initialSources return initialSources
}, (stored) => { }, (stored) => {
return typeSafeObjectFromEntries(typeSafeObjectEntries({ return typeSafeObjectFromEntries(typeSafeObjectEntries({
@ -45,16 +45,16 @@ function initRefetchSources() {
export const refetchSourcesAtom = atom(initRefetchSources()) export const refetchSourcesAtom = atom(initRefetchSources())
export const currentSectionIDAtom = atom<SectionID>("focus") export const currentColumnIDAtom = atom<ColumnID>("focus")
export const currentSectionAtom = atom((get) => { export const currentColumnAtom = atom((get) => {
const id = get(currentSectionIDAtom) const id = get(currentColumnIDAtom)
return get(localSourcesAtom)[id] return get(localSourcesAtom)[id]
}, (get, set, update: Update<SourceID[]>) => { }, (get, set, update: Update<SourceID[]>) => {
const _ = update instanceof Function ? update(get(currentSectionAtom)) : update const _ = update instanceof Function ? update(get(currentColumnAtom)) : update
set(localSourcesAtom, { set(localSourcesAtom, {
...get(localSourcesAtom), ...get(localSourcesAtom),
[get(currentSectionIDAtom)]: _, [get(currentColumnIDAtom)]: _,
}) })
}) })

View File

@ -17,10 +17,10 @@ import { CSS } from "@dnd-kit/utilities"
import { motion } from "framer-motion" import { motion } from "framer-motion"
import type { ItemsProps } from "./card" import type { ItemsProps } from "./card"
import { CardOverlay, CardWrapper } from "./card" import { CardOverlay, CardWrapper } from "./card"
import { currentSectionAtom } from "~/atoms" import { currentColumnAtom } from "~/atoms"
export function Dnd() { export function Dnd() {
const [items, setItems] = useAtom(currentSectionAtom) const [items, setItems] = useAtom(currentColumnAtom)
return ( return (
<DndWrapper items={items} setItems={setItems}> <DndWrapper items={items} setItems={setItems}>
<motion.div <motion.div

View File

@ -1,17 +1,17 @@
import { metadata, sectionIds } from "@shared/data" import { columnIds, metadata } from "@shared/data"
import type { SectionID } from "@shared/types" import type { ColumnID } from "@shared/types"
import { Link } from "@tanstack/react-router" import { Link } from "@tanstack/react-router"
import clsx from "clsx" import clsx from "clsx"
import { useAtom } from "jotai" import { useAtom } from "jotai"
import { useEffect } from "react" import { useEffect } from "react"
import { Dnd } from "./dnd" import { Dnd } from "./dnd"
import { currentSectionIDAtom } from "~/atoms" import { currentColumnIDAtom } from "~/atoms"
export function Section({ id }: { id: SectionID }) { export function Column({ id }: { id: ColumnID }) {
const [currentSectionID, setCurrentSectionID] = useAtom(currentSectionIDAtom) const [currentColumnID, setCurrentColumnID] = useAtom(currentColumnIDAtom)
useEffect(() => { useEffect(() => {
setCurrentSectionID(id) setCurrentColumnID(id)
}, [id, setCurrentSectionID]) }, [id, setCurrentColumnID])
return ( return (
<> <>
@ -21,22 +21,22 @@ export function Section({ id }: { id: SectionID }) {
"md:(z-100 mb-6)", "md:(z-100 mb-6)",
])} ])}
> >
{sectionIds.map(section => ( {columnIds.map(columnId => (
<Link <Link
key={section} key={columnId}
to="/s/$section" to="/s/$column"
params={{ section }} params={{ column: columnId }}
className={clsx( className={clsx(
"text-sm", "text-sm",
id === section ? "color-primary font-bold" : "op-70 dark:op-90", id === columnId ? "color-primary font-bold" : "op-70 dark:op-90",
)} )}
> >
{metadata[section].name} {metadata[columnId].name}
</Link> </Link>
))} ))}
</span> </span>
</div> </div>
{ currentSectionID === id && <Dnd />} { currentColumnID === id && <Dnd />}
</> </>
) )
} }

View File

@ -7,7 +7,7 @@ import type { SourceID } from "@shared/types"
import { Homepage, Version } from "@shared/consts" import { Homepage, Version } from "@shared/consts"
import logo from "~/assets/icon.svg" import logo from "~/assets/icon.svg"
import { useDark } from "~/hooks/useDark" import { useDark } from "~/hooks/useDark"
import { currentSectionAtom, goToTopAtom, refetchSourcesAtom } from "~/atoms" import { currentColumnAtom, goToTopAtom, refetchSourcesAtom } from "~/atoms"
function ThemeToggle() { function ThemeToggle() {
const { toggleDark } = useDark() const { toggleDark } = useDark()
@ -38,19 +38,19 @@ export function GithubIcon() {
} }
function RefreshButton() { function RefreshButton() {
const currentSection = useAtomValue(currentSectionAtom) const currentColumn = useAtomValue(currentColumnAtom)
const setRefetchSource = useSetAtom(refetchSourcesAtom) const setRefetchSource = useSetAtom(refetchSourcesAtom)
const refreshAll = useCallback(() => { const refreshAll = useCallback(() => {
const obj = Object.fromEntries(currentSection.map(id => [id, Date.now()])) const obj = Object.fromEntries(currentColumn.map(id => [id, Date.now()]))
setRefetchSource(prev => ({ setRefetchSource(prev => ({
...prev, ...prev,
...obj, ...obj,
})) }))
}, [currentSection, setRefetchSource]) }, [currentColumn, setRefetchSource])
const isFetching = useIsFetching({ const isFetching = useIsFetching({
predicate: (query) => { predicate: (query) => {
return currentSection.includes(query.queryKey[0] as SourceID) return currentColumn.includes(query.queryKey[0] as SourceID)
}, },
}) })

View File

@ -12,7 +12,7 @@
import { Route as rootRoute } from './routes/__root' import { Route as rootRoute } from './routes/__root'
import { Route as IndexImport } from './routes/index' import { Route as IndexImport } from './routes/index'
import { Route as SSectionImport } from './routes/s.$section' import { Route as SColumnImport } from './routes/s.$column'
// Create/Update Routes // Create/Update Routes
@ -21,8 +21,8 @@ const IndexRoute = IndexImport.update({
getParentRoute: () => rootRoute, getParentRoute: () => rootRoute,
} as any) } as any)
const SSectionRoute = SSectionImport.update({ const SColumnRoute = SColumnImport.update({
path: '/s/$section', path: '/s/$column',
getParentRoute: () => rootRoute, getParentRoute: () => rootRoute,
} as any) } as any)
@ -37,11 +37,11 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof IndexImport preLoaderRoute: typeof IndexImport
parentRoute: typeof rootRoute parentRoute: typeof rootRoute
} }
'/s/$section': { '/s/$column': {
id: '/s/$section' id: '/s/$column'
path: '/s/$section' path: '/s/$column'
fullPath: '/s/$section' fullPath: '/s/$column'
preLoaderRoute: typeof SSectionImport preLoaderRoute: typeof SColumnImport
parentRoute: typeof rootRoute parentRoute: typeof rootRoute
} }
} }
@ -51,37 +51,37 @@ declare module '@tanstack/react-router' {
export interface FileRoutesByFullPath { export interface FileRoutesByFullPath {
'/': typeof IndexRoute '/': typeof IndexRoute
'/s/$section': typeof SSectionRoute '/s/$column': typeof SColumnRoute
} }
export interface FileRoutesByTo { export interface FileRoutesByTo {
'/': typeof IndexRoute '/': typeof IndexRoute
'/s/$section': typeof SSectionRoute '/s/$column': typeof SColumnRoute
} }
export interface FileRoutesById { export interface FileRoutesById {
__root__: typeof rootRoute __root__: typeof rootRoute
'/': typeof IndexRoute '/': typeof IndexRoute
'/s/$section': typeof SSectionRoute '/s/$column': typeof SColumnRoute
} }
export interface FileRouteTypes { export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '/s/$section' fullPaths: '/' | '/s/$column'
fileRoutesByTo: FileRoutesByTo fileRoutesByTo: FileRoutesByTo
to: '/' | '/s/$section' to: '/' | '/s/$column'
id: '__root__' | '/' | '/s/$section' id: '__root__' | '/' | '/s/$column'
fileRoutesById: FileRoutesById fileRoutesById: FileRoutesById
} }
export interface RootRouteChildren { export interface RootRouteChildren {
IndexRoute: typeof IndexRoute IndexRoute: typeof IndexRoute
SSectionRoute: typeof SSectionRoute SColumnRoute: typeof SColumnRoute
} }
const rootRouteChildren: RootRouteChildren = { const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute, IndexRoute: IndexRoute,
SSectionRoute: SSectionRoute, SColumnRoute: SColumnRoute,
} }
export const routeTree = rootRoute export const routeTree = rootRoute
@ -97,14 +97,14 @@ export const routeTree = rootRoute
"filePath": "__root.tsx", "filePath": "__root.tsx",
"children": [ "children": [
"/", "/",
"/s/$section" "/s/$column"
] ]
}, },
"/": { "/": {
"filePath": "index.tsx" "filePath": "index.tsx"
}, },
"/s/$section": { "/s/$column": {
"filePath": "s.$section.tsx" "filePath": "s.$column.tsx"
} }
} }
} }

View File

@ -2,7 +2,7 @@ import { createFileRoute } from "@tanstack/react-router"
import { useAtomValue } from "jotai" import { useAtomValue } from "jotai"
import { useMemo } from "react" import { useMemo } from "react"
import { localSourcesAtom } from "~/atoms" import { localSourcesAtom } from "~/atoms"
import { Section } from "~/components/section" import { Column } from "~/components/column"
export const Route = createFileRoute("/")({ export const Route = createFileRoute("/")({
component: IndexComponent, component: IndexComponent,
@ -14,5 +14,5 @@ function IndexComponent() {
return focusSources.focus.length ? "focus" : "realtime" return focusSources.focus.length ? "focus" : "realtime"
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) }, [])
return <Section id={id} /> return <Column id={id} />
} }

28
src/routes/s.$column.tsx Normal file
View File

@ -0,0 +1,28 @@
import { createFileRoute, redirect } from "@tanstack/react-router"
import { columnIds } from "@shared/data"
import { Column } from "~/components/column"
export const Route = createFileRoute("/s/$column")({
component: SectionComponent,
params: {
parse: (params) => {
const column = columnIds.find(x => x === params.column.toLowerCase())
if (!column)
throw new Error(`"${params.column}" is not a valid column.`)
return {
column,
}
},
stringify: params => params,
},
onError: (error) => {
if (error?.routerCode === "PARSE_PARAMS") {
throw redirect({ to: "/" })
}
},
})
function SectionComponent() {
const { column } = Route.useParams()
return <Column id={column} />
}

View File

@ -1,28 +0,0 @@
import { createFileRoute, redirect } from "@tanstack/react-router"
import { sectionIds } from "@shared/data"
import { Section } from "~/components/section"
export const Route = createFileRoute("/s/$section")({
component: SectionComponent,
params: {
parse: (params) => {
const section = sectionIds.find(x => x === params.section.toLowerCase())
if (!section)
throw new Error(`"${params.section}" is not a valid section.`)
return {
section,
}
},
stringify: params => params,
},
onError: (error) => {
if (error?.routerCode === "PARSE_PARAMS") {
throw redirect({ to: "/" })
}
},
})
function SectionComponent() {
const { section } = Route.useParams()
return <Section id={section} />
}