65 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-04-22 17:14:32 -05:00
import { useQuery } from '@apollo/client'
2022-07-21 17:55:05 -05:00
import Item, { ItemSkeleton } from './item'
import ItemJob from './item-job'
2021-04-14 18:56:29 -05:00
import styles from './items.module.css'
2022-02-17 11:23:43 -06:00
import { ITEMS } from '../fragments/items'
2021-09-30 10:46:58 -05:00
import MoreFooter from './more-footer'
2022-01-07 10:32:31 -06:00
import React from 'react'
2022-01-27 13:18:48 -06:00
import Comment from './comment'
2021-04-14 18:56:29 -05:00
2022-10-25 16:35:32 -05:00
export default function Items ({ variables = {}, query, destructureData, rank, items, pins, cursor }) {
const { data, fetchMore } = useQuery(query || ITEMS, { variables })
2021-09-30 10:46:58 -05:00
if (!data && !items) {
2021-06-24 18:58:58 -05:00
return <ItemsSkeleton rank={rank} />
2021-06-22 12:47:49 -05:00
}
2021-04-22 17:14:32 -05:00
2021-09-30 10:46:58 -05:00
if (data) {
2022-10-25 16:35:32 -05:00
if (destructureData) {
({ items, pins, cursor } = destructureData(data))
} else {
({ items: { items, pins, cursor } } = data)
}
2021-09-30 10:46:58 -05:00
}
2022-01-07 10:32:31 -06:00
const pinMap = pins?.reduce((a, p) => { a[p.position] = p; return a }, {})
2021-06-22 12:47:49 -05:00
return (
<>
2021-04-22 17:14:32 -05:00
<div className={styles.grid}>
2021-06-22 12:47:49 -05:00
{items.map((item, i) => (
2022-01-07 10:32:31 -06:00
<React.Fragment key={item.id}>
2022-02-17 11:23:43 -06:00
{pinMap && pinMap[i + 1] && <Item item={pinMap[i + 1]} />}
2022-01-27 13:18:48 -06:00
{item.parentId
? <><div /><div className='pb-3'><Comment item={item} noReply includeParent /></div></>
2022-09-29 15:42:33 -05:00
: (item.isJob
2022-02-17 11:23:43 -06:00
? <ItemJob item={item} rank={rank && i + 1} />
2022-09-22 13:44:50 -05:00
: (item.title
? <Item item={item} rank={rank && i + 1} />
: (
<div className='pb-2'>
<Comment item={item} noReply includeParent clickToContext />
</div>)))}
2022-01-07 10:32:31 -06:00
</React.Fragment>
2021-04-22 17:14:32 -05:00
))}
</div>
2021-09-30 10:46:58 -05:00
<MoreFooter
cursor={cursor} fetchMore={fetchMore}
Skeleton={() => <ItemsSkeleton rank={rank} startRank={items.length} />}
/>
2021-06-22 12:47:49 -05:00
</>
)
}
2022-09-22 13:44:50 -05:00
export function ItemsSkeleton ({ rank, startRank = 0 }) {
2021-06-22 12:47:49 -05:00
const items = new Array(21).fill(null)
2021-04-22 17:14:32 -05:00
2021-04-14 18:56:29 -05:00
return (
<div className={styles.grid}>
2021-06-22 12:47:49 -05:00
{items.map((_, i) => (
2021-06-24 18:58:58 -05:00
<ItemSkeleton rank={rank && i + startRank + 1} key={i + startRank} />
2021-04-14 18:56:29 -05:00
))}
</div>
)
}