Skip to content

Commit

Permalink
fix: jotai store and ui bg
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Mar 27, 2024
1 parent d7a26a9 commit 82690a1
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/app/(app)/(page-detail)/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/display-name */
import React from 'react'
import type { Metadata } from 'next'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ interface PageParams {
slug: string
}

// eslint-disable-next-line react/display-name
export default async (props: NextPageParams<PageParams>) => {
const data = await getData(props.params)

Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/header/internal/HeaderContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const AccessibleMenu: Component = () => {
initial={{ y: -20 }}
animate={{ y: 0 }}
exit={{ y: -20, opacity: 0 }}
className="fixed inset-x-0 top-12 z-10 flex justify-center duration-100"
className="pointer-events-none fixed inset-x-0 top-12 z-10 flex justify-center duration-100"
>
<ForDesktop />
</m.div>
Expand Down Expand Up @@ -110,7 +110,7 @@ const ForDesktop: Component<{
'shadow-lg shadow-zinc-800/5 ring-1 ring-zinc-900/5 backdrop-blur-md',
'dark:from-zinc-900/70 dark:to-zinc-800/90 dark:ring-zinc-100/10',
'group [--spotlight-color:oklch(var(--a)_/_0.12)]',
'duration-200',
'pointer-events-auto duration-200',
shouldHideNavBg && '!bg-none !shadow-none !ring-transparent',
className,
)}
Expand Down
26 changes: 7 additions & 19 deletions src/components/modules/activity/Presence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,21 @@ import { getColorScheme, stringToHue } from '~/lib/color'
import { formatSeconds } from '~/lib/datetime'
import { debounce, uniq } from '~/lib/lodash'
import { apiClient } from '~/lib/request'
import { springScrollTo } from '~/lib/scroller'
import {
useWrappedElementPosition,
useWrappedElementSize,
} from '~/providers/shared/WrappedElementProvider'
import { queries } from '~/queries/definition'
import { socketClient } from '~/socket'

import { commentStoragePrefix } from '../comment/CommentBox/providers'
import { useRoomContext } from './Room'

export const Presence = () => {
const isMobile = useIsMobile()

const isClient = useIsClient()

return isMobile ? null : isClient ? <PresenceImpl /> : null
return isClient ? <PresenceImpl /> : null
}

const PresenceImpl = () => {
const { roomName } = useRoomContext()

const isMobile = useIsMobile()
const { refetch } = useQuery({
...queries.activity.presence(roomName),

Expand Down Expand Up @@ -123,6 +116,8 @@ const PresenceImpl = () => {
update(percent)
}, [percent, update])

if (isMobile) return null

return <ReadPresenceTimeline />
}

Expand Down Expand Up @@ -216,14 +211,14 @@ const TimelineItem: FC<TimelineItemProps> = memo(({ type, identity }) => {
TimelineItem.displayName = 'TimelineItem'

const MoitonBar = forwardRef<
HTMLButtonElement,
HTMLDivElement,
{
position: number
bgColor: string
isCurrent: boolean
}
>(({ bgColor, isCurrent, position, ...rest }, ref) => {
const elRef = useRef<HTMLButtonElement>(null)
const elRef = useRef<HTMLDivElement>(null)

const [memoedPosition] = useState(position)
useLayoutEffect(() => {
Expand Down Expand Up @@ -258,17 +253,10 @@ const MoitonBar = forwardRef<
},
)
}, [isCurrent, position])
const { y } = useWrappedElementPosition()
const { h } = useWrappedElementSize()

useImperativeHandle(ref, () => elRef.current!)
return (
<button
onClick={() => {
// read percent calc: Math.floor(Math.min(Math.max(0, ((scrollTop - y) / h) * 100), 100)) || 0
// so the reversal is
springScrollTo(y + (position / 100) * h)
}}
<div
aria-label={isCurrent ? '你在这里' : `读者在这里 - ${position}%`}
ref={elRef}
className={clsx(
Expand Down
6 changes: 3 additions & 3 deletions src/components/modules/activity/Room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { createContext, useContext, useEffect, useMemo } from 'react'
import type { FC, PropsWithChildren } from 'react'

import { useSocketIsConnect } from '~/atoms/hooks'
import { useSocketIsConnect, useSocketSessionId } from '~/atoms/hooks'
import { socketClient } from '~/socket'
import { SocketEmitEnum } from '~/types/events'

Expand All @@ -18,7 +18,7 @@ export const RoomProvider: FC<
} & PropsWithChildren
> = ({ roomName, children }) => {
const socketIsConnect = useSocketIsConnect()

const identity = useSocketSessionId()
const ctxValue = useMemo(() => ({ roomName }), [roomName])
useEffect(() => {
if (!socketIsConnect) return
Expand All @@ -31,7 +31,7 @@ export const RoomProvider: FC<
roomName,
})
}
}, [roomName, socketIsConnect])
}, [roomName, socketIsConnect, identity])

return (
<RoomContext.Provider value={ctxValue}>{children}</RoomContext.Provider>
Expand Down
7 changes: 5 additions & 2 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { getGlobalStore } from 'jojoo'
import { setGlobalStore } from 'jojoo'
import { createStore } from 'jotai'

export const jotaiStore = getGlobalStore()
const store = createStore()
setGlobalStore(store)
export const jotaiStore = store
10 changes: 3 additions & 7 deletions src/providers/root/page-scroll-info-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { startTransition, useMemo, useRef } from 'react'
import { useMemo, useRef } from 'react'
import { useIsomorphicLayoutEffect } from 'foxact/use-isomorphic-layout-effect'
import { atom, useAtomValue, useSetAtom } from 'jotai'
import type { FC, PropsWithChildren } from 'react'
Expand All @@ -27,9 +27,6 @@ const ScrollDetector = () => {
const prevScrollY = useRef(0)
const setIsInteractiveOnceRef = useRef(false)

// const lastTime = useRef(0)
// const setScrollSpeed = useSetAtom(pageScrollSpeedAtom)

useIsomorphicLayoutEffect(() => {
const scrollHandler = throttle(
() => {
Expand All @@ -43,9 +40,8 @@ const ScrollDetector = () => {
prevScrollY.current - currentTop > 0 ? 'up' : 'down',
)
prevScrollY.current = currentTop
startTransition(() => {
setPageScrollLocation(prevScrollY.current)
})

setPageScrollLocation(prevScrollY.current)
},
16,
{
Expand Down

0 comments on commit 82690a1

Please sign in to comment.