Search + type chips in the Assets panel and picker library; Stock photos entry in the picker (feature-detected, hidden when stock_disabled/absent); bgPosition focal point for Hero and Background Section. Refs #55
143 lines
5.2 KiB
TypeScript
143 lines
5.2 KiB
TypeScript
import React, { useState, useCallback } from 'react';
|
|
import { useStockPhotos, StockPhoto, StockCredit } from '../hooks/useStockPhotos';
|
|
|
|
export interface PickedAsset {
|
|
url: string;
|
|
alt?: string;
|
|
credit?: StockCredit;
|
|
}
|
|
|
|
interface Props {
|
|
/** Called with the imported (site-local) asset once "Use photo" completes. */
|
|
onPick: (asset: PickedAsset) => void;
|
|
}
|
|
|
|
const httpsOnly = (u?: string) => (u && /^https:\/\//i.test(u) ? u : undefined);
|
|
|
|
export const creditLine = (c?: StockCredit) => (c?.name ? `Photo by ${c.name} on Pexels` : 'Photo from Pexels');
|
|
|
|
export const StockPhotoTab: React.FC<Props> = ({ onPick }) => {
|
|
const { availability, loading, error, search, importPhoto } = useStockPhotos();
|
|
const [query, setQuery] = useState('');
|
|
const [submitted, setSubmitted] = useState('');
|
|
const [photos, setPhotos] = useState<StockPhoto[]>([]);
|
|
const [nextPage, setNextPage] = useState<number | null>(null);
|
|
const [selected, setSelected] = useState<StockPhoto | null>(null);
|
|
const [importing, setImporting] = useState(false);
|
|
const [searched, setSearched] = useState(false);
|
|
|
|
const runSearch = useCallback(async () => {
|
|
const q = query.trim();
|
|
if (!q) return;
|
|
setSubmitted(q);
|
|
setSelected(null);
|
|
const res = await search(q, 1);
|
|
if (res) { setPhotos(res.results); setNextPage(res.nextPage); setSearched(true); }
|
|
else setPhotos([]);
|
|
}, [query, search]);
|
|
|
|
const loadMore = useCallback(async () => {
|
|
if (nextPage == null) return;
|
|
const res = await search(submitted, nextPage);
|
|
if (res) { setPhotos((p) => [...p, ...res.results]); setNextPage(res.nextPage); }
|
|
}, [nextPage, search, submitted]);
|
|
|
|
const usePhoto = useCallback(async () => {
|
|
if (!selected) return;
|
|
setImporting(true);
|
|
try {
|
|
const imported = await importPhoto(selected.id, 'large');
|
|
if (!imported) return;
|
|
const credit = imported.credit || selected.credit;
|
|
onPick({ url: imported.url, alt: selected.alt || creditLine(credit), credit });
|
|
} finally {
|
|
setImporting(false);
|
|
}
|
|
}, [selected, importPhoto, onPick]);
|
|
|
|
if (availability === 'disabled') {
|
|
return (
|
|
<div className="stock-tab" data-testid="stock-disabled">
|
|
<div className="stock-status">Stock photos are not enabled for this server.</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="stock-tab" data-testid="stock-tab">
|
|
<form
|
|
className="guided-input-row"
|
|
onSubmit={(e) => { e.preventDefault(); runSearch(); }}
|
|
>
|
|
<input
|
|
type="search"
|
|
className="guided-input"
|
|
value={query}
|
|
placeholder="Search free photos..."
|
|
aria-label="Search stock photos"
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
data-testid="stock-search-input"
|
|
style={{ fontSize: 11 }}
|
|
/>
|
|
<button type="submit" className="preset-btn apply-btn" disabled={loading || !query.trim()} data-testid="stock-search-btn">
|
|
Search
|
|
</button>
|
|
</form>
|
|
|
|
{error && <div className="stock-status error" role="alert" data-testid="stock-error">{error}</div>}
|
|
{loading && photos.length === 0 && <div className="stock-status">Searching...</div>}
|
|
{!loading && !error && searched && photos.length === 0 && (
|
|
<div className="stock-status" data-testid="stock-empty">No photos found for “{submitted}”.</div>
|
|
)}
|
|
|
|
{photos.length > 0 && (
|
|
<div className="stock-grid" data-testid="stock-grid">
|
|
{photos.map((p) => (
|
|
<button
|
|
key={p.id}
|
|
type="button"
|
|
className={`stock-thumb${selected?.id === p.id ? ' selected' : ''}`}
|
|
onClick={() => setSelected(p)}
|
|
aria-label={p.alt || `Photo ${p.id}`}
|
|
aria-pressed={selected?.id === p.id}
|
|
data-testid={`stock-thumb-${p.id}`}
|
|
style={p.width && p.height ? { aspectRatio: `${p.width} / ${p.height}` } : undefined}
|
|
>
|
|
<img src={p.thumb} alt={p.alt || ''} loading="lazy" />
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
{nextPage != null && photos.length > 0 && (
|
|
<button type="button" className="preset-btn" onClick={loadMore} disabled={loading} data-testid="stock-more">
|
|
{loading ? 'Loading...' : 'Load more'}
|
|
</button>
|
|
)}
|
|
|
|
{selected && (
|
|
<div data-testid="stock-selected">
|
|
<div className="stock-credit" data-testid="stock-credit">
|
|
{httpsOnly(selected.credit?.url) ? (
|
|
<>Photo by <a href={httpsOnly(selected.credit.url)} target="_blank" rel="noopener noreferrer">{selected.credit.name}</a> on Pexels</>
|
|
) : creditLine(selected.credit)}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="preset-btn apply-btn"
|
|
onClick={usePhoto}
|
|
disabled={importing}
|
|
data-testid="stock-use"
|
|
style={{ marginTop: 4, width: '100%' }}
|
|
>
|
|
{importing ? 'Importing...' : 'Use photo'}
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
<div className="stock-footer">
|
|
<a href="https://www.pexels.com" target="_blank" rel="noopener noreferrer">Photos from Pexels</a>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|