From ab35e2383d2b7efb8ed6af97a88bb41107c12140 Mon Sep 17 00:00:00 2001 From: Ou Date: Tue, 1 Oct 2024 13:01:01 +0800 Subject: [PATCH] chore: file structure --- src/components/{ => section}/Card.tsx | 184 +++++++++++--------------- src/components/{ => section}/Dnd.tsx | 38 +++++- src/components/{ => section}/Pure.tsx | 2 +- src/routes/index.tsx | 6 +- uno.config.ts | 3 +- 5 files changed, 119 insertions(+), 114 deletions(-) rename src/components/{ => section}/Card.tsx (76%) rename src/components/{ => section}/Dnd.tsx (70%) rename src/components/{ => section}/Pure.tsx (96%) diff --git a/src/components/Card.tsx b/src/components/section/Card.tsx similarity index 76% rename from src/components/Card.tsx rename to src/components/section/Card.tsx index 44b59d2..1838bfb 100644 --- a/src/components/Card.tsx +++ b/src/components/section/Card.tsx @@ -3,35 +3,51 @@ import type { UseQueryResult } from "@tanstack/react-query" import { useQuery } from "@tanstack/react-query" import { relativeTime } from "@shared/utils" import clsx from "clsx" -import { CSS } from "@dnd-kit/utilities" import { useInView } from "react-intersection-observer" import { useAtom } from "jotai" import { forwardRef, useCallback, useImperativeHandle, useRef } from "react" import { sources } from "@shared/data" import type { SyntheticListenerMap } from "@dnd-kit/core/dist/hooks/utilities" -import { useSortable } from "@dnd-kit/sortable" import { focusSourcesAtom, refetchSourceAtom } from "~/atoms" -interface ItemsProps extends React.HTMLAttributes { +export interface ItemsProps extends React.HTMLAttributes { id: SourceID - withOpacity?: boolean - isDragging?: boolean + /** + * 是否显示透明度,拖动时原卡片的样式 + */ + isDragged?: boolean + isOverlay?: boolean listeners?: SyntheticListenerMap } -export const CardWrapper = forwardRef(({ id, withOpacity, isDragging, listeners, style, ...props }, dndRef) => { +interface NewsCardProps { + id: SourceID + inView: boolean + isOverlay?: boolean + handleListeners?: SyntheticListenerMap +} + +interface Query { + query: UseQueryResult +} + +export const CardWrapper = forwardRef(({ id, isDragged, isOverlay, listeners, style, ...props }, dndRef) => { const ref = useRef(null) const { ref: inViewRef, inView } = useInView({ threshold: 0, }) - useImperativeHandle(dndRef, () => ref.current as HTMLDivElement) - useImperativeHandle(inViewRef, () => ref.current as HTMLDivElement) + useImperativeHandle(dndRef, () => ref.current!) + useImperativeHandle(inViewRef, () => ref.current!) return (
(({ id, withOpa }} {...props} > - +
) }) -export function SortableCardWrapper(props: ItemsProps) { - const { id } = props - const { - isDragging, - attributes, - listeners, - setNodeRef, - transform, - transition, - } = useSortable({ id }) +export function NewsCard({ id, inView, isOverlay, handleListeners }: NewsCardProps) { + const [focusSources, setFocusSources] = useAtom(focusSourcesAtom) + const [refetchSource, setRefetchSource] = useAtom(refetchSourceAtom) + const query = useQuery({ + queryKey: [id, refetchSource[id]], + queryFn: async ({ queryKey }) => { + const [_id, _refetchTime] = queryKey as [SourceID, number] + let url = `/api/${_id}` + if (Date.now() - _refetchTime < 1000) { + url = `/api/${_id}?latest` + } + const response = await fetch(url) + return await response.json() as SourceInfo + }, + // refetch 时显示原有的数据 + placeholderData: prev => prev, + staleTime: 1000 * 60 * 5, + enabled: inView, + refetchOnWindowFocus: true, + }) - const style = { - transform: CSS.Transform.toString(transform), - transition: transition || undefined, - } + const addFocusList = useCallback(() => { + setFocusSources(focusSources.includes(id) ? focusSources.filter(i => i !== id) : [...focusSources, id]) + }, [setFocusSources, focusSources, id]) + const manualRefetch = useCallback(() => { + setRefetchSource(prev => ({ + ...prev, + [id]: Date.now(), + })) + }, [setRefetchSource, id]) return ( - + <> +
+
+ {id} e.currentTarget.hidden = true} /> + + {sources[id].name} + +
+ +
+
+ +
+
+ +
+
+
+ ) } -interface NewsCardProps { - id: SourceID - inView: boolean - isDragging?: boolean - listeners?: SyntheticListenerMap -} - -interface Query { - query: UseQueryResult -} - function SubTitle({ query }: Query) { const subTitle = query.data?.type if (subTitle) return {subTitle} @@ -139,63 +175,3 @@ function NewsList({ query }: Query) { ) } - -export function NewsCard({ id, inView, isDragging, listeners }: NewsCardProps) { - const [focusSources, setFocusSources] = useAtom(focusSourcesAtom) - const [refetchSource, setRefetchSource] = useAtom(refetchSourceAtom) - const query = useQuery({ - queryKey: [id, refetchSource[id]], - queryFn: async ({ queryKey }) => { - const [_id, _refetchTime] = queryKey as [SourceID, number] - let url = `/api/${_id}` - if (Date.now() - _refetchTime < 1000) { - url = `/api/${_id}?latest` - } - const response = await fetch(url) - return await response.json() as SourceInfo - }, - // refetch 时显示原有的数据 - placeholderData: prev => prev, - staleTime: 1000 * 60 * 5, - enabled: inView, - refetchOnWindowFocus: true, - }) - - const addFocusList = useCallback(() => { - setFocusSources(focusSources.includes(id) ? focusSources.filter(i => i !== id) : [...focusSources, id]) - }, [setFocusSources, focusSources, id]) - const manualRefetch = useCallback(() => { - setRefetchSource(prev => ({ - ...prev, - [id]: Date.now(), - })) - }, [setRefetchSource, id]) - - return ( - <> -
-
- {id} e.currentTarget.hidden = true} /> - - {sources[id].name} - -
- -
-
- -
-
- -
-
-
- - ) -} diff --git a/src/components/Dnd.tsx b/src/components/section/Dnd.tsx similarity index 70% rename from src/components/Dnd.tsx rename to src/components/section/Dnd.tsx index e69a4d2..4c4f649 100644 --- a/src/components/Dnd.tsx +++ b/src/components/section/Dnd.tsx @@ -9,14 +9,16 @@ import { useSensor, useSensors, } from "@dnd-kit/core" -import { SortableContext, arrayMove, rectSortingStrategy } from "@dnd-kit/sortable" +import { SortableContext, arrayMove, rectSortingStrategy, useSortable } from "@dnd-kit/sortable" import { useAtom } from "jotai" import type { SourceID } from "@shared/types" +import { CSS } from "@dnd-kit/utilities" import { GridContainer } from "./Pure" -import { CardWrapper, SortableCardWrapper } from "./Card" +import type { ItemsProps } from "./Card" +import { CardWrapper } from "./Card" import { focusSourcesAtom } from "~/atoms" -export function Main() { +export function Dnd() { const [items, setItems] = useAtom(focusSourcesAtom) const [activeId, setActiveId] = useState(null) const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor)) @@ -58,8 +60,36 @@ export function Main() { - {!!activeId && } + {!!activeId && } ) } + +function SortableCardWrapper(props: ItemsProps) { + const { id } = props + const { + isDragging, + attributes, + listeners, + setNodeRef, + transform, + transition, + } = useSortable({ id }) + + const style = { + transform: CSS.Transform.toString(transform), + transition: transition || undefined, + } + + return ( + + ) +} diff --git a/src/components/Pure.tsx b/src/components/section/Pure.tsx similarity index 96% rename from src/components/Pure.tsx rename to src/components/section/Pure.tsx index 4b31dd0..44e9a40 100644 --- a/src/components/Pure.tsx +++ b/src/components/section/Pure.tsx @@ -16,7 +16,7 @@ export function GridContainer({ children }: PropsWithChildren) { ) } -export function Main() { +export function Pure() { const currentSection = useAtomValue(currentSectionAtom) return ( diff --git a/src/routes/index.tsx b/src/routes/index.tsx index fc0a994..ed91336 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -5,8 +5,8 @@ import clsx from "clsx" import { useSetAtom } from "jotai" import { useEffect } from "react" import { currentSectionAtom } from "~/atoms" -import { Main as DndMain } from "~/components/Dnd" -import { Main as PureMain } from "~/components/Pure" +import { Dnd } from "~/components/section/Dnd" +import { Pure } from "~/components/section/Pure" export const Route = createFileRoute("/")({ validateSearch: (search: any) => ({ @@ -37,7 +37,7 @@ function IndexComponent() { ))} { - id === "focus" ? : + id === "focus" ? : } ) diff --git a/uno.config.ts b/uno.config.ts index 2db36aa..6da12ab 100644 --- a/uno.config.ts +++ b/uno.config.ts @@ -15,8 +15,7 @@ export default defineConfig({ "bg-base": "bg-white dark:bg-[#1d1c1c]", "border-base": "border-gray-500/30", - "bg-tooltip": "bg-white:75 dark:bg-neutral-900:75 backdrop-blur-8", - "bg-glass": "bg-white:75 dark:bg-neutral-900:75 backdrop-blur-5", + "bg-glass": "bg-white:75 dark:bg-[#1d1c1c]:75 backdrop-blur-5", "bg-code": "bg-gray5:5", "bg-hover": "bg-primary-400:5",