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

248 lines
7.5 KiB

  1. import { useQuery, useQueryClient } from '@tanstack/react-query'
  2. import { motion } from 'framer-motion'
  3. import { useEffect } from 'react'
  4. import { Helmet } from 'react-helmet-async'
  5. import { useLocation } from 'react-router-dom'
  6. import TagLink from '@/components/TagLink'
  7. import PrefetchLink from '@/components/PrefetchLink'
  8. import PageTitle from '@/components/common/PageTitle'
  9. import Pagination from '@/components/common/Pagination'
  10. import MainArea from '@/components/layout/MainArea'
  11. import { toast } from '@/components/ui/use-toast'
  12. import { SITE_TITLE } from '@/config'
  13. import { apiPut } from '@/lib/api'
  14. import { fetchPostChanges } from '@/lib/posts'
  15. import { postsKeys, tagsKeys } from '@/lib/queryKeys'
  16. import { fetchTag } from '@/lib/tags'
  17. import { cn, dateString, originalCreatedAtString } from '@/lib/utils'
  18. import type { FC } from 'react'
  19. const renderDiff = (diff: { current: string | null; prev: string | null }) => (
  20. <>
  21. {(diff.prev && diff.prev !== diff.current) && (
  22. <>
  23. <del className="text-red-600 dark:text-red-400">
  24. {diff.prev}
  25. </del>
  26. {diff.current && <br/>}
  27. </>)}
  28. {diff.current}
  29. </>)
  30. export default (() => {
  31. const location = useLocation ()
  32. const query = new URLSearchParams (location.search)
  33. const id = query.get ('id')
  34. const tagId = query.get ('tag')
  35. const page = Number (query.get ('page') ?? 1)
  36. const limit = Number (query.get ('limit') ?? 20)
  37. // 投稿列の結合で使用
  38. let rowsCnt: number
  39. const { data: tag } =
  40. tagId
  41. ? useQuery ({ queryKey: tagsKeys.show (tagId),
  42. queryFn: () => fetchTag (tagId) })
  43. : { data: null }
  44. const { data, isLoading: loading } = useQuery ({
  45. queryKey: postsKeys.changes ({ ...(id && { post: id }),
  46. ...(tagId && { tag: tagId }),
  47. page, limit }),
  48. queryFn: () => fetchPostChanges ({ ...(id && { post: id }),
  49. ...(tagId && { tag: tagId }),
  50. page, limit }) })
  51. const changes = data?.versions ?? []
  52. const totalPages = data ? Math.ceil (data.count / limit) : 0
  53. const qc = useQueryClient ()
  54. useEffect (() => {
  55. document.querySelector ('table')?.scrollIntoView ({ behavior: 'smooth' })
  56. }, [location.search])
  57. const layoutIds: string[] = []
  58. return (
  59. <MainArea>
  60. <Helmet>
  61. <title>{`耕作履歴 | ${ SITE_TITLE }`}</title>
  62. </Helmet>
  63. <PageTitle>
  64. 耕作履歴
  65. {id && <>: 投稿 {<PrefetchLink to={`/posts/${ id }`}>#{id}</PrefetchLink>}</>}
  66. {tag && <>(<TagLink tag={tag} withWiki={false} withCount={false}/>)</>}
  67. </PageTitle>
  68. {loading ? 'Loading...' : (
  69. <>
  70. <div className="overflow-x-auto">
  71. <table className="w-full min-w-[1200px] table-fixed border-collapse">
  72. <colgroup>
  73. {/* 投稿 */}
  74. <col className="w-64"/>
  75. {/* 版 */}
  76. <col className="w-40"/>
  77. {/* タイトル */}
  78. <col className="w-96"/>
  79. {/* URL */}
  80. <col className="w-96"/>
  81. {/* タグ */}
  82. <col className="w-[48rem]"/>
  83. {/* オリジナルの投稿日時 */}
  84. <col className="w-96"/>
  85. {/* 更新日時 */}
  86. <col className="w-64"/>
  87. {/* (差戻ボタン) */}
  88. <col className="w-20"/>
  89. </colgroup>
  90. <thead className="border-b-2 border-black dark:border-white">
  91. <tr>
  92. <th className="p-2 text-left">投稿</th>
  93. <th className="p-2 text-left">版</th>
  94. <th className="p-2 text-left">タイトル</th>
  95. <th className="p-2 text-left">URL</th>
  96. <th className="p-2 text-left">タグ</th>
  97. <th className="p-2 text-left">オリジナルの投稿日時</th>
  98. <th className="p-2 text-left">更新日時</th>
  99. <th className="p-2"/>
  100. </tr>
  101. </thead>
  102. <tbody>
  103. {changes.map ((change, i) => {
  104. const withPost = i === 0 || change.postId !== changes[i - 1].postId
  105. if (withPost)
  106. {
  107. rowsCnt = 1
  108. for (let j = i + 1;
  109. (j < changes.length
  110. && change.postId === changes[j].postId);
  111. ++j)
  112. ++rowsCnt
  113. }
  114. let layoutId: string | undefined = `page-${ change.postId }`
  115. if (layoutIds.includes (layoutId))
  116. layoutId = undefined
  117. else
  118. layoutIds.push (layoutId)
  119. return (
  120. <tr key={`${ change.postId }.${ change.versionNo }`}
  121. className={cn ('even:bg-gray-100 dark:even:bg-gray-700',
  122. withPost && 'border-t')}>
  123. {withPost && (
  124. <td className="align-top p-2 bg-white dark:bg-[#242424] border-r"
  125. rowSpan={rowsCnt}>
  126. <PrefetchLink to={`/posts/${ change.postId }`}>
  127. <motion.div
  128. layoutId={layoutId}
  129. transition={{ layout: { duration: .2, ease: 'easeOut' } }}>
  130. <img src={change.thumbnail.current
  131. || change.thumbnailBase.current
  132. || undefined}
  133. alt={change.title.current || change.url.current}
  134. title={change.title.current || change.url.current || undefined}
  135. className="w-40"/>
  136. </motion.div>
  137. </PrefetchLink>
  138. </td>)}
  139. <td className="p-2">{change.postId}.{change.versionNo}</td>
  140. <td className="p-2 break-all">{renderDiff (change.title)}</td>
  141. <td className="p-2 break-all">{renderDiff (change.url)}</td>
  142. <td className="p-2">
  143. {change.tags.map ((tag, i) => (
  144. tag.type === 'added'
  145. ? (
  146. <ins
  147. key={i}
  148. className="mr-2 text-green-600 dark:text-green-400">
  149. {tag.name}
  150. </ins>)
  151. : (
  152. tag.type === 'removed'
  153. ? (
  154. <del
  155. key={i}
  156. className="mr-2 text-red-600 dark:text-red-400">
  157. {tag.name}
  158. </del>)
  159. : (
  160. <span key={i} className="mr-2">
  161. {tag.name}
  162. </span>))))}
  163. </td>
  164. <td className="p-2">
  165. {change.versionNo === 1
  166. ? originalCreatedAtString (change.originalCreatedFrom.current,
  167. change.originalCreatedBefore.current)
  168. : renderDiff ({
  169. current: originalCreatedAtString (
  170. change.originalCreatedFrom.current,
  171. change.originalCreatedBefore.current),
  172. prev: originalCreatedAtString (
  173. change.originalCreatedFrom.prev,
  174. change.originalCreatedBefore.prev) })}
  175. </td>
  176. <td className="p-2">
  177. {change.createdByUser
  178. ? (
  179. <PrefetchLink to={`/users/${ change.createdByUser.id }`}>
  180. {change.createdByUser.name
  181. || `名もなきニジラー(#${ change.createdByUser.id })`}
  182. </PrefetchLink>)
  183. : 'bot 操作'}
  184. <br/>
  185. {dateString (change.createdAt)}
  186. </td>
  187. <td className="p-2">
  188. <a
  189. href="#"
  190. onClick={async e => {
  191. e.preventDefault ()
  192. if (!(confirm (
  193. `『${ change.title.current
  194. || change.url.current }』を版 ${
  195. change.versionNo } に差戻します.\nよろしいですか?`)))
  196. return
  197. await apiPut (
  198. `/posts/${ change.postId }`,
  199. { title: change.title.current,
  200. tags: change.tags
  201. .filter (t => t.type !== 'removed')
  202. .map (t => t.name)
  203. .filter (t => t.slice (0, 5) !== 'nico:')
  204. .join (' '),
  205. original_created_from:
  206. change.originalCreatedFrom.current,
  207. original_created_before:
  208. change.originalCreatedBefore.current })
  209. qc.invalidateQueries ({ queryKey: postsKeys.root })
  210. qc.invalidateQueries ({ queryKey: tagsKeys.root })
  211. toast ({ description: '更新しました.' })
  212. }}>
  213. 復元
  214. </a>
  215. </td>
  216. </tr>)
  217. })}
  218. </tbody>
  219. </table>
  220. </div>
  221. <Pagination page={page} totalPages={totalPages}/>
  222. </>)}
  223. </MainArea>)
  224. }) satisfies FC