ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
9.1 KiB

  1. import { useQuery } from '@tanstack/react-query'
  2. import { AnimatePresence, motion } from 'framer-motion'
  3. import { Fragment, useEffect, useLayoutEffect, useRef, useState } from 'react'
  4. import { useLocation } from 'react-router-dom'
  5. import Separator from '@/components/MenuSeparator'
  6. import PrefetchLink from '@/components/PrefetchLink'
  7. import TopNavUser from '@/components/TopNavUser'
  8. import { WikiIdBus } from '@/lib/eventBus/WikiIdBus'
  9. import { tagsKeys, wikiKeys } from '@/lib/queryKeys'
  10. import { fetchTagByName } from '@/lib/tags'
  11. import { cn } from '@/lib/utils'
  12. import { fetchWikiPage } from '@/lib/wiki'
  13. import type { FC, MouseEvent } from 'react'
  14. import type { Menu, User } from '@/types'
  15. type Props = { user: User | null }
  16. export default (({ user }: Props) => {
  17. const location = useLocation ()
  18. const dirRef = useRef<(-1) | 1> (1)
  19. const itemsRef = useRef<(HTMLAnchorElement | null)[]> ([])
  20. const navRef = useRef<HTMLDivElement | null> (null)
  21. const measure = () => {
  22. const nav = navRef.current
  23. const el = itemsRef.current[activeIdx]
  24. if (!(nav) || !(el) || activeIdx < 0)
  25. return
  26. const navRect = nav.getBoundingClientRect ()
  27. const elRect = el.getBoundingClientRect ()
  28. setHl ({ left: elRect.left - navRect.left,
  29. width: elRect.width,
  30. visible: true })
  31. }
  32. const [hl, setHl] = useState<{ left: number; width: number; visible: boolean }> ({
  33. left: 0,
  34. width: 0,
  35. visible: false })
  36. const [menuOpen, setMenuOpen] = useState (false)
  37. const [openItemIdx, setOpenItemIdx] = useState (-1)
  38. const [wikiId, setWikiId] = useState<number | null> (WikiIdBus.get ())
  39. const wikiIdStr = String (wikiId ?? '')
  40. const { data: wikiPage } = useQuery ({
  41. enabled: Boolean (wikiIdStr),
  42. queryKey: wikiKeys.show (wikiIdStr, { }),
  43. queryFn: () => fetchWikiPage (wikiIdStr, { }) })
  44. const effectiveTitle = wikiPage?.title ?? ''
  45. const { data: tag } = useQuery ({
  46. enabled: Boolean (effectiveTitle),
  47. queryKey: tagsKeys.show (effectiveTitle),
  48. queryFn: () => fetchTagByName (effectiveTitle) })
  49. const postCount = tag?.postCount ?? 0
  50. const wikiPageFlg = Boolean (/^\/wiki\/(?!new|changes)[^\/]+/.test (location.pathname) && wikiId)
  51. const wikiTitle = location.pathname.split ('/')[2] ?? ''
  52. const menu: Menu = [
  53. { name: '広場', to: '/posts', subMenu: [
  54. { name: '一覧', to: '/posts' },
  55. { name: '検索', to: '/posts/search' },
  56. { name: '投稿追加', to: '/posts/new' },
  57. { name: '履歴', to: '/posts/changes' },
  58. { name: 'ヘルプ', to: '/wiki/ヘルプ:広場' }] },
  59. { name: 'タグ', to: '/tags', subMenu: [
  60. { name: 'タグ一覧', to: '/tags', visible: false },
  61. { name: '別名タグ', to: '/tags/aliases', visible: false },
  62. { name: '上位タグ', to: '/tags/implications', visible: false },
  63. { name: 'ニコニコ連携', to: '/tags/nico' },
  64. { name: 'ヘルプ', to: '/wiki/ヘルプ:タグ' }] },
  65. { name: 'Wiki', to: '/wiki/ヘルプ:ホーム', base: '/wiki', subMenu: [
  66. { name: '検索', to: '/wiki' },
  67. { name: '新規', to: '/wiki/new' },
  68. { name: '全体履歴', to: '/wiki/changes' },
  69. { name: 'ヘルプ', to: '/wiki/ヘルプ:Wiki' },
  70. { component: <Separator/>, visible: wikiPageFlg },
  71. { name: `広場 (${ postCount || 0 })`, to: `/posts?tags=${ wikiTitle }`,
  72. visible: wikiPageFlg },
  73. { name: '履歴', to: `/wiki/changes?id=${ wikiId }`, visible: wikiPageFlg },
  74. { name: '編輯', to: `/wiki/${ wikiId || wikiTitle }/edit`, visible: wikiPageFlg }] },
  75. { name: 'ユーザ', to: '/users', subMenu: [
  76. { name: '一覧', to: '/users', visible: false },
  77. { name: 'お前', to: `/users/${ user?.id }`, visible: false },
  78. { name: '設定', to: '/users/settings', visible: Boolean (user) }] }]
  79. const activeIdx = menu.findIndex (item => location.pathname.startsWith (item.base || item.to))
  80. const prevActiveIdxRef = useRef<number> (activeIdx)
  81. if (activeIdx !== prevActiveIdxRef.current)
  82. {
  83. dirRef.current = activeIdx > prevActiveIdxRef.current ? 1 : -1
  84. prevActiveIdxRef.current = activeIdx
  85. }
  86. const dir = dirRef.current
  87. useLayoutEffect (() => {
  88. if (activeIdx < 0)
  89. return
  90. const raf = requestAnimationFrame (measure)
  91. const onResize = () => requestAnimationFrame (measure)
  92. addEventListener ('resize', onResize)
  93. return () => {
  94. cancelAnimationFrame (raf)
  95. removeEventListener ('resize', onResize)
  96. }
  97. }, [activeIdx])
  98. useEffect (() => {
  99. const unsubscribe = WikiIdBus.subscribe (setWikiId)
  100. return () => unsubscribe ()
  101. }, [])
  102. useEffect (() => {
  103. setMenuOpen (false)
  104. setOpenItemIdx (menu.findIndex (item => (
  105. location.pathname.startsWith (item.base || item.to))))
  106. }, [location])
  107. return (
  108. <>
  109. <nav className="px-3 flex justify-between items-center w-full min-h-[48px]
  110. bg-yellow-200 dark:bg-red-975 md:bg-yellow-50">
  111. <div className="flex items-center gap-2 h-full">
  112. <PrefetchLink
  113. to="/posts"
  114. className="mx-4 text-xl font-bold text-pink-600 hover:text-pink-400
  115. dark:text-pink-300 dark:hover:text-pink-100"
  116. onClick={() => {
  117. scroll (0, 0)
  118. }}>
  119. ぼざクリ タグ広場
  120. </PrefetchLink>
  121. <div ref={navRef} className="relative hidden md:flex h-full items-center">
  122. <div aria-hidden
  123. className={cn ('absolute top-1/2 -translate-y-1/2 h-full',
  124. 'bg-yellow-200 dark:bg-red-950',
  125. 'transition-[transform,width] duration-200 ease-out')}
  126. style={{ width: hl.width,
  127. transform: `translate(${ hl.left }px, -50%)`,
  128. opacity: hl.visible ? 1 : 0 }}/>
  129. {menu.map ((item, i) => (
  130. <PrefetchLink
  131. key={i}
  132. to={item.to}
  133. ref={(el: (HTMLAnchorElement | null)) => {
  134. itemsRef.current[i] = el
  135. }}
  136. className={cn ('relative z-10 flex h-full items-center px-5',
  137. (i === openItemIdx) && 'font-bold')}>
  138. {item.name}
  139. </PrefetchLink>))}
  140. </div>
  141. </div>
  142. <TopNavUser user={user}/>
  143. <a href="#"
  144. className="md:hidden ml-auto pr-4
  145. text-pink-600 hover:text-pink-400
  146. dark:text-pink-300 dark:hover:text-pink-100"
  147. onClick={ev => {
  148. ev.preventDefault ()
  149. setMenuOpen (!(menuOpen))
  150. }}>
  151. {menuOpen ? '×' : 'Menu'}
  152. </a>
  153. </nav>
  154. <div className="relative hidden md:flex bg-yellow-200 dark:bg-red-950
  155. items-center w-full min-h-[40px] overflow-hidden">
  156. <AnimatePresence initial={false} custom={dir}>
  157. <motion.div
  158. key={activeIdx}
  159. custom={dir}
  160. variants={{ enter: (d: -1 | 1) => ({ y: d * 24, opacity: 0 }),
  161. centre: { y: 0, opacity: 1 },
  162. exit: (d: -1 | 1) => ({ y: (-d) * 24, opacity: 0 }) }}
  163. className="absolute inset-0 flex items-center px-3"
  164. initial="enter"
  165. animate="centre"
  166. exit="exit"
  167. transition={{ duration: .2, ease: 'easeOut' }}>
  168. {(menu[activeIdx]?.subMenu ?? [])
  169. .filter (item => item.visible ?? true)
  170. .map ((item, i) => (
  171. 'component' in item
  172. ? <Fragment key={`c-${ i }`}>{item.component}</Fragment>
  173. : (
  174. <PrefetchLink
  175. key={`l-${ i }`}
  176. to={item.to}
  177. className="h-full flex items-center px-3">
  178. {item.name}
  179. </PrefetchLink>)))}
  180. </motion.div>
  181. </AnimatePresence>
  182. </div>
  183. <AnimatePresence initial={false}>
  184. {menuOpen && (
  185. <motion.div
  186. key="spmenu"
  187. className={cn ('flex flex-col md:hidden',
  188. 'bg-yellow-200 dark:bg-red-975 items-start')}
  189. variants={{ closed: { clipPath: 'inset(0 0 100% 0)',
  190. height: 0 },
  191. open: { clipPath: 'inset(0 0 0% 0)',
  192. height: 'auto' } }}
  193. initial="closed"
  194. animate="open"
  195. exit="closed"
  196. transition={{ duration: .2, ease: 'easeOut' }}>
  197. <Separator/>
  198. {menu.map ((item, i) => (
  199. <Fragment key={i}>
  200. <PrefetchLink
  201. to={i === openItemIdx ? item.to : '#'}
  202. className={cn ('w-full min-h-[40px] flex items-center pl-8',
  203. ((i === openItemIdx)
  204. && 'font-bold bg-yellow-50 dark:bg-red-950'))}
  205. onClick={(ev: MouseEvent<HTMLAnchorElement>) => {
  206. if (i !== openItemIdx)
  207. {
  208. ev.preventDefault ()
  209. setOpenItemIdx (i)
  210. }
  211. }}>
  212. {item.name}
  213. </PrefetchLink>
  214. <AnimatePresence initial={false}>
  215. {i === openItemIdx && (
  216. <motion.div
  217. key={`sp-sub-${ i }`}
  218. className="w-full bg-yellow-50 dark:bg-red-950"
  219. variants={{ closed: { clipPath: 'inset(0 0 100% 0)',
  220. height: 0,
  221. opacity: 0 },
  222. open: { clipPath: 'inset(0 0 0% 0)',
  223. height: 'auto',
  224. opacity: 1 } }}
  225. initial="closed"
  226. animate="open"
  227. exit="closed"
  228. transition={{ duration: .2, ease: 'easeOut' }}>
  229. {item.subMenu
  230. .filter (subItem => subItem.visible ?? true)
  231. .map ((subItem, j) => (
  232. 'component' in subItem
  233. ? (
  234. <Fragment key={`sp-c-${ i }-${ j }`}>
  235. {subItem.component}
  236. </Fragment>)
  237. : (
  238. <PrefetchLink
  239. key={`sp-l-${ i }-${ j }`}
  240. to={subItem.to}
  241. className="w-full min-h-[36px] flex items-center pl-12">
  242. {subItem.name}
  243. </PrefetchLink>)))}
  244. </motion.div>)}
  245. </AnimatePresence>
  246. </Fragment>))}
  247. <TopNavUser user={user} sp/>
  248. <Separator/>
  249. </motion.div>)}
  250. </AnimatePresence>
  251. </>)
  252. }) satisfies FC<Props>