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

284 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 dirRef = useRef<(-1) | 1> (1)
  17. const itemsRef = useRef<(HTMLAnchorElement | null)[]> ([])
  18. const navRef = useRef<HTMLDivElement | null> (null)
  19. const measure = () => {
  20. const nav = navRef.current
  21. const el = itemsRef.current[activeIdx]
  22. if (!(nav) || !(el) || activeIdx < 0)
  23. return
  24. const navRect = nav.getBoundingClientRect ()
  25. const elRect = el.getBoundingClientRect ()
  26. setHl ({ left: elRect.left - navRect.left,
  27. width: elRect.width,
  28. visible: true })
  29. }
  30. const [hl, setHl] = useState<{ left: number; width: number; visible: boolean }> ({
  31. left: 0,
  32. width: 0,
  33. visible: false })
  34. const [menuOpen, setMenuOpen] = useState (false)
  35. const [openItemIdx, setOpenItemIdx] = useState (-1)
  36. const [postCount, setPostCount] = useState<number | null> (null)
  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. if (activeIdx !== prevActiveIdxRef.current)
  69. {
  70. dirRef.current = activeIdx > prevActiveIdxRef.current ? 1 : -1
  71. prevActiveIdxRef.current = activeIdx
  72. }
  73. const dir = dirRef.current
  74. useLayoutEffect (() => {
  75. if (activeIdx < 0)
  76. return
  77. const raf = requestAnimationFrame (measure)
  78. const onResize = () => requestAnimationFrame (measure)
  79. addEventListener ('resize', onResize)
  80. return () => {
  81. cancelAnimationFrame (raf)
  82. removeEventListener ('resize', onResize)
  83. }
  84. }, [activeIdx])
  85. useEffect (() => {
  86. const unsubscribe = WikiIdBus.subscribe (setWikiId)
  87. return () => unsubscribe ()
  88. }, [])
  89. useEffect (() => {
  90. setMenuOpen (false)
  91. setOpenItemIdx (menu.findIndex (item => (
  92. location.pathname.startsWith (item.base || item.to))))
  93. }, [location])
  94. useEffect (() => {
  95. if (!(wikiId))
  96. return
  97. const fetchPostCount = async () => {
  98. try
  99. {
  100. const pageRes = await axios.get (`${ API_BASE_URL }/wiki/${ wikiId }`)
  101. const wikiPage = toCamel (pageRes.data as any, { deep: true }) as WikiPage
  102. const tagRes = await axios.get (`${ API_BASE_URL }/tags/name/${ wikiPage.title }`)
  103. const tag = toCamel (tagRes.data as any, { deep: true }) as Tag
  104. setPostCount (tag.postCount)
  105. }
  106. catch
  107. {
  108. setPostCount (0)
  109. }
  110. }
  111. fetchPostCount ()
  112. }, [wikiId])
  113. return (
  114. <>
  115. <nav className="px-3 flex justify-between items-center w-full min-h-[48px]
  116. bg-yellow-200 dark:bg-red-975 md:bg-yellow-50">
  117. <div className="flex items-center gap-2 h-full">
  118. <Link to="/"
  119. className="mx-4 text-xl font-bold text-pink-600 hover:text-pink-400
  120. dark:text-pink-300 dark:hover:text-pink-100">
  121. ぼざクリ タグ広場
  122. </Link>
  123. <div ref={navRef} className="relative hidden md:flex h-full items-center">
  124. <div aria-hidden
  125. className={cn ('absolute top-1/2 -translate-y-1/2 h-full',
  126. 'bg-yellow-200 dark:bg-red-950',
  127. 'transition-[transform,width] duration-200 ease-out')}
  128. style={{ width: hl.width,
  129. transform: `translate(${ hl.left }px, -50%)`,
  130. opacity: hl.visible ? 1 : 0 }}/>
  131. {menu.map ((item, i) => (
  132. <Link key={i}
  133. to={item.to}
  134. ref={el => {
  135. itemsRef.current[i] = el
  136. }}
  137. className={cn ('relative z-10 flex h-full items-center px-5',
  138. (i === openItemIdx) && 'font-bold')}>
  139. {item.name}
  140. </Link>))}
  141. </div>
  142. </div>
  143. <TopNavUser user={user}/>
  144. <a href="#"
  145. className="md:hidden ml-auto pr-4
  146. text-pink-600 hover:text-pink-400
  147. dark:text-pink-300 dark:hover:text-pink-100"
  148. onClick={ev => {
  149. ev.preventDefault ()
  150. setMenuOpen (!(menuOpen))
  151. }}>
  152. {menuOpen ? '×' : 'Menu'}
  153. </a>
  154. </nav>
  155. <div className="relative hidden md:flex bg-yellow-200 dark:bg-red-950
  156. items-center w-full min-h-[40px] overflow-hidden">
  157. <AnimatePresence initial={false} custom={dir}>
  158. <motion.div
  159. key={activeIdx}
  160. custom={dir}
  161. variants={{ enter: (d: -1 | 1) => ({ y: d * 24, opacity: 0 }),
  162. centre: { y: 0, opacity: 1 },
  163. exit: (d: -1 | 1) => ({ y: (-d) * 24, opacity: 0 }) }}
  164. className="absolute inset-0 flex items-center px-3"
  165. initial="enter"
  166. animate="centre"
  167. exit="exit"
  168. transition={{ duration: .2, 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>