-
Notifications
You must be signed in to change notification settings - Fork 5
adds network-modification-table #1063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Mathieu-Deharbe
wants to merge
5
commits into
main
Choose a base branch
from
network-modification-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88cc5d1
adds network-modification-table
Mathieu-Deharbe 83cbdd8
extracts createBaseColumns and reorganizes
Mathieu-Deharbe 14f26b8
Merge branch 'main' into network-modification-table
Mathieu-Deharbe 9fb98f3
updated in order to work with gridstudy
Mathieu-Deharbe 6574685
licenses
Mathieu-Deharbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
49 changes: 49 additions & 0 deletions
49
src/components/network-modification-table/columns-definition.tsx
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * Copyright (c) 2026, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| import { | ||
| NetworkModificationEditorNameHeaderProps, | ||
| } from './renderers'; | ||
|
|
||
| const CHIP_PADDING_PX = 24; | ||
| const CHAR_WIDTH_PX = 8; | ||
| const COLUMN_PADDING_PX = 12; | ||
| const MIN_COLUMN_SIZE = 40; | ||
|
|
||
| export const computeTagMinSize = (tag: string): number => { | ||
| const chipContentWidth = tag.length * CHAR_WIDTH_PX + CHIP_PADDING_PX; | ||
| return Math.max(chipContentWidth + COLUMN_PADDING_PX, MIN_COLUMN_SIZE); | ||
| }; | ||
|
|
||
| export const BASE_MODIFICATION_TABLE_COLUMNS = { | ||
| DRAG_HANDLE: { | ||
| id: 'dragHandle', | ||
| autoExtensible: false, | ||
| }, | ||
| SELECT: { | ||
| id: 'select', | ||
| autoExtensible: false, | ||
| }, | ||
| NAME: { | ||
| id: 'modificationName', | ||
| autoExtensible: true, | ||
| }, | ||
| DESCRIPTION: { | ||
| id: 'modificationDescription', | ||
| autoExtensible: false, | ||
| }, | ||
| SWITCH: { | ||
| id: 'switch', | ||
| autoExtensible: false, | ||
| }, | ||
| }; | ||
|
|
||
| export const AUTO_EXTENSIBLE_COLUMNS = Object.values(BASE_MODIFICATION_TABLE_COLUMNS) | ||
| .filter((column) => column.autoExtensible) | ||
| .map((column) => column.id); | ||
|
|
||
| export type NameHeaderProps = Omit<NetworkModificationEditorNameHeaderProps, 'modificationCount'>; | ||
This file contains hidden or 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,14 @@ | ||
| /** | ||
| import NetworkModificationsTable from './network-modification-table/network-modifications-table'; | ||
| * Copyright (c) 2026, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| export * from './network-table-styles'; | ||
| export * from './columns-definition'; | ||
| export * from './network-modifications-table'; | ||
| export * from './use-modifications-drag-and-drop'; | ||
| export * from './renderers'; | ||
| export * from './row'; | ||
158 changes: 158 additions & 0 deletions
158
src/components/network-modification-table/network-modifications-table.tsx
This file contains hidden or 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,158 @@ | ||
| /** | ||
| * Copyright (c) 2026, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| import React, { Dispatch, SetStateAction, useEffect, useMemo, useRef } from 'react'; | ||
| import { Box, Table, TableBody, TableCell, TableHead, TableRow, useTheme } from '@mui/material'; | ||
| import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table'; | ||
| import { DragDropContext, DragStart, Droppable, DroppableProvided, DropResult } from '@hello-pangea/dnd'; | ||
| import { useVirtualizer } from '@tanstack/react-virtual'; | ||
| import { UUID } from 'node:crypto'; | ||
Mathieu-Deharbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { NetworkModificationEditorNameHeaderProps } from './renderers'; | ||
| import { AUTO_EXTENSIBLE_COLUMNS, NameHeaderProps } from './columns-definition'; | ||
| import { useModificationsDragAndDrop } from './use-modifications-drag-and-drop'; | ||
| import { NetworkModificationMetadata } from '../../hooks'; | ||
| import { createHeaderCellStyle, MODIFICATION_ROW_HEIGHT, networkTableStyles } from './network-table-styles'; | ||
| import { ModificationRow } from './row'; | ||
|
|
||
| interface NetworkModificationsTableProps extends Omit<NetworkModificationEditorNameHeaderProps, 'modificationCount'> { | ||
| modifications: NetworkModificationMetadata[]; | ||
| setModifications: Dispatch<SetStateAction<NetworkModificationMetadata[]>>; | ||
| handleCellClick: (modification: NetworkModificationMetadata) => void; | ||
| isRowDragDisabled?: boolean; | ||
| onRowDragStart: (event: DragStart) => void; | ||
| onRowDragEnd: (event: DropResult) => void; | ||
| onRowSelected: (selectedRows: NetworkModificationMetadata[]) => void; | ||
| createAllColumns: (isRowDragDisabled: boolean, | ||
| modificationsCount: number, | ||
| nameHeaderProps: NameHeaderProps, | ||
| setModifications: React.Dispatch<SetStateAction<NetworkModificationMetadata[]>> | ||
| ) => ColumnDef<NetworkModificationMetadata, any>[]; | ||
| highlightedModificationUuid: UUID | null; | ||
| } | ||
|
|
||
| export function NetworkModificationsTable({ | ||
| modifications, | ||
| setModifications, | ||
| handleCellClick, | ||
| isRowDragDisabled = false, | ||
| onRowDragStart, | ||
| onRowDragEnd, | ||
| onRowSelected, | ||
| createAllColumns, | ||
| highlightedModificationUuid, | ||
| ...nameHeaderProps | ||
| }: Readonly<NetworkModificationsTableProps>) { | ||
| const theme = useTheme(); | ||
|
|
||
| const containerRef = useRef<HTMLDivElement | null>(null); | ||
| const lastClickedIndex = useRef<number | null>(null); | ||
|
|
||
| const columns = useMemo<ColumnDef<NetworkModificationMetadata>[]>(() => | ||
| createAllColumns(isRowDragDisabled ?? false, | ||
| modifications.length, | ||
| nameHeaderProps, | ||
| setModifications) | ||
| , [ | ||
|
Check failure on line 59 in src/components/network-modification-table/network-modifications-table.tsx
|
||
| isRowDragDisabled, | ||
| modifications, | ||
| nameHeaderProps, | ||
| setModifications, | ||
| ]); | ||
Mathieu-Deharbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const table = useReactTable({ | ||
| data: modifications, | ||
| columns, | ||
| getCoreRowModel: getCoreRowModel(), | ||
| getRowId: (row:any) => row.uuid, | ||
| enableRowSelection: true, | ||
| meta: { lastClickedIndex, onRowSelected }, | ||
| }); | ||
|
|
||
| const { rows } = table.getRowModel(); | ||
|
|
||
| const virtualizer = useVirtualizer({ | ||
| count: rows.length, | ||
| getScrollElement: () => containerRef.current, | ||
| overscan: 5, | ||
| estimateSize: () => MODIFICATION_ROW_HEIGHT, | ||
| }); | ||
| const virtualItems = virtualizer.getVirtualItems(); | ||
|
|
||
| const { handleDragUpdate, handleDragEnd, renderClone } = useModificationsDragAndDrop({ | ||
| rows, | ||
| containerRef, | ||
| onRowDragEnd, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| table.resetRowSelection(); | ||
| lastClickedIndex.current = null; | ||
| }, [table]); | ||
Mathieu-Deharbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| useEffect(() => { | ||
| if (highlightedModificationUuid && containerRef.current) { | ||
| const rowIndex = rows.findIndex((row) => row.original.uuid === highlightedModificationUuid); | ||
| if (rowIndex !== -1) { | ||
| virtualizer.scrollToIndex(rowIndex, { align: 'start', behavior: 'auto' }); | ||
| } | ||
| } | ||
| }, [highlightedModificationUuid, rows, virtualizer]); | ||
|
|
||
| return ( | ||
| <DragDropContext onDragEnd={handleDragEnd} onDragStart={onRowDragStart} onDragUpdate={handleDragUpdate}> | ||
| <Box sx={networkTableStyles.tableWrapper}> | ||
| <Droppable droppableId="modifications-table" mode="virtual" renderClone={renderClone}> | ||
| {(provided: DroppableProvided) => ( | ||
| <Box ref={containerRef} sx={networkTableStyles.container}> | ||
| <Table sx={networkTableStyles.table}> | ||
| <TableHead sx={networkTableStyles.thead}> | ||
| {table.getHeaderGroups().map((headerGroup) => ( | ||
| <TableRow key={headerGroup.id} sx={networkTableStyles.tableRow}> | ||
| {headerGroup.headers.map((header) => ( | ||
| <TableCell | ||
| key={header.id} | ||
| sx={createHeaderCellStyle( | ||
| header, | ||
| theme, | ||
| header.index === 0, | ||
| header.index === headerGroup.headers.length - 1, | ||
| AUTO_EXTENSIBLE_COLUMNS.includes(header.column.id) | ||
| )} | ||
| > | ||
| {flexRender(header.column.columnDef.header, header.getContext())} | ||
| </TableCell> | ||
| ))} | ||
| </TableRow> | ||
| ))} | ||
| </TableHead> | ||
| <TableBody | ||
| ref={provided.innerRef} | ||
| {...provided.droppableProps} | ||
| sx={{ ...networkTableStyles.tableBody, height: `${virtualizer.getTotalSize()}px` }} | ||
| > | ||
| {virtualItems.map((virtualRow) => { | ||
| const row = rows[virtualRow.index]; | ||
| return ( | ||
| <ModificationRow | ||
| key={row.id} | ||
| virtualRow={virtualRow} | ||
| row={row} | ||
| handleCellClick={handleCellClick} | ||
| isRowDragDisabled={isRowDragDisabled} | ||
| highlightedModificationUuid={highlightedModificationUuid} | ||
| /> | ||
| ); | ||
| })} | ||
| </TableBody> | ||
| </Table> | ||
| </Box> | ||
| )} | ||
| </Droppable> | ||
| </Box> | ||
| </DragDropContext> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.