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

161 lines
4.6 KiB

  1. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
  2. import { motion } from 'framer-motion'
  3. import { useEffect, useRef, useState } from 'react'
  4. import { Helmet } from 'react-helmet-async'
  5. import { useParams } from 'react-router-dom'
  6. import PostEditForm from '@/components/PostEditForm'
  7. import PostEmbed from '@/components/PostEmbed'
  8. import PostList from '@/components/PostList'
  9. import TagDetailSidebar from '@/components/TagDetailSidebar'
  10. import TabGroup, { Tab } from '@/components/common/TabGroup'
  11. import MainArea from '@/components/layout/MainArea'
  12. import { Button } from '@/components/ui/button'
  13. import { toast } from '@/components/ui/use-toast'
  14. import { SITE_TITLE } from '@/config'
  15. import { fetchPost, toggleViewedFlg } from '@/lib/posts'
  16. import { postsKeys } from '@/lib/queryKeys'
  17. import { cn } from '@/lib/utils'
  18. import NotFound from '@/pages/NotFound'
  19. import ServiceUnavailable from '@/pages/ServiceUnavailable'
  20. import type { FC } from 'react'
  21. import type { NiconicoViewerHandle, User } from '@/types'
  22. type Props = { user: User | null }
  23. export default (({ user }: Props) => {
  24. const { id } = useParams ()
  25. const postId = String (id ?? '')
  26. const postKey = postsKeys.show (postId)
  27. const { data: post, isError: errorFlg, error } = useQuery ({
  28. enabled: Boolean (id),
  29. queryKey: postKey,
  30. queryFn: () => fetchPost (postId) })
  31. const qc = useQueryClient ()
  32. const embedRef = useRef<NiconicoViewerHandle> (null)
  33. const [status, setStatus] = useState (200)
  34. const changeViewedFlg = useMutation ({
  35. mutationFn: async () => {
  36. const cur = qc.getQueryData<any> (postKey)
  37. const next = !(cur?.viewed)
  38. await toggleViewedFlg (postId, next)
  39. return next
  40. },
  41. onMutate: async () => {
  42. await qc.cancelQueries ({ queryKey: postKey })
  43. const prev = qc.getQueryData<any> (postKey)
  44. qc.setQueryData (postKey,
  45. (cur: any) => cur ? { ...cur, viewed: !(cur.viewed) } : cur)
  46. return { prev }
  47. },
  48. onError: (...[, , ctx]) => {
  49. if (ctx?.prev)
  50. qc.setQueryData (postKey, ctx.prev)
  51. toast ({ title: '失敗……', description: '通信に失敗しました……' })
  52. },
  53. onSuccess: () => {
  54. qc.invalidateQueries ({ queryKey: postsKeys.root })
  55. } })
  56. useEffect (() => {
  57. if (!(errorFlg))
  58. return
  59. const code = (error as any)?.response.status ?? (error as any)?.status
  60. if (code)
  61. setStatus (code)
  62. }, [errorFlg, error])
  63. useEffect (() => {
  64. scroll (0, 0)
  65. setStatus (200)
  66. }, [id])
  67. switch (status)
  68. {
  69. case 404:
  70. return <NotFound/>
  71. case 503:
  72. return <ServiceUnavailable/>
  73. }
  74. const viewedClass = (post?.viewed
  75. ? 'bg-blue-600 hover:bg-blue-700'
  76. : 'bg-gray-500 hover:bg-gray-600')
  77. return (
  78. <div className="md:flex md:flex-1">
  79. <Helmet>
  80. {(post?.thumbnail || post?.thumbnailBase) && (
  81. <meta name="thumbnail" content={post.thumbnail || post.thumbnailBase}/>)}
  82. {post && <title>{`${ post.title || post.url } | ${ SITE_TITLE }`}</title>}
  83. </Helmet>
  84. <div className="hidden md:block">
  85. <TagDetailSidebar post={post ?? null}/>
  86. </div>
  87. <MainArea className="relative">
  88. {post
  89. ? (
  90. <>
  91. {(post.thumbnail || post.thumbnailBase) && (
  92. <motion.div
  93. layoutId={`page-${ id }`}
  94. className="absolute top-4 left-4 w-[min(640px,calc(100vw-2rem))] h-[360px]
  95. overflow-hidden rounded-xl pointer-events-none z-50"
  96. initial={{ opacity: 1 }}
  97. animate={{ opacity: 0 }}
  98. transition={{ duration: .2, ease: 'easeOut' }}>
  99. <img src={post.thumbnail || post.thumbnailBase}
  100. alt={post.title || post.url}
  101. title={post.title || post.url || undefined}
  102. className="object-cover w-full h-full"/>
  103. </motion.div>)}
  104. <PostEmbed
  105. ref={embedRef}
  106. post={post}
  107. onLoadComplete={() => embedRef.current?.play ()}/>
  108. <Button onClick={() => changeViewedFlg.mutate ()}
  109. disabled={changeViewedFlg.isPending}
  110. className={cn ('text-white', viewedClass)}>
  111. {post.viewed ? '閲覧済' : '未閲覧'}
  112. </Button>
  113. <TabGroup>
  114. <Tab name="関聯">
  115. {post.related.length > 0
  116. ? <PostList posts={post.related}/>
  117. : 'まだないよ(笑)'}
  118. </Tab>
  119. {['admin', 'member'].some (r => user?.role === r) && (
  120. <Tab name="編輯">
  121. <PostEditForm
  122. post={post}
  123. onSave={newPost => {
  124. qc.setQueryData (postsKeys.show (postId),
  125. (prev: any) => newPost ?? prev)
  126. qc.invalidateQueries ({ queryKey: postsKeys.root })
  127. toast ({ description: '更新しました.' })
  128. }}/>
  129. </Tab>)}
  130. </TabGroup>
  131. </>)
  132. : 'Loading...'}
  133. </MainArea>
  134. <div className="md:hidden">
  135. <TagDetailSidebar post={post ?? null}/>
  136. </div>
  137. </div>)
  138. }) satisfies FC<Props>