'use client'; import LoaderButton from '@/components/primitives/LoaderButton'; import AppGrid from '@/components/AppGrid'; import { clsx } from 'clsx/lite'; import { IoCloseSharp } from 'react-icons/io5'; import { useEffect, useRef } from 'react'; import { Tags } from '@/tag'; import FieldsetTag from '@/tag/FieldsetTag'; import { batchPhotoAction } from '@/photo/actions'; import { toastSuccess } from '@/toast'; import DeletePhotosButton from '@/admin/DeletePhotosButton'; import { photoQuantityText } from '@/photo'; import { FaArrowDown, FaCheck } from 'react-icons/fa6'; import ResponsiveText from '@/components/primitives/ResponsiveText'; import IconFavs from '@/components/icons/IconFavs'; import IconTag from '@/components/icons/IconTag'; import { useAppText } from '@/i18n/state/client'; import { useSelectPhotosState } from './SelectPhotosState'; import { Albums } from '@/album'; import FieldsetAlbum from '@/album/FieldsetAlbum'; import IconAlbum from '@/components/icons/IconAlbum'; import FieldsetWithStatus from '@/components/FieldsetWithStatus'; import { convertStringToArray } from '@/utility/string'; export default function AdminBatchEditPanelClient({ uniqueAlbums, uniqueTags, }: { uniqueAlbums: Albums uniqueTags: Tags }) { const refNote = useRef(null); const { canCurrentPageSelectPhotos, shouldShowSelectAll, isSelectingPhotos, stopSelectingPhotos, isSelectingAllPhotos, toggleIsSelectingAllPhotos, selectedPhotoIds, selectAllPhotoOptions, selectAllCount, isPerformingSelectEdit, setIsPerformingSelectEdit, albumTitles, setAlbumTitles, tags, setTags, tagErrorMessage, setTagErrorMessage, } = useSelectPhotosState(); const appText = useAppText(); const isInAlbumMode = albumTitles !== undefined; const isInTagMode = tags !== undefined; const batchPhotoActionArguments = ( isSelectingAllPhotos && selectAllPhotoOptions ) ? { photoOptions: selectAllPhotoOptions } : { photoIds: selectedPhotoIds }; const photosText = photoQuantityText( (isSelectingAllPhotos && selectAllCount !== undefined ? selectAllCount : selectedPhotoIds?.length) ?? 0, appText, false, false, ); const isFormDisabled = isPerformingSelectEdit || isSelectingAllPhotos ? !Boolean(selectAllCount) : selectedPhotoIds?.length === 0; const renderPhotoSelectionStatus = isSelectingAllPhotos ? selectAllCount === undefined ? 'Selecting ...' : {`${selectAllCount} photos selected`} : selectedPhotoIds?.length === 0 ? <> Select photos below : {photosText} selected ; const renderActions = isInTagMode || isInAlbumMode ? <> } onClick={() => { setAlbumTitles?.(undefined); setTags?.(undefined); setTagErrorMessage?.(''); }} disabled={isPerformingSelectEdit} /> } confirmText={isInTagMode // eslint-disable-next-line max-len ? `Are you sure you want to apply tags to ${photosText}? This action cannot be undone.` // eslint-disable-next-line max-len : `Are you sure you want to add ${photosText} to these albums? This action cannot be undone.`} onClick={() => { setIsPerformingSelectEdit?.(true); if (isInTagMode) { const tagsArray = convertStringToArray(tags, false); const tagsFormatted = tagsArray .map(tag => `"${tag}"`) .join(', '); batchPhotoAction({ ...batchPhotoActionArguments, tags: tagsArray, }) .then(() => { toastSuccess(`${photosText} tagged ${tagsFormatted}`); stopSelectingPhotos?.(); }) .finally(() => setIsPerformingSelectEdit?.(false)); } else if (isInAlbumMode) { const albumTitlesArray = convertStringToArray(albumTitles, false); const albumTitlesFormatted = albumTitlesArray .map(title => `"${title}"`) .join(', '); batchPhotoAction({ ...batchPhotoActionArguments, albumTitles: albumTitlesArray, }) .then(() => { toastSuccess( `${photosText} added to ${albumTitlesFormatted}`, ); stopSelectingPhotos?.(); }) .finally(() => setIsPerformingSelectEdit?.(false)); } }} disabled={ ( (!tags || Boolean(tagErrorMessage)) && !albumTitles ) || isFormDisabled } primary > Apply : <> setIsPerformingSelectEdit?.(true)} onDelete={stopSelectingPhotos} onFinish={() => setIsPerformingSelectEdit?.(false)} /> } disabled={isFormDisabled} confirmText={`Are you sure you want to favorite ${photosText}?`} onClick={() => { setIsPerformingSelectEdit?.(true); batchPhotoAction({ ...batchPhotoActionArguments, action: 'favorite', }) .then(() => { toastSuccess(`${photosText} favorited`); stopSelectingPhotos?.(); }) .finally(() => setIsPerformingSelectEdit?.(false)); }} /> setAlbumTitles?.('')} disabled={isFormDisabled} icon={} > Album setTags?.('')} disabled={isFormDisabled} icon={} > Tag } onClick={stopSelectingPhotos} /> ; const shouldShowPanel = isSelectingPhotos && canCurrentPageSelectPhotos; useEffect(() => { // Steal focus from Admin Menu to hide tooltip if (isSelectingPhotos) { refNote.current?.focus(); } }, [isSelectingPhotos]); return shouldShowPanel ?
{isInAlbumMode ? : isInTagMode ? setTags?.(tags)} onError={setTagErrorMessage} readOnly={isPerformingSelectEdit} openOnLoad hideLabel /> :
{renderPhotoSelectionStatus}
} {renderActions}
{shouldShowSelectAll && } {tagErrorMessage &&
{tagErrorMessage}
} } /> : null; }