forked from evergreen-ci/spruce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVG-20806 Refactor files table to use LG table component (evergreen-c…
- Loading branch information
Showing
15 changed files
with
475 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,7 @@ | |
"@leafygreen-ui/segmented-control": "8.2.6", | ||
"@leafygreen-ui/select": "10.2.0", | ||
"@leafygreen-ui/side-nav": "13.0.2", | ||
"@leafygreen-ui/skeleton-loader": "1.1.0", | ||
"@leafygreen-ui/table": "10.0.1", | ||
"@leafygreen-ui/table/new": "npm:@leafygreen-ui/[email protected]", | ||
"@leafygreen-ui/tabs": "11.0.4", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useRef } from "react"; | ||
import { LGColumnDef, useLeafyGreenTable } from "@leafygreen-ui/table/new"; | ||
import { CustomStoryObj, CustomMeta } from "test_utils/types"; | ||
import { BaseTable } from "./BaseTable"; | ||
|
||
export default { | ||
component: BaseTable, | ||
} satisfies CustomMeta<typeof BaseTable>; | ||
|
||
export const Default: CustomStoryObj<typeof BaseTable> = { | ||
render: (args) => <TemplateComponent {...args} data={fullArray} />, | ||
args: { | ||
shouldAlternateRowColor: true, | ||
darkMode: false, | ||
}, | ||
}; | ||
|
||
export const EmptyState: CustomStoryObj<typeof BaseTable> = { | ||
render: (args) => <TemplateComponent {...args} data={[]} />, | ||
args: { | ||
shouldAlternateRowColor: true, | ||
darkMode: false, | ||
}, | ||
}; | ||
interface DataShape { | ||
name: string; | ||
column1: string; | ||
column2: string; | ||
} | ||
const fullArray: DataShape[] = Array.from({ length: 100 }, (_, i) => ({ | ||
name: `name ${i}`, | ||
column1: `column1 ${i}`, | ||
column2: `column2 ${i}`, | ||
})); | ||
const columns: LGColumnDef<DataShape>[] = [ | ||
{ | ||
accessorKey: "name", | ||
header: "Name", | ||
size: 60, | ||
enableSorting: true, | ||
}, | ||
{ | ||
accessorKey: "column1", | ||
header: "Column 1", | ||
size: 60, | ||
enableSorting: true, | ||
}, | ||
{ | ||
accessorKey: "column2", | ||
header: "Column 2", | ||
size: 60, | ||
enableSorting: true, | ||
}, | ||
]; | ||
|
||
const TemplateComponent: React.FC< | ||
React.ComponentProps<typeof BaseTable> & { | ||
data: DataShape[]; | ||
} | ||
> = (args) => { | ||
const { data, ...rest } = args; | ||
const tableContainerRef = useRef<HTMLDivElement>(null); | ||
const table = useLeafyGreenTable<DataShape>({ | ||
data, | ||
columns, | ||
containerRef: tableContainerRef, | ||
}); | ||
|
||
return <BaseTable {...rest} table={table} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { CustomStoryObj, CustomMeta } from "test_utils/types"; | ||
|
||
import TableControl from "."; | ||
|
||
export default { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/pages/task/taskTabs/FileTable/GroupedFileTable/GroupedFileTable.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { CustomStoryObj, CustomMeta } from "test_utils/types"; | ||
import GroupedFileTable from "."; | ||
|
||
const files = [ | ||
{ | ||
name: "some_file", | ||
link: "some_link", | ||
}, | ||
{ | ||
name: "another_file", | ||
link: "another_link", | ||
}, | ||
]; | ||
|
||
export default { | ||
title: "Pages/Task/table/GroupedFileTable", | ||
component: GroupedFileTable, | ||
} satisfies CustomMeta<typeof GroupedFileTable>; | ||
|
||
export const DefaultTable: CustomStoryObj<typeof GroupedFileTable> = { | ||
render: (args) => <GroupedFileTable {...args} />, | ||
args: { | ||
taskName: "Task 1", | ||
files, | ||
}, | ||
}; |
58 changes: 58 additions & 0 deletions
58
src/pages/task/taskTabs/FileTable/GroupedFileTable/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useRef } from "react"; | ||
import styled from "@emotion/styled"; | ||
import { useLeafyGreenTable, LGColumnDef } from "@leafygreen-ui/table/new"; | ||
import { Subtitle } from "@leafygreen-ui/typography"; | ||
import { StyledLink } from "components/styles"; | ||
import { BaseTable } from "components/Table/BaseTable"; | ||
import { size } from "constants/tokens"; | ||
import { Unpacked } from "types/utils"; | ||
import { GroupedFiles } from "../types"; | ||
|
||
type GroupedFilesFile = Unpacked<GroupedFiles["files"]>; | ||
|
||
const columns: LGColumnDef<GroupedFilesFile>[] = [ | ||
{ | ||
accessorKey: "name", | ||
header: "Name", | ||
size: 60, | ||
enableSorting: true, | ||
cell: (value) => ( | ||
<StyledLink | ||
href={value.row.original.link} | ||
data-cy="file-link" | ||
target="_blank" | ||
> | ||
{value.getValue()} | ||
</StyledLink> | ||
), | ||
}, | ||
]; | ||
|
||
interface GroupedFileTableProps { | ||
files: GroupedFilesFile[]; | ||
taskName?: string; | ||
} | ||
const GroupedFileTable: React.FC<GroupedFileTableProps> = ({ | ||
files, | ||
taskName, | ||
}) => { | ||
const tableContainerRef = useRef<HTMLDivElement>(null); | ||
|
||
const table = useLeafyGreenTable<GroupedFilesFile>({ | ||
containerRef: tableContainerRef, | ||
data: files, | ||
columns, | ||
}); | ||
|
||
return ( | ||
<Container> | ||
{taskName && <Subtitle>{taskName}</Subtitle>} | ||
<BaseTable table={table} shouldAlternateRowColor /> | ||
</Container> | ||
); | ||
}; | ||
|
||
const Container = styled.div` | ||
margin-bottom: ${size.m}; | ||
`; | ||
export default GroupedFileTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useState } from "react"; | ||
import { useQuery } from "@apollo/client"; | ||
import styled from "@emotion/styled"; | ||
import { SearchInput } from "@leafygreen-ui/search-input"; | ||
import { Skeleton, TableSkeleton } from "@leafygreen-ui/skeleton-loader"; | ||
import { Body } from "@leafygreen-ui/typography"; | ||
import { size } from "constants/tokens"; | ||
import { useToastContext } from "context/toast"; | ||
import { TaskFilesQuery, TaskFilesQueryVariables } from "gql/generated/types"; | ||
import { GET_TASK_FILES } from "gql/queries"; | ||
import GroupedFileTable from "./GroupedFileTable"; | ||
import { filterGroupedFiles } from "./utils"; | ||
|
||
interface FileTableProps { | ||
taskId: string; | ||
execution: number; | ||
} | ||
const FileTable: React.FC<FileTableProps> = ({ execution, taskId }) => { | ||
const [search, setSearch] = useState(""); | ||
const dispatchToast = useToastContext(); | ||
const { data, loading } = useQuery<TaskFilesQuery, TaskFilesQueryVariables>( | ||
GET_TASK_FILES, | ||
{ | ||
variables: { | ||
taskId, | ||
execution, | ||
}, | ||
onError: (err) => { | ||
dispatchToast.error(`Unable to load task files: ${err}`); | ||
}, | ||
} | ||
); | ||
const { taskFiles } = data?.task ?? {}; | ||
|
||
const { groupedFiles = [] } = taskFiles ?? {}; | ||
const filteredGroupedFiles = filterGroupedFiles(groupedFiles, search); | ||
|
||
// We only want to show the file group name if there are multiple file groups. | ||
const hasMultipleFileGroups = groupedFiles.length > 1; | ||
|
||
return loading ? ( | ||
<FilesTableSkeleton /> | ||
) : ( | ||
<> | ||
<StyledSearchInput | ||
aria-label="Search file names" | ||
placeholder="Search file names" | ||
onChange={(e) => setSearch(e.target.value)} | ||
value={search} | ||
data-cy="file-search-input" | ||
/> | ||
{filteredGroupedFiles.length === 0 && <Body>No files found</Body>} | ||
{filteredGroupedFiles.map((groupedFile) => ( | ||
<GroupedFileTable | ||
key={groupedFile?.taskName} | ||
files={groupedFile?.files} | ||
taskName={hasMultipleFileGroups && groupedFile?.taskName} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
const FilesTableSkeleton = () => ( | ||
<> | ||
<Skeleton /> | ||
<TableSkeleton numCols={1} numRows={5} /> | ||
</> | ||
); | ||
const StyledSearchInput = styled(SearchInput)` | ||
margin-bottom: ${size.m}; | ||
width: 400px; | ||
`; | ||
export default FileTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { TaskFilesQuery } from "gql/generated/types"; | ||
import { Unpacked } from "types/utils"; | ||
|
||
export type GroupedFiles = Unpacked< | ||
TaskFilesQuery["task"]["taskFiles"]["groupedFiles"] | ||
>; |
Oops, something went wrong.