diff --git a/packages/web/pages/tools/bulk.tsx b/packages/web/pages/tools/bulk.tsx new file mode 100644 index 000000000..78dbc9cd0 --- /dev/null +++ b/packages/web/pages/tools/bulk.tsx @@ -0,0 +1,221 @@ +import { useCallback, useEffect, useState } from 'react' +import { applyStoredTheme } from '../../lib/themeUpdater' + +import { + Box, + SpanBox, + VStack, +} from '../../components/elements/LayoutPrimitives' + +import { StyledText } from '../../components/elements/StyledText' +import { ProfileLayout } from '../../components/templates/ProfileLayout' +import { + BulkAction, + bulkActionMutation, +} from '../../lib/networking/mutations/bulkActionMutation' +import { Button } from '../../components/elements/Button' +import { theme } from '../../components/tokens/stitches.config' +import { ConfirmationModal } from '../../components/patterns/ConfirmationModal' +import * as Select from '@radix-ui/react-select' + +import { showErrorToast, showSuccessToast } from '../../lib/toastHelpers' +import { useRouter } from 'next/router' +import { useGetLibraryItemsQuery } from '../../lib/networking/queries/useGetLibraryItemsQuery' +import { + BorderedFormInput, + FormInput, + FormLabel, +} from '../../components/elements/FormElements' + +type RunningState = 'none' | 'confirming' | 'running' | 'completed' + +export default function BulkPerformer(): JSX.Element { + const router = useRouter() + + applyStoredTheme(false) + + const [action, setAction] = useState() + const [query, setQuery] = useState('in:all') + const [expectedCount, setExpectedCount] = useState() + const [errorMessage, setErrorMessage] = useState() + const [runningState, setRunningState] = useState('none') + + const { itemsPages, isValidating } = useGetLibraryItemsQuery({ + searchQuery: query, + limit: 1, + sortDescending: false, + }) + + useEffect(() => { + console.log('itemsPages: ', itemsPages) + setExpectedCount(itemsPages?.find(() => true)?.search.pageInfo.totalCount) + }, [itemsPages]) + + const performAction = useCallback(() => { + ;(async () => { + console.log('performing action: ', action) + if (!action) { + showErrorToast('Unable to run action, no action set.') + return + } + try { + // action: BulkAction, + // query: string, + // expectedCount: number, + // labelIds?: string[] + const success = await bulkActionMutation(action, query, size) + if (!success) { + throw 'Success not returned' + } + showSuccessToast('Bulk action is being performed.') + setRunningState('completed') + } catch (err) { + showErrorToast('Error performing bulk action.') + } + })() + }, [action]) + + 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} + )} + +
+
+ ) +}