import React, { useState } from "react"; import _ from "lodash"; import { Table } from "react-bootstrap"; import { Link, useHistory } from "react-router-dom"; import Mousetrap from "mousetrap"; import { FindGalleriesQueryResult, GallerySlimDataFragment, } from "src/core/generated-graphql"; import { useGalleriesList } from "src/hooks"; import { TextUtils } from "src/utils"; import { showWhenSelected, PersistanceLevel } from "src/hooks/ListHook"; import { ListFilterModel } from "src/models/list-filter/filter"; import { DisplayMode } from "src/models/list-filter/types"; import { queryFindGalleries } from "src/core/StashService"; import { GalleryCard } from "./GalleryCard"; import GalleryWallCard from "./GalleryWallCard"; import { EditGalleriesDialog } from "./EditGalleriesDialog"; import { DeleteGalleriesDialog } from "./DeleteGalleriesDialog"; import { ExportDialog } from "../Shared/ExportDialog"; interface IGalleryList { filterHook?: (filter: ListFilterModel) => ListFilterModel; persistState?: PersistanceLevel; } export const GalleryList: React.FC = ({ filterHook, persistState, }) => { const history = useHistory(); const [isExportDialogOpen, setIsExportDialogOpen] = useState(false); const [isExportAll, setIsExportAll] = useState(false); const otherOperations = [ { text: "View Random", onClick: viewRandom, }, { text: "Export...", onClick: onExport, isDisplayed: showWhenSelected, }, { text: "Export all...", onClick: onExportAll, }, ]; const addKeybinds = ( result: FindGalleriesQueryResult, filter: ListFilterModel ) => { Mousetrap.bind("p r", () => { viewRandom(result, filter); }); return () => { Mousetrap.unbind("p r"); }; }; const listData = useGalleriesList({ zoomable: true, selectable: true, otherOperations, renderContent, renderEditDialog: renderEditGalleriesDialog, renderDeleteDialog: renderDeleteGalleriesDialog, filterHook, addKeybinds, persistState, }); async function viewRandom( result: FindGalleriesQueryResult, filter: ListFilterModel ) { // query for a random image if (result.data && result.data.findGalleries) { const { count } = result.data.findGalleries; const index = Math.floor(Math.random() * count); const filterCopy = _.cloneDeep(filter); filterCopy.itemsPerPage = 1; filterCopy.currentPage = index + 1; const singleResult = await queryFindGalleries(filterCopy); if ( singleResult && singleResult.data && singleResult.data.findGalleries && singleResult.data.findGalleries.galleries.length === 1 ) { const { id } = singleResult!.data!.findGalleries!.galleries[0]; // navigate to the image player page history.push(`/galleries/${id}`); } } } async function onExport() { setIsExportAll(false); setIsExportDialogOpen(true); } async function onExportAll() { setIsExportAll(true); setIsExportDialogOpen(true); } function maybeRenderGalleryExportDialog(selectedIds: Set) { if (isExportDialogOpen) { return ( <> { setIsExportDialogOpen(false); }} /> ); } } function renderEditGalleriesDialog( selectedImages: GallerySlimDataFragment[], onClose: (applied: boolean) => void ) { return ( <> ); } function renderDeleteGalleriesDialog( selectedImages: GallerySlimDataFragment[], onClose: (confirmed: boolean) => void ) { return ( <> ); } function renderGalleries( result: FindGalleriesQueryResult, filter: ListFilterModel, selectedIds: Set, zoomIndex: number ) { if (!result.data || !result.data.findGalleries) { return; } if (filter.displayMode === DisplayMode.Grid) { return (
{result.data.findGalleries.galleries.map((gallery) => ( 0} selected={selectedIds.has(gallery.id)} onSelectedChanged={(selected: boolean, shiftKey: boolean) => listData.onSelectChange(gallery.id, selected, shiftKey) } /> ))}
); } if (filter.displayMode === DisplayMode.List) { return ( {result.data.findGalleries.galleries.map((gallery) => ( ))}
Preview Title
{gallery.cover ? ( {gallery.title ) : undefined} {gallery.title ?? TextUtils.fileNameFromPath(gallery.path ?? "")}{" "} ({gallery.image_count}{" "} {gallery.image_count === 1 ? "image" : "images"})
); } if (filter.displayMode === DisplayMode.Wall) { return (
{result.data.findGalleries.galleries.map((gallery) => ( ))}
); } } function renderContent( result: FindGalleriesQueryResult, filter: ListFilterModel, selectedIds: Set, zoomIndex: number ) { return ( <> {maybeRenderGalleryExportDialog(selectedIds)} {renderGalleries(result, filter, selectedIds, zoomIndex)} ); } return listData.template; };