add top posts/comments to subs

This commit is contained in:
keyan 2023-06-12 17:15:20 -05:00
parent 6ed2c87c49
commit f0f51438c4
7 changed files with 119 additions and 13 deletions

View File

@ -226,8 +226,9 @@ export default {
return count return count
}, },
topItems: async (parent, { cursor, sort, when }, { me, models }) => { topItems: async (parent, { sub, cursor, sort, when }, { me, models }) => {
const decodedCursor = decodeCursor(cursor) const decodedCursor = decodeCursor(cursor)
const subArr = sub ? [sub] : []
const items = await itemQueryWithMeta({ const items = await itemQueryWithMeta({
me, me,
models, models,
@ -236,35 +237,39 @@ export default {
FROM "Item" FROM "Item"
WHERE "parentId" IS NULL AND "Item".created_at <= $1 WHERE "parentId" IS NULL AND "Item".created_at <= $1
AND "pinId" IS NULL AND "deletedAt" IS NULL AND "pinId" IS NULL AND "deletedAt" IS NULL
${subClause(sub, 3)}
${topClause(when)} ${topClause(when)}
${await filterClause(me, models)} ${await filterClause(me, models)}
${await topOrderClause(sort, me, models)} ${await topOrderClause(sort, me, models)}
OFFSET $2 OFFSET $2
LIMIT ${LIMIT}`, LIMIT ${LIMIT}`,
orderBy: await topOrderClause(sort, me, models) orderBy: await topOrderClause(sort, me, models)
}, decodedCursor.time, decodedCursor.offset) }, decodedCursor.time, decodedCursor.offset, ...subArr)
return { return {
cursor: items.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, cursor: items.length === LIMIT ? nextCursorEncoded(decodedCursor) : null,
items items
} }
}, },
topComments: async (parent, { cursor, sort, when }, { me, models }) => { topComments: async (parent, { sub, cursor, sort, when }, { me, models }) => {
const decodedCursor = decodeCursor(cursor) const decodedCursor = decodeCursor(cursor)
const subArr = sub ? [sub] : []
const comments = await itemQueryWithMeta({ const comments = await itemQueryWithMeta({
me, me,
models, models,
query: ` query: `
${SELECT} ${SELECT}
FROM "Item" FROM "Item"
WHERE "parentId" IS NOT NULL JOIN "Item" root ON "Item"."rootId" = root.id
AND "Item".created_at <= $1 AND "deletedAt" IS NULL WHERE "Item"."parentId" IS NOT NULL
AND "Item".created_at <= $1 AND "Item"."deletedAt" IS NULL
${subClause(sub, 3, 'root')}
${topClause(when)} ${topClause(when)}
${await filterClause(me, models)} ${await filterClause(me, models)}
${await topOrderClause(sort, me, models)} ${await topOrderClause(sort, me, models)}
OFFSET $2 OFFSET $2
LIMIT ${LIMIT}`, LIMIT ${LIMIT}`,
orderBy: await topOrderClause(sort, me, models) orderBy: await topOrderClause(sort, me, models)
}, decodedCursor.time, decodedCursor.offset) }, decodedCursor.time, decodedCursor.offset, ...subArr)
return { return {
cursor: comments.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, cursor: comments.length === LIMIT ? nextCursorEncoded(decodedCursor) : null,
comments comments

View File

@ -18,8 +18,8 @@ export default gql`
outlawedItems(cursor: String): Items outlawedItems(cursor: String): Items
borderlandItems(cursor: String): Items borderlandItems(cursor: String): Items
freebieItems(cursor: String): Items freebieItems(cursor: String): Items
topItems(cursor: String, sort: String, when: String): Items topItems(cursor: String, sub: String, sort: String, when: String): Items
topComments(cursor: String, sort: String, when: String): Comments topComments(cursor: String, sub: String, sort: String, when: String): Comments
} }
type TitleUnshorted { type TitleUnshorted {

View File

@ -239,9 +239,9 @@ export default function Header ({ sub }) {
<Nav.Link eventKey='recent' className={styles.navLink}>recent</Nav.Link> <Nav.Link eventKey='recent' className={styles.navLink}>recent</Nav.Link>
</Link> </Link>
</Nav.Item> </Nav.Item>
{!prefix && {sub !== 'jobs' &&
<Nav.Item className={className}> <Nav.Item className={className}>
<Link href='/top/posts/day' passHref> <Link href={prefix + '/top/posts/day'} passHref>
<Nav.Link eventKey='top' className={styles.navLink}>top</Nav.Link> <Nav.Link eventKey='top' className={styles.navLink}>top</Nav.Link>
</Link> </Link>
</Nav.Item>} </Nav.Item>}

View File

@ -4,7 +4,7 @@ import { Form, Select } from './form'
const USER_SORTS = ['stacked', 'spent', 'comments', 'posts', 'referrals'] const USER_SORTS = ['stacked', 'spent', 'comments', 'posts', 'referrals']
const ITEM_SORTS = ['votes', 'comments', 'sats'] const ITEM_SORTS = ['votes', 'comments', 'sats']
export default function TopHeader ({ cat }) { export default function TopHeader ({ sub, cat }) {
const router = useRouter() const router = useRouter()
const top = async values => { const top = async values => {
@ -17,6 +17,8 @@ export default function TopHeader ({ cat }) {
return return
} }
const prefix = sub ? `/~${sub}` : ''
if (typeof query.sort !== 'undefined') { if (typeof query.sort !== 'undefined') {
if (query.sort === '' || if (query.sort === '' ||
(what === 'users' && !USER_SORTS.includes(query.sort)) || (what === 'users' && !USER_SORTS.includes(query.sort)) ||
@ -26,7 +28,7 @@ export default function TopHeader ({ cat }) {
} }
await router.push({ await router.push({
pathname: `/top/${what}/${when || 'day'}`, pathname: `${prefix}/top/${what}/${when || 'day'}`,
query query
}) })
} }
@ -49,7 +51,7 @@ export default function TopHeader ({ cat }) {
onChange={(formik, e) => top({ ...formik?.values, what: e.target.value })} onChange={(formik, e) => top({ ...formik?.values, what: e.target.value })}
name='what' name='what'
size='sm' size='sm'
items={['posts', 'comments', 'users', 'cowboys']} items={router?.query?.sub ? ['posts', 'comments'] : ['posts', 'comments', 'users', 'cowboys']}
/> />
{cat !== 'cowboys' && {cat !== 'cowboys' &&
<> <>

View File

@ -40,6 +40,55 @@ export const SUB_ITEMS = gql`
} }
` `
export const SUB_TOP_ITEMS = gql`
${SUB_FIELDS}
${ITEM_FIELDS}
query SubTopItems($sub: String!, $sort: String, $cursor: String, $when: String) {
sub(name: $sub) {
...SubFields
}
topItems(sub: $sub, sort: $sort, cursor: $cursor, when: $when) {
cursor
items {
...ItemFields
},
pins {
...ItemFields
}
}
}`
export const SUB_TOP_COMMENTS = gql`
${SUB_FIELDS}
${COMMENT_FIELDS}
query SubTopComments($sub: String!, $sort: String, $cursor: String, $when: String = "day") {
sub(name: $sub) {
...SubFields
}
topComments(sub: $sub, sort: $sort, cursor: $cursor, when: $when) {
cursor
comments {
...CommentFields
root {
id
title
bounty
bountyPaidTo
subName
user {
name
streak
hideCowboyHat
id
}
}
}
}
}
`
export const SUB_SEARCH = gql` export const SUB_SEARCH = gql`
${SUB_FIELDS} ${SUB_FIELDS}
${ITEM_FIELDS} ${ITEM_FIELDS}

View File

@ -0,0 +1,25 @@
import Layout from '../../../../../components/layout'
import { useRouter } from 'next/router'
import { getGetServerSideProps } from '../../../../../api/ssrApollo'
import TopHeader from '../../../../../components/top-header'
import { SUB_TOP_COMMENTS } from '../../../../../fragments/subs'
import CommentsFlat from '../../../../../components/comments-flat'
export const getServerSideProps = getGetServerSideProps(SUB_TOP_COMMENTS, undefined, data => !data.sub)
export default function Index ({ data: { sub, topComments: { comments, cursor } } }) {
const router = useRouter()
return (
<Layout sub={sub?.name}>
<TopHeader sub={sub?.name} cat='comments' />
<CommentsFlat
comments={comments} cursor={cursor}
query={SUB_TOP_COMMENTS}
destructureData={data => data.topComments}
variables={{ sub: sub?.name, sort: router.query.sort, when: router.query.when }}
includeParent noReply
/>
</Layout>
)
}

View File

@ -0,0 +1,25 @@
import Layout from '../../../../../components/layout'
import Items from '../../../../../components/items'
import { useRouter } from 'next/router'
import { getGetServerSideProps } from '../../../../../api/ssrApollo'
import TopHeader from '../../../../../components/top-header'
import { SUB_TOP_ITEMS } from '../../../../../fragments/subs'
export const getServerSideProps = getGetServerSideProps(SUB_TOP_ITEMS, undefined,
data => !data.sub)
export default function Index ({ data: { sub, topItems: { items, cursor } } }) {
const router = useRouter()
return (
<Layout sub={sub?.name}>
<TopHeader sub={sub?.name} cat='posts' />
<Items
items={items} cursor={cursor}
query={SUB_TOP_ITEMS}
destructureData={data => data.topItems}
variables={{ sub: sub?.name, sort: router.query.sort, when: router.query.when }} rank
/>
</Layout>
)
}