ぼざクリタグ広場 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.
 
 
 
 
 
 

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