Skip to content

Commit

Permalink
Ensure that the Table defaults to the page that will contain the sele…
Browse files Browse the repository at this point in the history
…cted row when it is first mounted

Signed-off-by: Aayush Srivastava <aayushsrivastava.work@gmail.com>
  • Loading branch information
aayushsrivastava committed Jan 13, 2025
1 parent c0fdfdc commit f4ad40b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
5 changes: 5 additions & 0 deletions ui/src/features/assemble-freight/chart-table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { calculatePageForSelectedRow } from '@ui/utils/pagination';
import { Radio, Table } from 'antd';
import { useState } from 'react';

export const ChartTable = ({
versions,
Expand All @@ -9,9 +11,12 @@ export const ChartTable = ({
selected: string | undefined;
select: (version?: string) => void;
}) => {
const [defaultPage] = useState<number>(() => calculatePageForSelectedRow(selected, versions, (version) => version));

return (
<Table
dataSource={versions.map((version) => ({ version }))}
pagination={{ defaultCurrent: defaultPage }}
columns={[
{
width: '50px',
Expand Down
5 changes: 5 additions & 0 deletions ui/src/features/assemble-freight/commit-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DiscoveredCommit } from '@ui/gen/v1alpha1/generated_pb';
import { timestampDate } from '@ui/utils/connectrpc-utils';

import { TruncatedCopyable } from './truncated-copyable';
import { calculatePageForSelectedRow } from '@ui/utils/pagination';
import { useState } from 'react';

export const CommitTable = ({
commits,
Expand All @@ -14,10 +16,13 @@ export const CommitTable = ({
selected: DiscoveredCommit | undefined;
select: (commit?: DiscoveredCommit) => void;
}) => {
const [defaultPage] = useState<number>(() => calculatePageForSelectedRow(selected, commits, (commit) => commit.id));

return (
<>
<Table
dataSource={commits}
pagination={{ defaultCurrent: defaultPage }}
columns={[
{
render: (record: DiscoveredCommit) => (
Expand Down
81 changes: 44 additions & 37 deletions ui/src/features/assemble-freight/image-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { DiscoveredImageReference } from '@ui/gen/v1alpha1/generated_pb';
import { timestampDate } from '@ui/utils/connectrpc-utils';

import { TruncatedCopyable } from './truncated-copyable';
import { useState } from 'react';
import { calculatePageForSelectedRow } from '@ui/utils/pagination';

export const ImageTable = ({
references,
Expand All @@ -15,41 +17,46 @@ export const ImageTable = ({
references: DiscoveredImageReference[];
selected: DiscoveredImageReference | undefined;
select: (reference?: DiscoveredImageReference) => void;
}) => (
<>
<Table
dataSource={references}
columns={[
{
render: (record: DiscoveredImageReference) => (
<Radio
checked={selected?.tag === record?.tag}
onClick={() => select(selected === record ? undefined : record)}
/>
)
},
{ title: 'Tag', dataIndex: 'tag' },
{
title: 'Digest',
render: (record: DiscoveredImageReference) => <TruncatedCopyable text={record?.digest} />
},
{
title: 'Source Repo',
render: (record: DiscoveredImageReference) =>
record?.gitRepoURL ? (
<a href={record?.gitRepoURL} target='_blank' rel='noreferrer'>
{record?.gitRepoURL}
</a>
) : (
<FontAwesomeIcon icon={faQuestionCircle} className='text-gray-400' />
}) => {
const [defaultPage] = useState<number>(() => calculatePageForSelectedRow(selected, references, (ref) => ref.tag));

return (
<>
<Table
dataSource={references}
pagination={{ defaultCurrent: defaultPage }}
columns={[
{
render: (record: DiscoveredImageReference) => (
<Radio
checked={selected?.tag === record?.tag}
onClick={() => select(selected === record ? undefined : record)}
/>
)
},
{
title: 'Created At',
render: (record: DiscoveredImageReference) =>
timestampDate(record.createdAt)?.toLocaleString()
}
]}
/>
</>
);
},
{ title: 'Tag', dataIndex: 'tag' },
{
title: 'Digest',
render: (record: DiscoveredImageReference) => <TruncatedCopyable text={record?.digest} />
},
{
title: 'Source Repo',
render: (record: DiscoveredImageReference) =>
record?.gitRepoURL ? (
<a href={record?.gitRepoURL} target='_blank' rel='noreferrer'>
{record?.gitRepoURL}
</a>
) : (
<FontAwesomeIcon icon={faQuestionCircle} className='text-gray-400' />
)
},
{
title: 'Created At',
render: (record: DiscoveredImageReference) =>
timestampDate(record.createdAt)?.toLocaleString()
}
]}
/>
</>
)
};
14 changes: 14 additions & 0 deletions ui/src/utils/pagination.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, test } from "vitest";
import { calculatePageForSelectedRow } from "./pagination";

describe("calculatePageForSelectedRow", () => {
test.each([
[undefined, ["a", "b", "c"], (option: string) => option, 1],
["a", ["a", "b", "c"], (option: string) => option, 1],
["j", ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"], (option: string) => option, 1],
["k", ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"], (option: string) => option, 2],
])('selectedOption: %s, options: %s, key: %s, expectedPage: %s', (selectedOption, options, key, expectedPage) => {
const page = calculatePageForSelectedRow(selectedOption, options, key);
expect(page).toBe(expectedPage);
});
});
12 changes: 12 additions & 0 deletions ui/src/utils/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function calculatePageForSelectedRow<T>(selectedOption: T | undefined, options: T[], key: (option: T) => string): number {
const pageSize = 10;

if (selectedOption) {
const index = options.findIndex((option) => key(option) === key(selectedOption));
if (index >= 0) {
const page = Math.floor(index / pageSize) + 1;
return page;
}
}
return 1;
}

0 comments on commit f4ad40b

Please sign in to comment.