Spaces:
Paused
Paused
File size: 6,149 Bytes
1c72248 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
'use client';
import { useState, useEffect, useMemo } from 'react';
import { createGlobalState } from 'react-global-hooks';
import { Dialog, DialogBackdrop, DialogPanel } from '@headlessui/react';
export interface SampleImageModalState {
imgPath: string;
numSamples: number;
sampleImages: string[];
}
export const sampleImageModalState = createGlobalState<SampleImageModalState | null>(null);
export const openSampleImage = (sampleImageProps: SampleImageModalState) => {
sampleImageModalState.set(sampleImageProps);
};
export default function SampleImageModal() {
const [imageModal, setImageModal] = sampleImageModalState.use();
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
if (imageModal) {
setIsOpen(true);
}
}, [imageModal]);
useEffect(() => {
if (!isOpen) {
// use timeout to allow the dialog to close before resetting the state
setTimeout(() => {
setImageModal(null);
}, 500);
}
}, [isOpen]);
const onCancel = () => {
setIsOpen(false);
};
const imgInfo = useMemo(() => {
const ii = {
filename: '',
step: 0,
promptIdx: 0,
};
if (imageModal?.imgPath) {
const filename = imageModal.imgPath.split('/').pop();
if (!filename) return ii;
// filename is <timestep>__<zero_pad_step>_<prompt_idx>.<ext>
ii.filename = filename as string;
const parts = filename
.split('.')[0]
.split('_')
.filter(p => p !== '');
if (parts.length === 3) {
ii.step = parseInt(parts[1]);
ii.promptIdx = parseInt(parts[2]);
}
}
return ii;
}, [imageModal]);
const handleArrowUp = () => {
if (!imageModal) return;
console.log('Arrow Up pressed');
// Change image to same sample but up one step
const currentIdx = imageModal.sampleImages.findIndex(img => img === imageModal.imgPath);
if (currentIdx === -1) return;
const nextIdx = currentIdx - imageModal.numSamples;
if (nextIdx < 0) return;
openSampleImage({
imgPath: imageModal.sampleImages[nextIdx],
numSamples: imageModal.numSamples,
sampleImages: imageModal.sampleImages,
});
};
const handleArrowDown = () => {
if (!imageModal) return;
console.log('Arrow Down pressed');
// Change image to same sample but down one step
const currentIdx = imageModal.sampleImages.findIndex(img => img === imageModal.imgPath);
if (currentIdx === -1) return;
const nextIdx = currentIdx + imageModal.numSamples;
if (nextIdx >= imageModal.sampleImages.length) return;
openSampleImage({
imgPath: imageModal.sampleImages[nextIdx],
numSamples: imageModal.numSamples,
sampleImages: imageModal.sampleImages,
});
};
const handleArrowLeft = () => {
if (!imageModal) return;
if (imgInfo.promptIdx === 0) return;
console.log('Arrow Left pressed');
// go to previous sample
const currentIdx = imageModal.sampleImages.findIndex(img => img === imageModal.imgPath);
if (currentIdx === -1) return;
const minIdx = currentIdx - imgInfo.promptIdx;
const nextIdx = currentIdx - 1;
if (nextIdx < minIdx) return;
openSampleImage({
imgPath: imageModal.sampleImages[nextIdx],
numSamples: imageModal.numSamples,
sampleImages: imageModal.sampleImages,
});
};
const handleArrowRight = () => {
if (!imageModal) return;
console.log('Arrow Right pressed');
// go to next sample
const currentIdx = imageModal.sampleImages.findIndex(img => img === imageModal.imgPath);
if (currentIdx === -1) return;
const stepMinIdx = currentIdx - imgInfo.promptIdx;
const maxIdx = stepMinIdx + imageModal.numSamples - 1;
const nextIdx = currentIdx + 1;
if (nextIdx > maxIdx) return;
if (nextIdx >= imageModal.sampleImages.length) return;
openSampleImage({
imgPath: imageModal.sampleImages[nextIdx],
numSamples: imageModal.numSamples,
sampleImages: imageModal.sampleImages,
});
};
// Handle keyboard events
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (!isOpen) return;
switch (event.key) {
case 'Escape':
onCancel();
break;
case 'ArrowUp':
handleArrowUp();
break;
case 'ArrowDown':
handleArrowDown();
break;
case 'ArrowLeft':
handleArrowLeft();
break;
case 'ArrowRight':
handleArrowRight();
break;
default:
break;
}
};
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [isOpen, imageModal, imgInfo]);
return (
<Dialog open={isOpen} onClose={onCancel} className="relative z-10">
<DialogBackdrop
transition
className="fixed inset-0 bg-gray-900/75 transition-opacity data-closed:opacity-0 data-enter:duration-300 data-enter:ease-out data-leave:duration-200 data-leave:ease-in"
/>
<div className="fixed inset-0 z-10 w-screen overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center">
<DialogPanel
transition
className="relative transform overflow-hidden rounded-lg bg-gray-800 text-left shadow-xl transition-all data-closed:translate-y-4 data-closed:opacity-0 data-enter:duration-300 data-enter:ease-out data-leave:duration-200 data-leave:ease-in max-w-[95%] max-h-[95vh] data-closed:sm:translate-y-0 data-closed:sm:scale-95"
>
<div className="flex justify-center items-center">
{imageModal?.imgPath && (
<img
src={`/api/img/${encodeURIComponent(imageModal.imgPath)}`}
alt="Sample Image"
className="max-w-full max-h-[calc(95vh-2rem)] object-contain"
/>
)}
</div>
<div className="bg-gray-950 text-center text-sm p-2">step: {imgInfo.step}</div>
</DialogPanel>
</div>
</div>
</Dialog>
);
}
|