/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useState, useMemo } from 'react'
import { type Table } from '@tanstack/react-table'
import { Trash2 } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip'
import { ConfirmDialog } from '@/components/confirm-dialog'
import { CopyButton } from '@/components/copy-button'
import { DataTableBulkActions as BulkActionsToolbar } from '@/components/data-table'
import { deleteInvalidRedemptions } from '../api'
import { type Redemption } from '../types'
import { useRedemptions } from './redemptions-provider'
type DataTableBulkActionsProps = {
table: Table
}
export function DataTableBulkActions({
table,
}: DataTableBulkActionsProps) {
const { t } = useTranslation()
const { triggerRefresh } = useRedemptions()
const [showDeleteInvalidConfirm, setShowDeleteInvalidConfirm] =
useState(false)
const [isDeleting, setIsDeleting] = useState(false)
const selectedRows = table.getFilteredSelectedRowModel().rows
const contentToCopy = useMemo(() => {
const selectedCodes = selectedRows.map((row) => {
const redemption = row.original as Redemption
return `${redemption.name}\t${redemption.key}`
})
return selectedCodes.join('\n')
}, [selectedRows])
const handleDeleteInvalid = async () => {
setIsDeleting(true)
try {
const result = await deleteInvalidRedemptions()
if (result.success) {
const count = result.data || 0
toast.success(
t('Successfully deleted {{count}} invalid redemption codes', {
count,
})
)
table.resetRowSelection()
triggerRefresh()
setShowDeleteInvalidConfirm(false)
}
} finally {
setIsDeleting(false)
}
}
return (
<>
setShowDeleteInvalidConfirm(true)}
className='size-8'
aria-label={t('Delete invalid redemption codes')}
title={t('Delete invalid redemption codes')}
/>
}
>
{t('Delete invalid codes')}