-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
71a0ca5
commit 2dea0c8
Showing
15 changed files
with
182 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@siafoundation/design-system': minor | ||
--- | ||
|
||
Add handleBatchOperation method. |
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,5 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
The contracts multi-select menu now supports bulk deletion. |
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
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
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
66 changes: 66 additions & 0 deletions
66
apps/renterd/components/Contracts/ContractsBatchMenu/ContractsBatchDelete.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,66 @@ | ||
import { | ||
Button, | ||
handleBatchOperation, | ||
Paragraph, | ||
} from '@siafoundation/design-system' | ||
import { Delete16 } from '@siafoundation/react-icons' | ||
import { useContractDelete } from '@siafoundation/renterd-react' | ||
import { useCallback, useMemo } from 'react' | ||
import { useDialog } from '../../../contexts/dialog' | ||
import { useContracts } from '../../../contexts/contracts' | ||
import { pluralize } from '@siafoundation/units' | ||
|
||
export function ContractsBatchDelete() { | ||
const { multiSelect } = useContracts() | ||
|
||
const ids = useMemo( | ||
() => Object.entries(multiSelect.selectionMap).map(([_, item]) => item.id), | ||
[multiSelect.selectionMap] | ||
) | ||
const { openConfirmDialog } = useDialog() | ||
const deleteContract = useContractDelete() | ||
const deleteAll = useCallback(async () => { | ||
await handleBatchOperation( | ||
ids.map((id) => deleteContract.delete({ params: { id } })), | ||
{ | ||
toastError: ({ successCount, errorCount, totalCount }) => ({ | ||
title: `${pluralize(successCount, 'contract')} deleted`, | ||
body: `Error deleting ${errorCount}/${totalCount} total contracts.`, | ||
}), | ||
toastSuccess: ({ totalCount }) => ({ | ||
title: `${pluralize(totalCount, 'contract')} deleted`, | ||
}), | ||
after: () => { | ||
multiSelect.deselectAll() | ||
}, | ||
} | ||
) | ||
}, [multiSelect, ids, deleteContract]) | ||
|
||
return ( | ||
<Button | ||
aria-label="delete selected contracts" | ||
tip="Delete selected contracts" | ||
onClick={() => { | ||
openConfirmDialog({ | ||
title: `Delete contracts`, | ||
action: 'Delete', | ||
variant: 'red', | ||
body: ( | ||
<div className="flex flex-col gap-1"> | ||
<Paragraph size="14"> | ||
Are you sure you would like to delete the{' '} | ||
{pluralize(multiSelect.selectionCount, 'selected contract')}? | ||
</Paragraph> | ||
</div> | ||
), | ||
onConfirm: async () => { | ||
deleteAll() | ||
}, | ||
}) | ||
}} | ||
> | ||
<Delete16 /> | ||
</Button> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/renterd/components/Contracts/ContractsBatchMenu/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,13 @@ | ||
import { MultiSelectionMenu } from '@siafoundation/design-system' | ||
import { useContracts } from '../../../contexts/contracts' | ||
import { ContractsBatchDelete } from './ContractsBatchDelete' | ||
|
||
export function ContractsBatchMenu() { | ||
const { multiSelect } = useContracts() | ||
|
||
return ( | ||
<MultiSelectionMenu multiSelect={multiSelect} entityWord="contract"> | ||
<ContractsBatchDelete /> | ||
</MultiSelectionMenu> | ||
) | ||
} |
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
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
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,38 @@ | ||
import { ToastParams, triggerErrorToast, triggerSuccessToast } from './toast' | ||
|
||
type Results = { | ||
totalCount: number | ||
errorCount: number | ||
successCount: number | ||
} | ||
|
||
type Params = { | ||
toastSuccess: (results: Results) => ToastParams | ||
toastError: (results: Results) => ToastParams | ||
after?: () => void | Promise<void> | ||
} | ||
|
||
export async function handleBatchOperation<T>( | ||
operations: Promise<{ data?: T; error?: string }>[], | ||
params: Params | ||
) { | ||
const totalCount = operations.length | ||
let errorCount = 0 | ||
const results = await Promise.all(operations) | ||
for (const r of results) { | ||
if (r.error) { | ||
errorCount++ | ||
} | ||
} | ||
const successCount = totalCount - errorCount | ||
if (errorCount > 0) { | ||
triggerErrorToast( | ||
params.toastError({ totalCount, errorCount, successCount }) | ||
) | ||
} else { | ||
triggerSuccessToast( | ||
params.toastSuccess({ totalCount, errorCount, successCount }) | ||
) | ||
} | ||
await params.after?.() | ||
} |
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