2021-04-22 22:14:32 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2022-07-21 22:55:05 +00:00
|
|
|
import Item, { ItemSkeleton } from './item'
|
|
|
|
import ItemJob from './item-job'
|
2021-04-14 23:56:29 +00:00
|
|
|
import styles from './items.module.css'
|
2021-09-30 15:46:58 +00:00
|
|
|
import MoreFooter from './more-footer'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { Fragment, useCallback, useMemo } from 'react'
|
2023-05-06 21:51:17 +00:00
|
|
|
import { CommentFlat } from './comment'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { SUB_ITEMS } from '../fragments/subs'
|
|
|
|
import { LIMIT } from '../lib/cursor'
|
|
|
|
import ItemFull from './item-full'
|
2023-08-06 18:04:25 +00:00
|
|
|
import { useData } from './use-data'
|
2021-04-14 23:56:29 +00:00
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
export default function Items ({ ssrData, variables = {}, query, destructureData, rank, noMoreText, Footer, filter = () => true }) {
|
|
|
|
const { data, fetchMore } = useQuery(query || SUB_ITEMS, { variables })
|
|
|
|
const Foooter = Footer || MoreFooter
|
2023-08-06 18:04:25 +00:00
|
|
|
const dat = useData(data, ssrData)
|
2021-09-30 15:46:58 +00:00
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
const { items, pins, cursor } = useMemo(() => {
|
2023-08-06 18:04:25 +00:00
|
|
|
if (!dat) return {}
|
2022-10-25 21:35:32 +00:00
|
|
|
if (destructureData) {
|
2023-08-06 18:04:25 +00:00
|
|
|
return destructureData(dat)
|
2022-10-25 21:35:32 +00:00
|
|
|
} else {
|
2023-08-06 18:04:25 +00:00
|
|
|
return dat?.items
|
2022-10-25 21:35:32 +00:00
|
|
|
}
|
2023-08-06 18:04:25 +00:00
|
|
|
}, [dat])
|
2022-01-07 16:32:31 +00:00
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
const pinMap = useMemo(() =>
|
|
|
|
pins?.reduce((a, p) => { a[p.position] = p; return a }, {}), [pins])
|
|
|
|
|
|
|
|
const Skeleton = useCallback(() =>
|
2024-01-17 23:39:39 +00:00
|
|
|
<ItemsSkeleton rank={rank} startRank={items?.length} limit={variables.limit} Footer={Foooter} />, [rank, items])
|
2023-07-23 15:08:43 +00:00
|
|
|
|
2023-08-06 18:04:25 +00:00
|
|
|
if (!dat) {
|
2023-07-23 15:08:43 +00:00
|
|
|
return <Skeleton />
|
|
|
|
}
|
2023-07-23 14:16:12 +00:00
|
|
|
|
2021-06-22 17:47:49 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-04-22 22:14:32 +00:00
|
|
|
<div className={styles.grid}>
|
2023-07-23 15:08:43 +00:00
|
|
|
{items.filter(filter).map((item, i) => (
|
2023-05-06 21:51:17 +00:00
|
|
|
<Fragment key={item.id}>
|
2022-02-17 17:23:43 +00:00
|
|
|
{pinMap && pinMap[i + 1] && <Item item={pinMap[i + 1]} />}
|
2023-08-29 21:05:09 +00:00
|
|
|
<ListItem item={item} rank={rank && i + 1} siblingComments={variables.includeComments} />
|
2023-05-06 21:51:17 +00:00
|
|
|
</Fragment>
|
2021-04-22 22:14:32 +00:00
|
|
|
))}
|
|
|
|
</div>
|
2023-07-23 15:08:43 +00:00
|
|
|
<Foooter
|
|
|
|
cursor={cursor} fetchMore={fetchMore} noMoreText={noMoreText}
|
|
|
|
count={items?.length}
|
|
|
|
Skeleton={Skeleton}
|
2021-09-30 15:46:58 +00:00
|
|
|
/>
|
2021-06-22 17:47:49 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-29 21:05:09 +00:00
|
|
|
export function ListItem ({ item, ...props }) {
|
|
|
|
return (
|
|
|
|
item.parentId
|
|
|
|
? <CommentFlat item={item} noReply includeParent clickToContext {...props} />
|
|
|
|
: (item.isJob
|
|
|
|
? <ItemJob item={item} />
|
|
|
|
: (item.searchText
|
|
|
|
? <ItemFull item={item} noReply {...props} />
|
|
|
|
: <Item item={item} {...props} />))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-17 23:39:39 +00:00
|
|
|
export function ItemsSkeleton ({ rank, startRank = 0, limit = LIMIT, Footer }) {
|
2023-07-23 15:08:43 +00:00
|
|
|
const items = new Array(limit).fill(null)
|
2021-04-22 22:14:32 +00:00
|
|
|
|
2021-04-14 23:56:29 +00:00
|
|
|
return (
|
2024-01-17 23:39:39 +00:00
|
|
|
<>
|
|
|
|
<div className={styles.grid}>
|
|
|
|
{items.map((_, i) => (
|
|
|
|
<ItemSkeleton rank={rank && i + startRank + 1} key={i + startRank} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
<Footer invisible cursor />
|
|
|
|
</>
|
2021-04-14 23:56:29 +00:00
|
|
|
)
|
|
|
|
}
|