chore: minor changes

This commit is contained in:
Ou 2024-10-26 00:35:24 +08:00
parent f66cee4333
commit 8919b6f969
5 changed files with 36 additions and 38 deletions

View File

@ -1,7 +1,18 @@
import ReactDOM from "react-dom/client" import ReactDOM from "react-dom/client"
import { RouterProvider } from "@tanstack/react-router" import { RouterProvider, createRouter } from "@tanstack/react-router"
import { QueryClientProvider } from "@tanstack/react-query" import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { queryClient, router } from "./main" import { routeTree } from "./routeTree.gen"
const queryClient = new QueryClient()
const router = createRouter({
routeTree,
context: {
queryClient,
},
defaultPreload: "intent",
defaultPreloadStaleTime: 0,
})
const rootElement = document.getElementById("app")! const rootElement = document.getElementById("app")!
@ -13,3 +24,9 @@ if (!rootElement.innerHTML) {
</QueryClientProvider>, </QueryClientProvider>,
) )
} }
declare module "@tanstack/react-router" {
interface Register {
router: typeof router
}
}

View File

@ -138,7 +138,7 @@ function NewsCard({ id, inView, handleListeners }: NewsCardProps) {
<OverlayScrollbar <OverlayScrollbar
className={clsx([ className={clsx([
"h-full p-2 overflow-x-auto rounded-2xl bg-base bg-op-70!", "h-full p-2 overflow-y-auto rounded-2xl bg-base bg-op-70!",
isFreshFetching && `animate-pulse`, isFreshFetching && `animate-pulse`,
`sprinkle-${sources[id].color}`, `sprinkle-${sources[id].color}`,
])} ])}

View File

@ -1,9 +1,10 @@
import type { UseOverlayScrollbarsParams } from "overlayscrollbars-react" import type { UseOverlayScrollbarsParams } from "overlayscrollbars-react"
import { useOverlayScrollbars } from "overlayscrollbars-react" import { useOverlayScrollbars } from "overlayscrollbars-react"
import type { HTMLProps, PropsWithChildren } from "react" import type { HTMLProps, PropsWithChildren } from "react"
import { useCallback, useEffect, useMemo, useRef } from "react" import { useCallback, useMemo, useRef } from "react"
import { defu } from "defu" import { defu } from "defu"
import { useSetAtom } from "jotai" import { useSetAtom } from "jotai"
import { useMount } from "react-use"
import { goToTopAtom } from "~/atoms" import { goToTopAtom } from "~/atoms"
type Props = HTMLProps<HTMLDivElement> & UseOverlayScrollbarsParams type Props = HTMLProps<HTMLDivElement> & UseOverlayScrollbarsParams
@ -26,17 +27,17 @@ export function OverlayScrollbar({ disabled, children, options, events, defer, .
const [initialize] = useOverlayScrollbars(scrollbarParams) const [initialize] = useOverlayScrollbars(scrollbarParams)
useEffect(() => { useMount(() => {
if (!disabled) { if (!disabled) {
initialize({ initialize({
target: ref.current!, target: ref.current!,
cancel: { cancel: {
// 如果浏览器原生滚动条是覆盖在元素上的,则取消初始化 // 如果浏览器原生滚动条是覆盖在元素上的,则取消初始化
nativeScrollbarsOverlaid: true, nativeScrollbarsOverlaid: true,
}, },
}) })
} }
}, [initialize, disabled]) })
return ( return (
<div ref={ref} {...props}> <div ref={ref} {...props}>
@ -80,16 +81,13 @@ export function GlobalOverlayScrollbar({ children, ...props }: PropsWithChildren
defer: false, defer: false,
}) })
useEffect(() => { useMount(() => {
initialize({ initialize({
target: ref.current!, target: ref.current!,
cancel: { cancel: {
nativeScrollbarsOverlaid: true, nativeScrollbarsOverlaid: true,
}, },
}) })
}, [initialize])
useEffect(() => {
const el = ref.current const el = ref.current
if (el) { if (el) {
ref.current?.addEventListener("scroll", onScroll) ref.current?.addEventListener("scroll", onScroll)
@ -97,7 +95,7 @@ export function GlobalOverlayScrollbar({ children, ...props }: PropsWithChildren
el?.removeEventListener("scroll", onScroll) el?.removeEventListener("scroll", onScroll)
} }
} }
}, [onScroll]) })
return ( return (
<div ref={ref} {...props}> <div ref={ref} {...props}>

View File

@ -1,14 +1,17 @@
import { useEffect, useMemo } from "react" import { useMemo } from "react"
import { useLocalStorage, useMedia } from "react-use" import { useMedia, useUpdateEffect } from "react-use"
import { atomWithStorage } from "jotai/utils"
import { useAtom } from "jotai"
export declare type ColorScheme = "dark" | "light" | "auto" export declare type ColorScheme = "dark" | "light" | "auto"
export function useDark(key = "color-scheme", defaultColorScheme: ColorScheme = "auto") { const colorSchemeAtom = atomWithStorage("color-scheme", "auto")
const [colorScheme, setColorScheme] = useLocalStorage(key, defaultColorScheme) export function useDark() {
const [colorScheme, setColorScheme] = useAtom(colorSchemeAtom)
const prefersDarkMode = useMedia("(prefers-color-scheme: dark)") const prefersDarkMode = useMedia("(prefers-color-scheme: dark)")
const isDark = useMemo(() => colorScheme === "auto" ? prefersDarkMode : colorScheme === "dark", [colorScheme, prefersDarkMode]) const isDark = useMemo(() => colorScheme === "auto" ? prefersDarkMode : colorScheme === "dark", [colorScheme, prefersDarkMode])
useEffect(() => { useUpdateEffect(() => {
document.documentElement.classList.toggle("dark", isDark) document.documentElement.classList.toggle("dark", isDark)
}, [isDark]) }, [isDark])

View File

@ -1,20 +0,0 @@
import { QueryClient } from "@tanstack/react-query"
import { createRouter } from "@tanstack/react-router"
import { routeTree } from "./routeTree.gen"
export const queryClient = new QueryClient()
export const router = createRouter({
routeTree,
context: {
queryClient,
},
defaultPreload: "intent",
defaultPreloadStaleTime: 0,
})
declare module "@tanstack/react-router" {
interface Register {
router: typeof router
}
}