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

130 lines
3.9 KiB

  1. import { useQuery } 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 { SITE_TITLE } from '@/config'
  12. import { fetchPostChanges } from '@/lib/posts'
  13. import { postsKeys } from '@/lib/queryKeys'
  14. import { cn, dateString } from '@/lib/utils'
  15. import type { FC } from 'react'
  16. export default (() => {
  17. const location = useLocation ()
  18. const query = new URLSearchParams (location.search)
  19. const id = query.get ('id')
  20. const page = Number (query.get ('page') ?? 1)
  21. const limit = Number (query.get ('limit') ?? 20)
  22. // 投稿列の結合で使用
  23. let rowsCnt: number
  24. const { data, isLoading: loading } = useQuery ({
  25. queryKey: postsKeys.changes ({ ...(id && { id }), page, limit }),
  26. queryFn: () => fetchPostChanges ({ ...(id && { id }), page, limit }) })
  27. const changes = data?.changes ?? []
  28. const totalPages = data ? Math.ceil (data.count / limit) : 0
  29. useEffect (() => {
  30. document.querySelector ('table')?.scrollIntoView ({ behavior: 'smooth' })
  31. }, [location.search])
  32. const layoutIds: string[] = []
  33. return (
  34. <MainArea>
  35. <Helmet>
  36. <title>{`耕作履歴 | ${ SITE_TITLE }`}</title>
  37. </Helmet>
  38. <PageTitle>
  39. 耕作履歴
  40. {id && <>: 投稿 {<PrefetchLink to={`/posts/${ id }`}>#{id}</PrefetchLink>}</>}
  41. </PageTitle>
  42. {loading ? 'Loading...' : (
  43. <>
  44. <table className="table-auto w-full border-collapse">
  45. <thead className="border-b-2 border-black dark:border-white">
  46. <tr>
  47. <th className="p-2 text-left">投稿</th>
  48. <th className="p-2 text-left">変更</th>
  49. <th className="p-2 text-left">日時</th>
  50. </tr>
  51. </thead>
  52. <tbody>
  53. {changes.map ((change, i) => {
  54. const withPost = i === 0 || change.post.id !== changes[i - 1].post.id
  55. if (withPost)
  56. {
  57. rowsCnt = 1
  58. for (let j = i + 1;
  59. (j < changes.length
  60. && change.post.id === changes[j].post.id);
  61. ++j)
  62. ++rowsCnt
  63. }
  64. let layoutId: string | undefined = `page-${ change.post.id }`
  65. if (layoutIds.includes (layoutId))
  66. layoutId = undefined
  67. else
  68. layoutIds.push (layoutId)
  69. return (
  70. <tr key={`${ change.timestamp }-${ change.post.id }-${ change.tag?.id }`}
  71. className={cn ('even:bg-gray-100 dark:even:bg-gray-700',
  72. withPost && 'border-t')}>
  73. {withPost && (
  74. <td className="align-top p-2 bg-white dark:bg-[#242424] border-r"
  75. rowSpan={rowsCnt}>
  76. <PrefetchLink to={`/posts/${ change.post.id }`}>
  77. <motion.div
  78. layoutId={layoutId}
  79. transition={{ type: 'spring',
  80. stiffness: 500,
  81. damping: 40,
  82. mass: .5 }}>
  83. <img src={change.post.thumbnail
  84. || change.post.thumbnailBase
  85. || undefined}
  86. alt={change.post.title || change.post.url}
  87. title={change.post.title || change.post.url || undefined}
  88. className="w-40"/>
  89. </motion.div>
  90. </PrefetchLink>
  91. </td>)}
  92. <td className="p-2">
  93. {change.tag
  94. ? <TagLink tag={change.tag} withWiki={false} withCount={false}/>
  95. : '(マスタ削除済のタグ) '}
  96. {`を${ change.changeType === 'add' ? '記載' : '消除' }`}
  97. </td>
  98. <td className="p-2">
  99. {change.user
  100. ? (
  101. <PrefetchLink to={`/users/${ change.user.id }`}>
  102. {change.user.name}
  103. </PrefetchLink>)
  104. : 'bot 操作'}
  105. <br/>
  106. {dateString (change.timestamp)}
  107. </td>
  108. </tr>)
  109. })}
  110. </tbody>
  111. </table>
  112. <Pagination page={page} totalPages={totalPages}/>
  113. </>)}
  114. </MainArea>)
  115. }) satisfies FC