Skip to content

Commit

Permalink
[#133] reflect current tabs in URL
Browse files Browse the repository at this point in the history
project tabs now appear in URL when clicked (but using that URL doesn't yet set the page to the correct tabs)

QUESTION
Should value be set to a more specific type here?:

const handleTabChange = (type: 'tab' | 'comments', value: string | null)
  • Loading branch information
goblinodds committed Jan 5, 2024
1 parent c678b90 commit f2b9e1c
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pages/project/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ function ProjectPage({ projectId }: { projectId: string }) {
const projectQuery = trpc.project.detail.useQuery(projectQueryInput)
const project = projectQuery.data

// Set active tab based on URL query
const activeTab = router.query.tab || 'defaultTabValue' // top tabs
const activeCommentsTab = router.query.comments || 'defaultCommentsValue' // comments tabs

// QUESTION: should value be set to a more specific type?
const handleTabChange = (type: 'tab' | 'comments', value: string | null) => {
// Update URL with new tab value
const { id, ...restQuery } = router.query
router.push(
{
pathname: `/project/${projectId}`,
query: { ...restQuery, [type]: value },
},
undefined,
{ shallow: true },
)
}

const likeMutation = trpc.project.like.useMutation({
onSettled: () => {
return utils.project.detail.invalidate({ id: projectId })
Expand Down Expand Up @@ -251,6 +269,7 @@ function ProjectPage({ projectId }: { projectId: string }) {
? 'topContributors'
: 'registerDonations'
}
onChange={(value) => handleTabChange('tab', value)}
>
<Tabs.List>
{project.donationCount && (
Expand Down Expand Up @@ -307,7 +326,10 @@ function ProjectPage({ projectId }: { projectId: string }) {
</div>
</div>

<Tabs defaultValue={CommentType.COMMENT}>
<Tabs
defaultValue={CommentType.COMMENT}
onChange={(value) => handleTabChange('comments', value)}
>
<Tabs.List>
<Tabs.Tab value={CommentType.COMMENT}>Comments</Tabs.Tab>
<Tabs.Tab value={CommentType.Q_AND_A}>
Expand Down

0 comments on commit f2b9e1c

Please sign in to comment.