-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(renterd): contract multiselect and batch delete
- Loading branch information
1 parent
afc1830
commit 112cc4f
Showing
13 changed files
with
216 additions
and
36 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 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
The contracts table now supports multi-select. |
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 batch 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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
import { Button, 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 { handleBatchOperation } from '../../../lib/handleBatchOperation' | ||
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
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,42 @@ | ||
import { | ||
ToastParams, | ||
triggerErrorToast, | ||
triggerSuccessToast, | ||
} from '@siafoundation/design-system' | ||
|
||
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