Skip to content

Commit 73fa809

Browse files
committed
fix: float popover position calculation
Signed-off-by: Innei <[email protected]>
1 parent bf8b3ab commit 73fa809

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

src/components/layout/footer/GatewayCount.tsx

+40-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

33
import { useQuery } from '@tanstack/react-query'
4+
import { sleep } from 'openai/core'
45

56
import { useOnlineCount } from '~/atoms'
67
import { useSocketIsConnect } from '~/atoms/hooks'
@@ -115,51 +116,46 @@ const RoomsInfo = () => {
115116
const { data } = useQuery({
116117
queryKey: ['rooms'],
117118
refetchOnMount: true,
118-
queryFn: () => {
119-
return apiClient.activity
120-
.getRoomsInfo()
121-
.then((res) => res.$serialized)
122-
.then((data) => {
123-
const result = [] as {
124-
path: string
125-
title: string
126-
count: number
127-
}[]
128-
129-
const morphArticleIdToRoomName = (id: string) => `article_${id}`
130-
data.objects.notes.forEach((note) => {
131-
result.push({
132-
path: routeBuilder(Routes.Note, {
133-
id: note.nid,
134-
}),
135-
title: note.title,
136-
count: data.roomCount[morphArticleIdToRoomName(note.id)],
137-
})
138-
})
139-
140-
data.objects.posts.forEach((post) => {
141-
result.push({
142-
path: routeBuilder(Routes.Post, {
143-
category: post.category.slug,
144-
slug: post.slug,
145-
}),
146-
title: post.title,
147-
count: data.roomCount[morphArticleIdToRoomName(post.id)],
148-
})
149-
})
150-
151-
data.objects.pages.forEach((page) => {
152-
result.push({
153-
path: routeBuilder(Routes.Page, {
154-
slug: page.slug,
155-
}),
156-
title: page.title,
157-
count: data.roomCount[morphArticleIdToRoomName(page.id)],
158-
})
159-
})
160-
161-
return result.sort((a, b) => b.count - a.count)
119+
staleTime: 1000 * 10,
120+
queryFn: async () => {
121+
await sleep(1000)
122+
const res = await apiClient.activity.getRoomsInfo()
123+
const data = res.$serialized
124+
const result = [] as {
125+
path: string
126+
title: string
127+
count: number
128+
}[]
129+
const morphArticleIdToRoomName = (id: string) => `article_${id}`
130+
data.objects.notes.forEach((note) => {
131+
result.push({
132+
path: routeBuilder(Routes.Note, {
133+
id: note.nid,
134+
}),
135+
title: note.title,
136+
count: data.roomCount[morphArticleIdToRoomName(note.id)],
137+
})
138+
})
139+
data.objects.posts.forEach((post) => {
140+
result.push({
141+
path: routeBuilder(Routes.Post, {
142+
category: post.category.slug,
143+
slug: post.slug,
144+
}),
145+
title: post.title,
146+
count: data.roomCount[morphArticleIdToRoomName(post.id)],
147+
})
148+
})
149+
data.objects.pages.forEach((page) => {
150+
result.push({
151+
path: routeBuilder(Routes.Page, {
152+
slug: page.slug,
153+
}),
154+
title: page.title,
155+
count: data.roomCount[morphArticleIdToRoomName(page.id)],
162156
})
157+
})
158+
return result.sort((a, b) => b.count - a.count)
163159
},
164160
})
165161

src/components/modules/note/NoteActionAside.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const NoteBottomBarAction: Component = () => {
3535
const isMobile = useIsMobile()
3636
if (!isMobile) return null
3737
return (
38-
<div className="mb-8 flex items-center justify-center space-x-8">
38+
<div className="mb-8 mt-4 flex items-center justify-center space-x-8">
3939
<LikeButton />
4040
<ShareButton />
4141
<SubscribeButton />

src/components/ui/float-popover/FloatPopover.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use client'
22

3-
import { flip, offset, shift, useFloating } from '@floating-ui/react-dom'
3+
import {
4+
autoUpdate,
5+
flip,
6+
offset,
7+
shift,
8+
useFloating,
9+
} from '@floating-ui/react-dom'
410
import React, {
511
createContext,
612
createElement,
@@ -92,7 +98,7 @@ export const FloatPopover = function FloatPopover<T extends {}>(
9298
} = props
9399

94100
const [open, setOpen] = useState(false)
95-
const { x, y, refs, strategy, isPositioned } = useFloating({
101+
const { x, y, refs, strategy, isPositioned, elements, update } = useFloating({
96102
middleware: floatingProps.middleware ?? [
97103
flip({ padding: padding ?? 20 }),
98104
offset(offsetValue ?? 10),
@@ -103,6 +109,13 @@ export const FloatPopover = function FloatPopover<T extends {}>(
103109
whileElementsMounted: floatingProps.whileElementsMounted,
104110
})
105111

112+
useEffect(() => {
113+
if (open && elements.reference && elements.floating) {
114+
const cleanup = autoUpdate(elements.reference, elements.floating, update)
115+
return cleanup
116+
}
117+
}, [open, elements, update])
118+
106119
const containerRef = useRef<HTMLDivElement>(null)
107120

108121
useClickAway(containerRef, () => {

src/components/ui/theme-switcher/ThemeSwitcher.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const ButtonGroup = () => {
121121
}
122122

123123
return (
124-
<div className="w-fit-content inline-flex rounded-full border border-slate-200 p-[3px] dark:border-neutral-800">
124+
<div className="w-fit-content inline-flex rounded-full border border-zinc-200 p-[3px] dark:border-zinc-700">
125125
<button
126126
aria-label="Switch to light theme"
127127
type="button"

0 commit comments

Comments
 (0)