Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Srivastava <aayushsrivastava.work@gmail.com>
  • Loading branch information
aayushsrivastava committed Jan 13, 2025
1 parent f4ad40b commit 7ff94ad
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
10 changes: 7 additions & 3 deletions ui/src/features/assemble-freight/assemble-freight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,18 @@ export const AssembleFreight = ({
);
};

const DiscoveryTable = ({ selected, chosenItems, select }: {
selected?: DiscoveryResult,
const DiscoveryTable = ({
selected,
chosenItems,
select
}: {
selected?: DiscoveryResult;
chosenItems: {
[key: string]: {
artifact: DiscoveryResult;
info: FreightInfo;
};
},
};
select: (item?: FreightInfo) => void;
}) => {
if (!selected) {
Expand Down
7 changes: 5 additions & 2 deletions ui/src/features/assemble-freight/chart-table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { calculatePageForSelectedRow } from '@ui/utils/pagination';
import { Radio, Table } from 'antd';
import { useState } from 'react';

import { calculatePageForSelectedRow } from '@ui/utils/pagination';

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

return (
<Table
Expand Down
8 changes: 5 additions & 3 deletions ui/src/features/assemble-freight/commit-table.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Radio, Table } from 'antd';
import { useState } from 'react';

import { DiscoveredCommit } from '@ui/gen/v1alpha1/generated_pb';
import { timestampDate } from '@ui/utils/connectrpc-utils';
import { calculatePageForSelectedRow } from '@ui/utils/pagination';

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

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

return (
<>
Expand Down
14 changes: 9 additions & 5 deletions ui/src/features/assemble-freight/image-table.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { faQuestionCircle } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Radio, Table } from 'antd';
import { useState } from 'react';

import { DiscoveredImageReference } from '@ui/gen/v1alpha1/generated_pb';
import { timestampDate } from '@ui/utils/connectrpc-utils';
import { calculatePageForSelectedRow } from '@ui/utils/pagination';

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

export const ImageTable = ({
references,
Expand All @@ -18,7 +18,9 @@ export const ImageTable = ({
selected: DiscoveredImageReference | undefined;
select: (reference?: DiscoveredImageReference) => void;
}) => {
const [defaultPage] = useState<number>(() => calculatePageForSelectedRow(selected, references, (ref) => ref.tag));
const [defaultPage] = useState<number>(() =>
calculatePageForSelectedRow(selected, references, (ref) => ref.tag)
);

return (
<>
Expand All @@ -37,7 +39,9 @@ export const ImageTable = ({
{ title: 'Tag', dataIndex: 'tag' },
{
title: 'Digest',
render: (record: DiscoveredImageReference) => <TruncatedCopyable text={record?.digest} />
render: (record: DiscoveredImageReference) => (
<TruncatedCopyable text={record?.digest} />
)
},
{
title: 'Source Repo',
Expand All @@ -58,5 +62,5 @@ export const ImageTable = ({
]}
/>
</>
)
);
};
28 changes: 16 additions & 12 deletions ui/src/utils/pagination.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { describe, expect, test } from "vitest";
import { calculatePageForSelectedRow } from "./pagination";
import { describe, expect, test } from 'vitest';

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);
});
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);
}
);
});
22 changes: 13 additions & 9 deletions ui/src/utils/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export function calculatePageForSelectedRow<T>(selectedOption: T | undefined, options: T[], key: (option: T) => string): number {
const pageSize = 10;
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;
}
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;
}
return 1;
}

0 comments on commit 7ff94ad

Please sign in to comment.