All files / web/src/components/practice SessionPhotoGallery.tsx

0% Statements 0/524
0% Branches 0/1
0% Functions 0/1
0% Lines 0/524

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client'

import { useCallback, useEffect, useState } from 'react'
import { css } from '../../../styled-system/css'
import { PhotoUploadZone } from './PhotoUploadZone'

interface Attachment {
  id: string
  filename: string
  mimeType: string
  fileSize: number
  uploadedAt: string
  url: string
}

interface SessionPhotoGalleryProps {
  /** Player ID */
  playerId: string
  /** Session ID to fetch photos for */
  sessionId: string
  /** Whether the gallery is open */
  isOpen: boolean
  /** Close callback */
  onClose: () => void
  /** Start with upload section open */
  initialShowUpload?: boolean
  /** Callback when photos are successfully uploaded */
  onPhotosUploaded?: () => void
}

/**
 * Photo gallery modal for viewing and adding session attachments.
 * Fetches and displays photos for a specific session.
 * Allows adding new photos to the session.
 */
export function SessionPhotoGallery({
  playerId,
  sessionId,
  isOpen,
  onClose,
  initialShowUpload = false,
  onPhotosUploaded,
}: SessionPhotoGalleryProps) {
  const [attachments, setAttachments] = useState<Attachment[]>([])
  const [isLoading, setIsLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)
  const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
  const [showUpload, setShowUpload] = useState(initialShowUpload)
  const [pendingPhotos, setPendingPhotos] = useState<File[]>([])
  const [isUploading, setIsUploading] = useState(false)
  const [uploadError, setUploadError] = useState<string | null>(null)

  // Fetch attachments
  const fetchAttachments = useCallback(() => {
    setIsLoading(true)
    setError(null)

    fetch(`/api/curriculum/${playerId}/sessions/${sessionId}/attachments`)
      .then((res) => {
        if (!res.ok) throw new Error('Failed to fetch photos')
        return res.json()
      })
      .then((data) => {
        setAttachments(data.attachments || [])
        setIsLoading(false)
      })
      .catch((err) => {
        setError(err.message)
        setIsLoading(false)
      })
  }, [playerId, sessionId])

  // Fetch attachments and reset state when modal opens
  useEffect(() => {
    if (!isOpen) return
    setShowUpload(initialShowUpload)
    fetchAttachments()
  }, [isOpen, playerId, sessionId, initialShowUpload, fetchAttachments])

  const handleClose = useCallback(() => {
    setSelectedIndex(null)
    setShowUpload(false)
    setPendingPhotos([])
    onClose()
  }, [onClose])

  // Handle uploading new photos
  const handleUploadPhotos = useCallback(async () => {
    if (pendingPhotos.length === 0) return

    setIsUploading(true)
    setUploadError(null)

    try {
      const formData = new FormData()
      for (const photo of pendingPhotos) {
        formData.append('photos', photo)
      }

      const response = await fetch(
        `/api/curriculum/${playerId}/sessions/${sessionId}/attachments`,
        {
          method: 'POST',
          body: formData,
        }
      )

      if (!response.ok) {
        const data = await response.json()
        throw new Error(data.error || 'Failed to upload photos')
      }

      // Clear pending photos and refresh attachments
      setPendingPhotos([])
      setShowUpload(false)
      fetchAttachments()
      onPhotosUploaded?.()
    } catch (err) {
      setUploadError(err instanceof Error ? err.message : 'Failed to upload photos')
    } finally {
      setIsUploading(false)
    }
  }, [pendingPhotos, playerId, sessionId, fetchAttachments, onPhotosUploaded])

  const handlePrev = useCallback(() => {
    if (selectedIndex !== null && selectedIndex > 0) {
      setSelectedIndex(selectedIndex - 1)
    }
  }, [selectedIndex])

  const handleNext = useCallback(() => {
    if (selectedIndex !== null && selectedIndex < attachments.length - 1) {
      setSelectedIndex(selectedIndex + 1)
    }
  }, [selectedIndex, attachments.length])

  // Handle keyboard navigation
  useEffect(() => {
    if (!isOpen || selectedIndex === null) return

    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'ArrowLeft') handlePrev()
      if (e.key === 'ArrowRight') handleNext()
      if (e.key === 'Escape') setSelectedIndex(null)
    }

    window.addEventListener('keydown', handleKeyDown)
    return () => window.removeEventListener('keydown', handleKeyDown)
  }, [isOpen, selectedIndex, handlePrev, handleNext])

  if (!isOpen) return null

  return (
    <div
      data-component="session-photo-gallery"
      className={css({
        position: 'fixed',
        inset: 0,
        bg: 'rgba(0, 0, 0, 0.8)',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        zIndex: 10000,
        p: 4,
      })}
      onClick={handleClose}
    >
      <div
        className={css({
          bg: 'white',
          borderRadius: 'xl',
          maxW: '800px',
          w: '100%',
          maxH: '90vh',
          overflowY: 'auto',
          boxShadow: 'xl',
        })}
        onClick={(e) => e.stopPropagation()}
      >
        {/* Header */}
        <div
          className={css({
            p: 5,
            borderBottom: '1px solid',
            borderColor: 'gray.200',
            display: 'flex',
            justifyContent: 'space-between',
            alignItems: 'center',
          })}
        >
          <h2
            className={css({
              fontSize: 'xl',
              fontWeight: 'bold',
              color: 'gray.800',
            })}
          >
            Practice Photos
            {attachments.length > 0 && (
              <span
                className={css({
                  fontWeight: 'normal',
                  color: 'gray.500',
                  ml: 2,
                })}
              >
                ({attachments.length})
              </span>
            )}
          </h2>
          <div className={css({ display: 'flex', alignItems: 'center', gap: 2 })}>
            {!showUpload && (
              <button
                type="button"
                onClick={() => setShowUpload(true)}
                className={css({
                  px: 3,
                  py: 1.5,
                  bg: 'blue.500',
                  color: 'white',
                  borderRadius: 'md',
                  fontSize: 'sm',
                  fontWeight: 'medium',
                  cursor: 'pointer',
                  _hover: { bg: 'blue.600' },
                })}
              >
                + Add Photos
              </button>
            )}
            <button
              type="button"
              onClick={handleClose}
              className={css({
                fontSize: '2xl',
                color: 'gray.400',
                cursor: 'pointer',
                _hover: { color: 'gray.600' },
              })}
            >
              ×
            </button>
          </div>
        </div>

        {/* Content */}
        <div className={css({ p: 5 })}>
          {/* Upload Section */}
          {showUpload && (
            <div
              className={css({
                mb: 5,
                p: 4,
                bg: 'blue.50',
                borderRadius: 'lg',
                border: '1px solid',
                borderColor: 'blue.200',
              })}
            >
              <h3
                className={css({
                  fontSize: 'md',
                  fontWeight: 'semibold',
                  color: 'gray.800',
                  mb: 3,
                })}
              >
                Add Photos
              </h3>
              <PhotoUploadZone
                photos={pendingPhotos}
                onPhotosChange={setPendingPhotos}
                disabled={isUploading}
              />
              {uploadError && (
                <div
                  className={css({
                    mt: 3,
                    p: 2,
                    bg: 'red.50',
                    border: '1px solid',
                    borderColor: 'red.200',
                    borderRadius: 'md',
                    color: 'red.700',
                    fontSize: 'sm',
                  })}
                >
                  {uploadError}
                </div>
              )}
              <div className={css({ display: 'flex', gap: 2, mt: 3 })}>
                <button
                  type="button"
                  onClick={() => {
                    setShowUpload(false)
                    setPendingPhotos([])
                    setUploadError(null)
                  }}
                  disabled={isUploading}
                  className={css({
                    flex: 1,
                    py: 2,
                    border: '1px solid',
                    borderColor: 'gray.300',
                    borderRadius: 'md',
                    color: 'gray.700',
                    fontWeight: 'medium',
                    cursor: 'pointer',
                    _hover: { bg: 'gray.50' },
                    _disabled: { opacity: 0.5, cursor: 'not-allowed' },
                  })}
                >
                  Cancel
                </button>
                <button
                  type="button"
                  onClick={handleUploadPhotos}
                  disabled={isUploading || pendingPhotos.length === 0}
                  className={css({
                    flex: 1,
                    py: 2,
                    bg: 'blue.500',
                    color: 'white',
                    borderRadius: 'md',
                    fontWeight: 'medium',
                    cursor: 'pointer',
                    _hover: { bg: 'blue.600' },
                    _disabled: { opacity: 0.5, cursor: 'not-allowed' },
                  })}
                >
                  {isUploading
                    ? 'Uploading...'
                    : `Upload ${pendingPhotos.length} Photo${pendingPhotos.length !== 1 ? 's' : ''}`}
                </button>
              </div>
            </div>
          )}

          {isLoading ? (
            <div
              className={css({
                textAlign: 'center',
                py: 8,
                color: 'gray.500',
              })}
            >
              Loading photos...
            </div>
          ) : error ? (
            <div
              className={css({
                textAlign: 'center',
                py: 8,
                color: 'red.500',
              })}
            >
              {error}
            </div>
          ) : attachments.length === 0 ? (
            <div
              className={css({
                textAlign: 'center',
                py: 8,
                color: 'gray.500',
              })}
            >
              No photos for this session
            </div>
          ) : (
            <div
              className={css({
                display: 'grid',
                gridTemplateColumns: 'repeat(auto-fill, minmax(150px, 1fr))',
                gap: 4,
              })}
            >
              {attachments.map((att, idx) => (
                <button
                  key={att.id}
                  type="button"
                  onClick={() => setSelectedIndex(idx)}
                  className={css({
                    aspectRatio: '1',
                    borderRadius: 'lg',
                    overflow: 'hidden',
                    bg: 'gray.100',
                    cursor: 'pointer',
                    border: '2px solid transparent',
                    transition: 'all 0.15s',
                    _hover: {
                      borderColor: 'blue.500',
                      transform: 'scale(1.02)',
                    },
                  })}
                >
                  {/* biome-ignore lint/a11y/useAltText: decorative thumbnail */}
                  {/* biome-ignore lint/performance/noImgElement: API-served images can't use Next Image */}
                  <img
                    src={att.url}
                    className={css({
                      width: '100%',
                      height: '100%',
                      objectFit: 'cover',
                    })}
                  />
                </button>
              ))}
            </div>
          )}
        </div>
      </div>

      {/* Lightbox overlay for selected image */}
      {selectedIndex !== null && attachments[selectedIndex] && (
        <div
          className={css({
            position: 'fixed',
            inset: 0,
            bg: 'rgba(0, 0, 0, 0.95)',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            zIndex: 10001,
          })}
          onClick={() => setSelectedIndex(null)}
        >
          {/* Close button */}
          <button
            type="button"
            onClick={() => setSelectedIndex(null)}
            className={css({
              position: 'absolute',
              top: 4,
              right: 4,
              fontSize: '3xl',
              color: 'white',
              cursor: 'pointer',
              zIndex: 10,
              _hover: { color: 'gray.300' },
            })}
          >
            ×
          </button>

          {/* Previous button */}
          {selectedIndex > 0 && (
            <button
              type="button"
              onClick={(e) => {
                e.stopPropagation()
                handlePrev()
              }}
              className={css({
                position: 'absolute',
                left: 4,
                fontSize: '4xl',
                color: 'white',
                cursor: 'pointer',
                zIndex: 10,
                _hover: { color: 'gray.300' },
              })}
            >

            </button>
          )}

          {/* Next button */}
          {selectedIndex < attachments.length - 1 && (
            <button
              type="button"
              onClick={(e) => {
                e.stopPropagation()
                handleNext()
              }}
              className={css({
                position: 'absolute',
                right: 4,
                fontSize: '4xl',
                color: 'white',
                cursor: 'pointer',
                zIndex: 10,
                _hover: { color: 'gray.300' },
              })}
            >

            </button>
          )}

          {/* Image */}
          {/* biome-ignore lint/a11y/useAltText: full-size image view */}
          {/* biome-ignore lint/performance/noImgElement: API-served images can't use Next Image */}
          <img
            src={attachments[selectedIndex].url}
            onClick={(e) => e.stopPropagation()}
            className={css({
              maxW: '90vw',
              maxH: '90vh',
              objectFit: 'contain',
              borderRadius: 'lg',
            })}
          />

          {/* Counter */}
          <div
            className={css({
              position: 'absolute',
              bottom: 4,
              left: '50%',
              transform: 'translateX(-50%)',
              color: 'white',
              fontSize: 'sm',
              bg: 'rgba(0, 0, 0, 0.5)',
              px: 3,
              py: 1,
              borderRadius: 'full',
            })}
          >
            {selectedIndex + 1} / {attachments.length}
          </div>
        </div>
      )}
    </div>
  )
}