Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/ui/src/ui-component/dialog/ShareWithWorkspaceDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackba
import { cloneDeep } from 'lodash'

// Material
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Box, Stack, OutlinedInput, Typography } from '@mui/material'
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Box, Stack, OutlinedInput, Typography, Checkbox, FormControlLabel } from '@mui/material'

// Project imports
import { StyledButton } from '@/ui-component/button/StyledButton'
Expand Down Expand Up @@ -47,6 +47,28 @@ const ShareWithWorkspaceDialog = ({ show, dialogProps, onCancel, setError }) =>
const [outputSchema, setOutputSchema] = useState([])

const [name, setName] = useState('')
const [selectAll, setSelectAll] = useState(false)

// Handle select all / deselect all
const handleSelectAllChange = (event) => {
const checked = event.target.checked
setSelectAll(checked)
setOutputSchema((prevRows) => {
return prevRows.map((row) => ({
...row,
shared: checked
}))
})
}

// Update selectAll state when individual rows change
useEffect(() => {
if (outputSchema.length > 0) {
const allSelected = outputSchema.every((row) => row.shared)
const noneSelected = outputSchema.every((row) => !row.shared)
setSelectAll(allSelected ? true : noneSelected ? false : false)
}
}, [outputSchema])
Comment on lines +50 to +71
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The selectAll state can be simplified by deriving it directly from outputSchema using useMemo. This approach is more idiomatic in React, avoids redundant state updates (currently setSelectAll is called both in the change handler and the effect), and prevents potential synchronization issues. For instance, if outputSchema becomes empty, the current useEffect won't run due to the length check, potentially leaving selectAll in a stale state. Additionally, the ternary logic in the current effect is redundant as it evaluates to the same value as allSelected.

Suggested change
const [selectAll, setSelectAll] = useState(false)
// Handle select all / deselect all
const handleSelectAllChange = (event) => {
const checked = event.target.checked
setSelectAll(checked)
setOutputSchema((prevRows) => {
return prevRows.map((row) => ({
...row,
shared: checked
}))
})
}
// Update selectAll state when individual rows change
useEffect(() => {
if (outputSchema.length > 0) {
const allSelected = outputSchema.every((row) => row.shared)
const noneSelected = outputSchema.every((row) => !row.shared)
setSelectAll(allSelected ? true : noneSelected ? false : false)
}
}, [outputSchema])
const selectAll = useMemo(() => outputSchema.length > 0 && outputSchema.every((row) => row.shared), [outputSchema])
const isIndeterminate = useMemo(() => outputSchema.some((row) => row.shared) && !outputSchema.every((row) => row.shared), [outputSchema])
// Handle select all / deselect all
const handleSelectAllChange = (event) => {
const checked = event.target.checked
setOutputSchema((prevRows) => {
return prevRows.map((row) => ({
...row,
shared: checked
}))
})
}


const onRowUpdate = (newRow) => {
setTimeout(() => {
Expand Down Expand Up @@ -187,6 +209,13 @@ const ShareWithWorkspaceDialog = ({ show, dialogProps, onCancel, setError }) =>
<OutlinedInput id='name' type='string' disabled={true} fullWidth placeholder={name} value={name} name='name' />
</Box>
<Box sx={{ p: 2 }}>
<Stack direction='row' alignItems='center' justifyContent='space-between' sx={{ mb: 1 }}>
<Typography variant='overline'>Workspaces</Typography>
<FormControlLabel
control={<Checkbox checked={selectAll} onChange={handleSelectAllChange} size='small' />}
label={<Typography variant='body2'>Select All</Typography>}
/>
Comment on lines +214 to +217
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to use the indeterminate prop on the 'Select All' checkbox to provide better visual feedback when only some workspaces are selected. This works in conjunction with the isIndeterminate value suggested in the state management feedback.

Suggested change
<FormControlLabel
control={<Checkbox checked={selectAll} onChange={handleSelectAllChange} size='small' />}
label={<Typography variant='body2'>Select All</Typography>}
/>
<FormControlLabel
control={<Checkbox checked={selectAll} indeterminate={isIndeterminate} onChange={handleSelectAllChange} size='small' />}
label={<Typography variant='body2'>Select All</Typography>}
/>

</Stack>
<Grid columns={columns} rows={outputSchema} onRowUpdate={onRowUpdate} />
</Box>
</DialogContent>
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/ui-component/tooltip/MoreItemsTooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tooltip, Typography } from '@mui/material'
import { styled } from '@mui/material/styles'
import PropTypes from 'prop-types'

const StyledOl = styled('ol')(() => ({
const StyledUl = styled('ul')(() => ({
paddingLeft: 20,
margin: 0
}))
Expand All @@ -17,13 +17,13 @@ const MoreItemsTooltip = ({ images, children }) => {
return (
<Tooltip
title={
<StyledOl>
<StyledUl>
{images.map((img) => (
<StyledLi key={img.imageSrc || img.label}>
<Typography>{img.label}</Typography>
</StyledLi>
))}
</StyledOl>
</StyledUl>
}
placement='top'
>
Expand Down