Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/components/PokemonModal/PokemonModal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@
}
}

&__download {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: $spacing-sm;
padding: $spacing-md;
border: 2px solid var(--pokedex-red);
border-radius: $border-radius-md;
background: var(--pokedex-red);
color: #fff;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all $transition-normal;

&:hover:not(:disabled) {
background: var(--pokedex-red-dark);
border-color: var(--pokedex-red-dark);
}

&:disabled {
opacity: 0.7;
cursor: not-allowed;
}
}

&__download-icon {
font-size: 1.1rem;
}

&__controls {
display: flex;
flex-direction: column;
Expand Down
36 changes: 35 additions & 1 deletion src/components/PokemonModal/PokemonModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, useCallback } from 'react';
import '@google/model-viewer';
import type { PokemonWithForm } from '../../types/pokemon';
import type { ModelViewerElement } from '../../types/model-viewer';
Expand All @@ -14,6 +14,7 @@ export function PokemonModal({ pokemon, onClose }: PokemonModalProps) {
const [animations, setAnimations] = useState<string[]>([]);
const [selectedAnimation, setSelectedAnimation] = useState('');
const [modelError, setModelError] = useState(false);
const [downloading, setDownloading] = useState(false);

// Reset state when pokemon changes
useEffect(() => {
Expand Down Expand Up @@ -68,6 +69,28 @@ export function PokemonModal({ pokemon, onClose }: PokemonModalProps) {
};
}, [pokemon]);

const handleDownload = useCallback(async () => {
if (!pokemon || downloading) return;
setDownloading(true);
try {
const response = await fetch(pokemon.model);
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const formSuffix = pokemon.formName !== 'Regular' ? `_${pokemon.formName}` : '';
a.href = url;
a.download = `${pokemon.name}${formSuffix}.glb`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch {
// silently fail - model may not be downloadable
} finally {
setDownloading(false);
}
}, [pokemon, downloading]);

const handleClose = () => {
const modelViewer = modelViewerRef.current;
if (modelViewer) {
Expand Down Expand Up @@ -118,6 +141,17 @@ export function PokemonModal({ pokemon, onClose }: PokemonModalProps) {

{!modelError && (
<div className="pokemon-modal__controls">
<button
className="pokemon-modal__download"
onClick={handleDownload}
disabled={downloading}
>
<span className="pokemon-modal__download-icon">
{downloading ? '⏳' : '⬇'}
</span>
{downloading ? 'Downloading...' : 'Download Model'}
</button>

{animations.length > 0 ? (
<div className="pokemon-modal__control-group">
<label className="pokemon-modal__label">
Expand Down