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

156 lines
4.5 KiB

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