import { useRouter } from 'next/router' import { useCallback, useEffect, useState } from 'react' import { Button } from '../../components/elements/Button' import { BorderedFormInput, FormLabel, } from '../../components/elements/FormElements' import { VStack } from '../../components/elements/LayoutPrimitives' import { StyledText } from '../../components/elements/StyledText' import { ConfirmationModal } from '../../components/patterns/ConfirmationModal' import { ProfileLayout } from '../../components/templates/ProfileLayout' import { theme } from '../../components/tokens/stitches.config' import { DEFAULT_HOME_PATH } from '../../lib/navigations' import { BulkAction, useBulkActions, useGetLibraryItems, } from '../../lib/networking/library_items/useLibraryItems' import { applyStoredTheme } from '../../lib/themeUpdater' import { showErrorToast, showSuccessToast } from '../../lib/toastHelpers' type RunningState = 'none' | 'confirming' | 'running' | 'completed' export default function BulkPerformer(): JSX.Element { const router = useRouter() applyStoredTheme() const [action, setAction] = useState() const [query, setQuery] = useState('in:all') const [expectedCount, setExpectedCount] = useState() const [errorMessage, setErrorMessage] = useState() const [runningState, setRunningState] = useState('none') const bulkAction = useBulkActions() const { data: itemsPages, isLoading } = useGetLibraryItems(undefined, { searchQuery: query, limit: 1, sortDescending: false, includeCount: true, }) useEffect(() => { setExpectedCount(itemsPages?.pages.find(() => true)?.pageInfo.totalCount) }, [itemsPages]) const performAction = useCallback(() => { ;(async () => { console.log('performing action: ', action) if (isLoading) { showErrorToast('Query still being validated.') return } if (!action) { showErrorToast('Unable to run action, no action set.') return } if (!expectedCount) { showErrorToast('No items matching this query or query still running.') return } if (!action) { showErrorToast('No action selected') return } try { const success = await bulkAction.mutateAsync({ action, query, expectedCount, }) if (!success) { throw 'Success not returned' } showSuccessToast('Bulk action is being performed.') setRunningState('completed') } catch (err) { showErrorToast('Error performing bulk action.') } })() }, [action, query, expectedCount]) return ( Perform a Bulk Action Use this tool to perform a bulk operation on all the items in your library.

Note: This operation can not be undone. {runningState == 'completed' ? ( Your bulk action has started. Please note that it can take some time for these actions to complete. During this time, we recommend not modifying your library as new items could be updated by the action. ) : ( <> Search Query setQuery(e.target.value)} required /> Matches {expectedCount} items. Action )} {runningState == 'confirming' && ( setRunningState('none')} /> )} {runningState == 'completed' && ( )} {errorMessage && ( {errorMessage} )}
) }