Skip to content

Commit 9ecb0dd

Browse files
Auto expand pr table if param passed (#2783)
1 parent 3d519ac commit 9ecb0dd

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

src/pages/PullRequestPage/PullCoverage/routes/FilesChangedTab/FilesChanged/FilesChangedTable/FilesChangedTable.spec.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ const mockPull = ({
116116
const server = setupServer()
117117

118118
const wrapper =
119-
(queryClient: QueryClient): React.FC<React.PropsWithChildren> =>
119+
(
120+
queryClient: QueryClient,
121+
initialEntries = ['/gh/codecov/test-repo/pull/s2h5a6']
122+
): React.FC<React.PropsWithChildren> =>
120123
({ children }) =>
121124
(
122125
<QueryClientProvider client={queryClient}>
123-
<MemoryRouter initialEntries={['/gh/codecov/test-repo/pull/s2h5a6']}>
126+
<MemoryRouter initialEntries={initialEntries}>
124127
<Route path="/:provider/:owner/:repo/pull/:pull">{children}</Route>
125128
</MemoryRouter>
126129
</QueryClientProvider>
@@ -374,6 +377,18 @@ describe('FilesChangedTable', () => {
374377
const pullFileDiff = await screen.findByText('FileDiff')
375378
expect(pullFileDiff).toBeInTheDocument()
376379
})
380+
381+
it('auto expands if param is passed in url', async () => {
382+
const { queryClient } = setup()
383+
render(<FilesChangedTable />, {
384+
wrapper: wrapper(queryClient, [
385+
'/gh/codecov/test-repo/pull/s2h5a6?filepath=flag1/mafs.js',
386+
]),
387+
})
388+
389+
const pullFileDiff = await screen.findByText('FileDiff')
390+
expect(pullFileDiff).toBeInTheDocument()
391+
})
377392
})
378393

379394
describe('highlights critical files', () => {

src/pages/PullRequestPage/PullCoverage/routes/FilesChangedTab/FilesChanged/FilesChangedTable/FilesChangedTable.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import {
1212
import cs from 'classnames'
1313
import isEmpty from 'lodash/isEmpty'
1414
import isNumber from 'lodash/isNumber'
15-
import { Fragment, lazy, Suspense, useMemo, useState } from 'react'
16-
import { useParams } from 'react-router-dom'
15+
import qs from 'qs'
16+
import { Fragment, lazy, Suspense, useEffect, useMemo, useState } from 'react'
17+
import { useLocation , useParams } from 'react-router-dom'
1718

1819
import {
1920
ImpactedFile,
@@ -246,6 +247,11 @@ export default function FilesChangedTable() {
246247
{ id: 'missedLines', desc: true },
247248
])
248249
const { provider, owner, repo, pullId } = useParams<URLParams>()
250+
const location = useLocation()
251+
const params = qs.parse(location.search, {
252+
ignoreQueryPrefix: true,
253+
})
254+
const currentlySelectedFile = params.filepath
249255

250256
const { data: pullData, isLoading } = usePull({
251257
provider,
@@ -274,6 +280,19 @@ export default function FilesChangedTable() {
274280
return []
275281
}, [pullData?.pull?.compareWithBase])
276282

283+
useEffect(() => {
284+
if (data.length > 0 && currentlySelectedFile) {
285+
const fileToExpandIndex = data.findIndex(
286+
(file) => file && file.headName === currentlySelectedFile
287+
)
288+
if (fileToExpandIndex !== -1) {
289+
setExpanded({
290+
[fileToExpandIndex]: true,
291+
})
292+
}
293+
}
294+
}, [data, currentlySelectedFile])
295+
277296
const table = useReactTable({
278297
columns: getColumns({ pullId }),
279298
data,

src/pages/PullRequestPage/PullCoverage/routes/FilesChangedTab/FilesChanged/TableTeam/TableTeam.spec.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,14 @@ const mockNoChangeFileData = {
221221
const server = setupServer()
222222

223223
const wrapper =
224-
(queryClient: QueryClient): React.FC<React.PropsWithChildren> =>
224+
(
225+
queryClient: QueryClient,
226+
initialEntries = ['/gh/codecov/test-repo/pull/s2h5a6']
227+
): React.FC<React.PropsWithChildren> =>
225228
({ children }) =>
226229
(
227230
<QueryClientProvider client={queryClient}>
228-
<MemoryRouter initialEntries={['/gh/codecov/test-repo/pull/s2h5a6']}>
231+
<MemoryRouter initialEntries={initialEntries}>
229232
<Route path="/:provider/:owner/:repo/pull/:pull">{children}</Route>
230233
</MemoryRouter>
231234
</QueryClientProvider>
@@ -425,6 +428,18 @@ describe('TableTeam', () => {
425428
const pullFileDiff = await screen.findByText('FileDiff')
426429
expect(pullFileDiff).toBeInTheDocument()
427430
})
431+
432+
it('auto expands if param is passed in url', async () => {
433+
const { queryClient } = setup()
434+
render(<TableTeam />, {
435+
wrapper: wrapper(queryClient, [
436+
'/gh/codecov/test-repo/pull/s2h5a6?filepath=src/App.jsx',
437+
]),
438+
})
439+
440+
const pullFileDiff = await screen.findByText('FileDiff')
441+
expect(pullFileDiff).toBeInTheDocument()
442+
})
428443
})
429444

430445
describe('highlights critical files', () => {

src/pages/PullRequestPage/PullCoverage/routes/FilesChangedTab/FilesChanged/TableTeam/TableTeam.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {
1111
} from '@tanstack/react-table'
1212
import cs from 'classnames'
1313
import isEmpty from 'lodash/isEmpty'
14-
import { Fragment, lazy, Suspense, useMemo, useState } from 'react'
15-
import { useParams } from 'react-router-dom'
14+
import qs from 'qs'
15+
import { Fragment, lazy, Suspense, useEffect, useMemo, useState } from 'react'
16+
import { useLocation , useParams } from 'react-router-dom'
1617

1718
import {
1819
ImpactedFile,
@@ -174,6 +175,12 @@ export default function FilesChangedTableTeam() {
174175
mostRecentCompare = pullData?.pull?.compareWithBase
175176
}
176177

178+
const location = useLocation()
179+
const params = qs.parse(location.search, {
180+
ignoreQueryPrefix: true,
181+
})
182+
const currentlySelectedFile = params.filepath
183+
177184
const data = useMemo(() => {
178185
if (
179186
pullData?.pull?.compareWithBase?.__typename === 'Comparison' &&
@@ -185,6 +192,19 @@ export default function FilesChangedTableTeam() {
185192
return []
186193
}, [pullData?.pull?.compareWithBase])
187194

195+
useEffect(() => {
196+
if (data.length > 0 && currentlySelectedFile) {
197+
const fileToExpandIndex = data.findIndex(
198+
(file) => file && file.headName === currentlySelectedFile
199+
)
200+
if (fileToExpandIndex !== -1) {
201+
setExpanded({
202+
[fileToExpandIndex]: true,
203+
})
204+
}
205+
}
206+
}, [data, currentlySelectedFile])
207+
188208
const table = useReactTable({
189209
columns: getColumns({ pullId }),
190210
data,

0 commit comments

Comments
 (0)