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

283 lines
8.8 KiB

  1. import axios from 'axios'
  2. import toCamel from 'camelcase-keys'
  3. import { AnimatePresence, motion } from 'framer-motion'
  4. import { Fragment, useEffect, useLayoutEffect, useRef, useState } from 'react'
  5. import { Link, useLocation } from 'react-router-dom'
  6. import Separator from '@/components/MenuSeparator'
  7. import TopNavUser from '@/components/TopNavUser'
  8. import { API_BASE_URL } from '@/config'
  9. import { WikiIdBus } from '@/lib/eventBus/WikiIdBus'
  10. import { cn } from '@/lib/utils'
  11. import type { FC } from 'react'
  12. import type { Menu, Tag, User, WikiPage } from '@/types'
  13. type Props = { user: User | null }
  14. export default (({ user }: Props) => {
  15. const location = useLocation ()
  16. const itemRefs = useRef<(HTMLAnchorElement | null)[]> ([])
  17. const navRef = useRef<HTMLDivElement | null> (null)
  18. const measure = () => {
  19. const nav = navRef.current
  20. const el = itemRefs.current[activeIdx]
  21. if (!(nav) || !(el) || activeIdx < 0)
  22. return
  23. const navRect = nav.getBoundingClientRect ()
  24. const elRect = el.getBoundingClientRect ()
  25. setHl ({ left: elRect.left - navRect.left,
  26. width: elRect.width,
  27. visible: true })
  28. }
  29. const [hl, setHl] = useState<{ left: number; width: number; visible: boolean }> ({
  30. left: 0,
  31. width: 0,
  32. visible: false })
  33. const [menuOpen, setMenuOpen] = useState (false)
  34. const [openItemIdx, setOpenItemIdx] = useState (-1)
  35. const [postCount, setPostCount] = useState<number | null> (null)
  36. const [subDir, setSubDir] = useState<(-1) | 1> (1)
  37. const [wikiId, setWikiId] = useState<number | null> (WikiIdBus.get ())
  38. const wikiPageFlg = Boolean (/^\/wiki\/(?!new|changes)[^\/]+/.test (location.pathname) && wikiId)
  39. const wikiTitle = location.pathname.split ('/')[2]
  40. const menu: Menu = [
  41. { name: '広場', to: '/posts', subMenu: [
  42. { name: '一覧', to: '/posts' },
  43. { name: '投稿追加', to: '/posts/new' },
  44. { name: '耕作履歴', to: '/posts/changes' },
  45. { name: 'ヘルプ', to: '/wiki/ヘルプ:広場' }] },
  46. { name: 'タグ', to: '/tags', subMenu: [
  47. { name: 'タグ一覧', to: '/tags', visible: false },
  48. { name: '別名タグ', to: '/tags/aliases', visible: false },
  49. { name: '上位タグ', to: '/tags/implications', visible: false },
  50. { name: 'ニコニコ連携', to: '/tags/nico' },
  51. { name: 'ヘルプ', to: '/wiki/ヘルプ:タグ' }] },
  52. { name: 'Wiki', to: '/wiki/ヘルプ:ホーム', base: '/wiki', subMenu: [
  53. { name: '検索', to: '/wiki' },
  54. { name: '新規', to: '/wiki/new' },
  55. { name: '全体履歴', to: '/wiki/changes' },
  56. { name: 'ヘルプ', to: '/wiki/ヘルプ:Wiki' },
  57. { component: <Separator/>, visible: wikiPageFlg },
  58. { name: `広場 (${ postCount || 0 })`, to: `/posts?tags=${ wikiTitle }`,
  59. visible: wikiPageFlg },
  60. { name: '履歴', to: `/wiki/changes?id=${ wikiId }`, visible: wikiPageFlg },
  61. { name: '編輯', to: `/wiki/${ wikiId || wikiTitle }/edit`, visible: wikiPageFlg }] },
  62. { name: 'ユーザ', to: '/users', subMenu: [
  63. { name: '一覧', to: '/users', visible: false },
  64. { name: 'お前', to: `/users/${ user?.id }`, visible: false },
  65. { name: '設定', to: '/users/settings', visible: Boolean (user) }] }]
  66. const activeIdx = menu.findIndex (item => location.pathname.startsWith (item.base || item.to))
  67. const prevActiveIdxRef = useRef<number> (activeIdx)
  68. useLayoutEffect (() => {
  69. if (activeIdx < 0)
  70. return
  71. const raf = requestAnimationFrame (measure)
  72. const onResize = () => requestAnimationFrame (measure)
  73. addEventListener ('resize', onResize)
  74. return () => {
  75. cancelAnimationFrame (raf)
  76. removeEventListener ('resize', onResize)
  77. }
  78. }, [activeIdx])
  79. useEffect (() => {
  80. const unsubscribe = WikiIdBus.subscribe (setWikiId)
  81. return () => unsubscribe ()
  82. }, [])
  83. useEffect (() => {
  84. const prev = prevActiveIdxRef.current
  85. if (prev !== activeIdx)
  86. {
  87. setSubDir (activeIdx > prev ? 1 : -1)
  88. prevActiveIdxRef.current = activeIdx
  89. }
  90. }, [activeIdx])
  91. useEffect (() => {
  92. setMenuOpen (false)
  93. setOpenItemIdx (menu.findIndex (item => (
  94. location.pathname.startsWith (item.base || item.to))))
  95. }, [location])
  96. useEffect (() => {
  97. if (!(wikiId))
  98. return
  99. const fetchPostCount = async () => {
  100. try
  101. {
  102. const pageRes = await axios.get (`${ API_BASE_URL }/wiki/${ wikiId }`)
  103. const wikiPage = toCamel (pageRes.data as any, { deep: true }) as WikiPage
  104. const tagRes = await axios.get (`${ API_BASE_URL }/tags/name/${ wikiPage.title }`)
  105. const tag = toCamel (tagRes.data as any, { deep: true }) as Tag
  106. setPostCount (tag.postCount)
  107. }
  108. catch
  109. {
  110. setPostCount (0)
  111. }
  112. }
  113. fetchPostCount ()
  114. }, [wikiId])
  115. return (
  116. <>
  117. <nav className="px-3 flex justify-between items-center w-full min-h-[48px]
  118. bg-yellow-200 dark:bg-red-975 md:bg-yellow-50">
  119. <div className="flex items-center gap-2 h-full">
  120. <Link to="/"
  121. className="mx-4 text-xl font-bold text-pink-600 hover:text-pink-400
  122. dark:text-pink-300 dark:hover:text-pink-100">
  123. ぼざクリ タグ広場
  124. </Link>
  125. <div ref={navRef} className="relative hidden md:flex h-full items-center">
  126. <div aria-hidden
  127. className={cn ('absolute top-1/2 -translate-y-1/2 h-full rounded-md',
  128. 'bg-yellow-200 dark:bg-red-950',
  129. 'transition-[transform,width,opacity] duration-200 ease-out')}
  130. style={{ width: hl.width,
  131. transform: `translate(${ hl.left }px, -50%)`,
  132. opacity: hl.visible ? 1 : 0 }}/>
  133. {menu.map ((item, i) => (
  134. <Link key={i}
  135. to={item.to}
  136. ref={el => {
  137. itemRefs.current[i] = el
  138. }}
  139. className={cn ('relative z-10 flex h-full items-center px-5',
  140. ((i === openItemIdx)
  141. ? 'font-bold'
  142. : 'opacity-90 hover:opacity-100'))}>
  143. {item.name}
  144. </Link>))}
  145. </div>
  146. </div>
  147. <TopNavUser user={user}/>
  148. <a href="#"
  149. className="md:hidden ml-auto pr-4
  150. text-pink-600 hover:text-pink-400
  151. dark:text-pink-300 dark:hover:text-pink-100"
  152. onClick={ev => {
  153. ev.preventDefault ()
  154. setMenuOpen (!(menuOpen))
  155. }}>
  156. {menuOpen ? '×' : 'Menu'}
  157. </a>
  158. </nav>
  159. <div className="hidden md:flex bg-yellow-200 dark:bg-red-950
  160. items-center w-full min-h-[40px] px-3 overflow-hidden">
  161. <AnimatePresence mode="wait" initial={false}>
  162. <motion.div
  163. key={activeIdx}
  164. className="flex items-center"
  165. initial={{ y: subDir * 24, opacity: 0 }}
  166. animate={{ y: 0, opacity: 1 }}
  167. exit={{ y: subDir * 24, opacity: 0 }}
  168. transition={{ duration: .1, ease: 'easeOut' }}>
  169. {(menu[activeIdx]?.subMenu ?? [])
  170. .filter (item => item.visible ?? true)
  171. .map ((item, i) => (
  172. 'component' in item
  173. ? <Fragment key={`c-${ i }`}>{item.component}</Fragment>
  174. : (
  175. <Link key={`l-${ i }`}
  176. to={item.to}
  177. className="h-full flex items-center px-3">
  178. {item.name}
  179. </Link>)))}
  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. <Link 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 => {
  205. if (i !== openItemIdx)
  206. {
  207. ev.preventDefault ()
  208. setOpenItemIdx (i)
  209. }
  210. }}>
  211. {item.name}
  212. </Link>
  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. <Link key={`sp-l-${ i }-${ j }`}
  238. to={subItem.to}
  239. className="w-full min-h-[36px] flex items-center pl-12">
  240. {subItem.name}
  241. </Link>)))}
  242. </motion.div>)}
  243. </AnimatePresence>
  244. </Fragment>))}
  245. <TopNavUser user={user} sp/>
  246. <Separator/>
  247. </motion.div>)}
  248. </AnimatePresence>
  249. </>)
  250. }) satisfies FC<Props>