Files
interactive-story/frontend/src/ArtPicker.jsx
T
parththakkar106andClaude Opus 5 57c24a07d8 Redesign the library UI and add scenario cover art
Rework the home page into a single landing surface and give the app a
consistent visual language, chosen as "illuminated tome" over two other
pitched directions because it builds on the existing Cinzel + gold identity
instead of replacing it.

Home is now Continue (up to 4 in-progress stories, each showing where you
left off) over a scenario shelf, each section with a "See all" link. The
full adventure list moves to /adventures.

Scenario cover art has three tiers, in precedence order: an uploaded picture
(downscaled client-side to 400px WebP before storing), an emoji, or gradient
art generated from a hash of the title so no card is ever an empty box.
Adventures inherit their scenario's art. Images live in the row rather than
on disk because Render's free tier has no persistent volume, and it keeps
export bundles self-contained; list responses carry a cacheable
/api/scenarios/{id}/image URL rather than the base64.

Also: ambient drifting motes behind the app, loading skeletons, staggered
card entrance, ornamental scene breaks and a drop cap in the story, a
"Weaving" thinking indicator, and a toast system replacing every alert().

Two fixes found along the way:

- Importing a scenario bundle with no "tags" key returned a 500. Column
  defaults are not applied until flush, so the attribute was still None
  when the width clamp sliced it.
- Anything meaning "the story's latest narration" was missing action type
  "start", which is the only text a freshly created adventure has, so new
  adventures looked empty. Collected as NARRATION_TYPES.

Migrations 30 and 31 add scenarios.image and scenarios.icon; both are
additive with a '' default and were verified against a database stamped at
29. vite.config.js now reads AIDND_API_PORT so the recurring port-8000
clash with another local app needs no file edit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014FGY1yvzSeKgTtRfeVtDmx
2026-07-26 13:17:46 +05:30

148 lines
5.3 KiB
React

import { useRef, useState } from 'react'
import { ScenarioArt } from './components'
// Cards render the plate at 46px (92px on a 2x screen), and the editor preview
// at 76px. 400px square gives headroom for both plus any future larger use,
// while keeping the stored data URI in the tens of kilobytes.
const MAX_EDGE = 400
const WEBP_QUALITY = 0.82
// The backend caps the column at 400_000 chars; stay clearly under it so a
// pathological image fails here with a clear message rather than as a 422.
const MAX_DATA_URI = 360_000
// A spread of moods rather than a themed set — most scenarios find something
// close enough here and skip hunting for a picture.
const SUGGESTED_ICONS = [
'⚔️', '🗡️', '🏹', '🛡️', '🔮', '🗝️', '📜', '🏰',
'🐉', '👑', '💀', '🕯️', '🌑', '🌲', '⛰️', '🌊',
'🚀', '🛰️', '🤖', '👁️', '🩸', '🃏',
]
/** Read a File and re-encode it small, as a WebP data URI.
*
* Downscaling in the browser means the upload never leaves the machine at full
* size and the row stays small — no server-side image library required.
*/
function downscaleToDataURI(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onerror = () => reject(new Error('Could not read that file'))
reader.onload = () => {
const img = new Image()
img.onerror = () => reject(new Error('That file is not an image the browser can read'))
img.onload = () => {
const scale = Math.min(1, MAX_EDGE / Math.max(img.width, img.height))
const canvas = document.createElement('canvas')
// Math.max(1, …) guards against a 0-dimension canvas, which throws.
canvas.width = Math.max(1, Math.round(img.width * scale))
canvas.height = Math.max(1, Math.round(img.height * scale))
const ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
let uri = canvas.toDataURL('image/webp', WEBP_QUALITY)
// Safari only gained WebP encoding recently; if it silently fell back
// to PNG, retry as JPEG so we don't store a huge lossless blob.
if (!uri.startsWith('data:image/webp')) {
uri = canvas.toDataURL('image/jpeg', WEBP_QUALITY)
}
if (uri.length > MAX_DATA_URI) {
reject(new Error('That image is too detailed to store — try a smaller or simpler one'))
return
}
resolve(uri)
}
img.src = reader.result
}
reader.readAsDataURL(file)
})
}
/** Cover-art picker: upload a picture, choose an emoji, or leave it generated.
*
* `image` and `icon` are the stored values; `onChange({ image, icon })` gets
* both on every change so the caller can persist them together.
*/
export default function ArtPicker({ title, image, icon, onChange, disabled }) {
const fileRef = useRef(null)
const [error, setError] = useState('')
const pick = async (event) => {
const file = event.target.files?.[0]
// Reset immediately so picking the same file twice still fires onChange.
event.target.value = ''
if (!file) return
setError('')
try {
const uri = await downscaleToDataURI(file)
// An uploaded picture wins over an emoji, so clear the icon to make the
// precedence visible rather than leaving a hidden value behind.
onChange({ image: uri, icon: '' })
} catch (err) {
setError(err.message)
}
}
const chooseIcon = (glyph) => {
setError('')
onChange({ image: '', icon: icon === glyph ? '' : glyph })
}
const clear = () => {
setError('')
onChange({ image: '', icon: '' })
}
return (
<label className="field">
<span className="label">Cover art</span>
<div className="art-picker">
<span className="art-preview">
{/* Same three-tier fallback the cards use, at preview size. */}
<ScenarioArt image={image} icon={icon} title={title} />
</span>
<div className="art-controls">
<div className="art-buttons">
<button type="button" disabled={disabled} onClick={() => fileRef.current?.click()}>
Upload image
</button>
{(image || icon) && (
<button type="button" disabled={disabled} onClick={clear}>Clear</button>
)}
</div>
<input
ref={fileRef}
type="file"
accept="image/png,image/jpeg,image/webp,image/gif,image/avif"
hidden
onChange={pick}
/>
<div className="icon-row">
{SUGGESTED_ICONS.map((glyph) => (
<button
key={glyph}
type="button"
disabled={disabled}
className={icon === glyph ? 'active' : ''}
title={`Use ${glyph}`}
onClick={() => chooseIcon(glyph)}
>
{glyph}
</button>
))}
</div>
{error ? (
<p className="art-hint" style={{ color: 'var(--danger)' }}>{error}</p>
) : (
<p className="art-hint">
Pictures are shrunk to {MAX_EDGE}px before saving. With neither a picture
nor an emoji, the card draws its own art from the title.
</p>
)}
</div>
</div>
</label>
)
}