-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
833 additions
and
280 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
weave-js/src/components/PagePanelComponents/Home/Browse3/grid/pagination.ts
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
215 changes: 215 additions & 0 deletions
215
.../src/components/PagePanelComponents/Home/Browse3/pages/CallPage/EditableDataTableView.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,215 @@ | ||
import {Box} from '@mui/material'; | ||
import {GridPaginationModel, GridRenderCellParams} from '@mui/x-data-grid-pro'; | ||
import React, {FC, useCallback, useMemo, useState} from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import {isWeaveObjectRef, parseRefMaybe} from '../../../../../../react'; | ||
import {Tooltip} from '../../../../../Tooltip'; | ||
import {flattenObjectPreservingWeaveTypes} from '../../../Browse2/browse2Util'; | ||
import {DEFAULT_PAGE_SIZE} from '../../grid/pagination'; | ||
import {StyledDataGrid} from '../../StyledDataGrid'; | ||
import {A} from '../common/Links'; | ||
import {useEditContext} from '../EditContext'; // Import the context | ||
import {useWFHooks} from '../wfReactInterface/context'; | ||
import {SortBy} from '../wfReactInterface/traceServerClientTypes'; | ||
import {RowId} from './DataTableView'; // Import shared components | ||
|
||
type DatasetObjectVal = { | ||
_type: 'Dataset'; | ||
name: string | null; | ||
description: string | null; | ||
rows: string; | ||
_class_name: 'Dataset'; | ||
_bases: ['Object', 'BaseModel']; | ||
}; | ||
|
||
interface EditableDataTableViewProps { | ||
datasetObjectId: string; | ||
datasetObject: DatasetObjectVal; | ||
fullHeight?: boolean; | ||
} | ||
|
||
// Add styled component for edited cells | ||
const EditedCellHighlightWrapper = styled(Box)` | ||
.edited-cell { | ||
background-color: rgba(25, 118, 210, 0.1); | ||
border-radius: 8px; | ||
transition: background-color 0.5s ease; | ||
} | ||
`; | ||
|
||
export const EditableDataTableView: FC<EditableDataTableViewProps> = props => { | ||
const {useTableRowsQuery, useTableQueryStats} = useWFHooks(); | ||
const [sortBy] = useState<SortBy[]>([]); | ||
const {editedCellsMap, processRowUpdate, setRowIndices} = useEditContext(); // Use the context | ||
|
||
const [paginationModel, setPaginationModel] = useState<GridPaginationModel>({ | ||
page: 0, | ||
pageSize: DEFAULT_PAGE_SIZE, | ||
}); | ||
|
||
// Parse table ref | ||
const parsedRef = useMemo( | ||
() => parseRefMaybe(props.datasetObject.rows), | ||
[props.datasetObject.rows] | ||
); | ||
const lookupKey = useMemo(() => { | ||
if ( | ||
parsedRef == null || | ||
!isWeaveObjectRef(parsedRef) || | ||
parsedRef.weaveKind !== 'table' | ||
) { | ||
return null; | ||
} | ||
return { | ||
entity: parsedRef.entityName, | ||
project: parsedRef.projectName, | ||
digest: parsedRef.artifactVersion, | ||
}; | ||
}, [parsedRef]); | ||
|
||
// Fetch row count | ||
const numRowsQuery = useTableQueryStats( | ||
lookupKey?.entity ?? '', | ||
lookupKey?.project ?? '', | ||
lookupKey?.digest ?? '', | ||
{skip: lookupKey == null} | ||
); | ||
|
||
// Fetch rows | ||
const fetchQuery = useTableRowsQuery( | ||
lookupKey?.entity ?? '', | ||
lookupKey?.project ?? '', | ||
lookupKey?.digest ?? '', | ||
undefined, | ||
paginationModel.pageSize, | ||
paginationModel.page * paginationModel.pageSize, | ||
sortBy, | ||
{skip: lookupKey == null} | ||
); | ||
|
||
// Convert data to list of dictionaries and flatten nested objects | ||
const dataAsListOfDict = useMemo(() => { | ||
return (fetchQuery.result?.rows ?? []).map(row => { | ||
let val = row; | ||
if (val == null) { | ||
return {}; | ||
} else if (typeof val === 'object' && !Array.isArray(val)) { | ||
if ('val' in val) { | ||
val = val.val; // Extract val field | ||
} | ||
return flattenObjectPreservingWeaveTypes(val); | ||
} | ||
return {'': val}; | ||
}); | ||
}, [fetchQuery.result?.rows]); | ||
|
||
// Reapply edits when rows are fetched | ||
const rows = useMemo(() => { | ||
if (!fetchQuery.loading && fetchQuery.result?.rows) { | ||
return dataAsListOfDict.map((row, i) => { | ||
const digest = fetchQuery.result!.rows[i].digest; | ||
const rowKey = `${digest}`; | ||
const editedRow = editedCellsMap.get(rowKey); | ||
const baseRow = editedRow ? {...row, ...editedRow} : row; | ||
setRowIndices(prev => { | ||
const updatedMap = new Map(prev); | ||
updatedMap.set(rowKey, i); | ||
return updatedMap; | ||
}); | ||
return { | ||
id: digest, | ||
...baseRow, | ||
}; | ||
}); | ||
} | ||
return []; | ||
}, [ | ||
fetchQuery.loading, | ||
fetchQuery.result, | ||
dataAsListOfDict, | ||
editedCellsMap, | ||
setRowIndices, | ||
]); | ||
|
||
// Generate columns with cell class names for edited cells | ||
const columns = useMemo(() => { | ||
const firstRow = rows[0] ?? {}; | ||
return Object.keys(firstRow).map(field => { | ||
if (field === 'id') { | ||
return { | ||
field, | ||
headerName: 'id', | ||
width: 50, | ||
editable: false, | ||
filterable: false, | ||
sortable: false, | ||
renderCell: (params: GridRenderCellParams) => { | ||
const id = params.value; | ||
const rowLabel = id ? id.slice(-4) : params.id; | ||
const rowSpan = ( | ||
<Tooltip trigger={<RowId>{rowLabel}</RowId>} content={id} /> | ||
); | ||
return <A onClick={() => {}}>{rowSpan}</A>; | ||
}, | ||
}; | ||
} | ||
return { | ||
field, | ||
headerName: field, | ||
flex: 1, | ||
editable: true, | ||
sortable: false, | ||
filterable: false, | ||
cellClassName: (params: any) => { | ||
const rowKey = `${params.row.id}`; | ||
const editedRow = editedCellsMap.get(rowKey); | ||
return editedRow && editedRow[field] !== undefined | ||
? 'edited-cell' | ||
: ''; | ||
}, | ||
}; | ||
}); | ||
}, [rows, editedCellsMap]); | ||
|
||
// Handle pagination model change | ||
const handlePaginationModelChange = useCallback( | ||
(newModel: GridPaginationModel) => { | ||
setPaginationModel(newModel); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
width: '100%', | ||
height: props.fullHeight ? '100%' : 'inherit', | ||
}}> | ||
<EditedCellHighlightWrapper> | ||
<StyledDataGrid | ||
density="compact" | ||
rows={rows} | ||
columns={columns} | ||
editMode="cell" | ||
pagination={true} | ||
paginationMode="server" | ||
paginationModel={paginationModel} | ||
onPaginationModelChange={handlePaginationModelChange} | ||
rowCount={numRowsQuery.result?.count ?? 0} | ||
pageSizeOptions={[5, 10, 20, 50, 100]} | ||
disableMultipleColumnsSorting | ||
loading={fetchQuery.loading} | ||
disableRowSelectionOnClick | ||
keepBorders={false} | ||
sx={{ | ||
border: 'none', | ||
}} | ||
processRowUpdate={processRowUpdate} | ||
/> | ||
</EditedCellHighlightWrapper> | ||
</div> | ||
); | ||
}; |
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
21 changes: 21 additions & 0 deletions
21
weave-js/src/components/PagePanelComponents/Home/Browse3/pages/EditContext.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,21 @@ | ||
import React, {createContext, useContext} from 'react'; | ||
|
||
interface EditContextType { | ||
editedCellsMap: Map<string, any>; | ||
setEditedCellsMap: React.Dispatch<React.SetStateAction<Map<string, any>>>; | ||
rowIndices: Map<string, number>; | ||
setRowIndices: React.Dispatch<React.SetStateAction<Map<string, number>>>; | ||
processRowUpdate: (newRow: any, oldRow: any) => any; | ||
} | ||
|
||
export const EditContext = createContext<EditContextType | undefined>( | ||
undefined | ||
); | ||
|
||
export const useEditContext = () => { | ||
const context = useContext(EditContext); | ||
if (!context) { | ||
throw new Error('useEditContext must be used within an EditProvider'); | ||
} | ||
return context; | ||
}; |
Oops, something went wrong.