clipwright/dashboard/src/components/HookModal.jsx
juancarlos.cavero 2b2e287a9b feat(hook): replace semantics — a new hook swaps the burned one, plus remove
The auto-hook was burned into the canonical clip in place, so editing the
hook stacked a second overlay on top of the first. Hooks now follow the
exact derivation pattern captions use:

- main.py: auto_hook_clip writes hooked_<ts>_<clean>.mp4 next to the clean
  canonical instead of mutating it; captions burn on top of the hooked file
  (subtitled_<ts>_hooked_<ts>_<clean>). Watermark moves BEFORE the hook so
  the branding lives in the canonical and survives hook replacement.
- app.py: _strip_burned_hook walks hooked_/legacy hook_ prefixes back like
  _strip_burned_captions; /api/hook strips captions AND the old hook before
  burning, so a new hook always REPLACES the previous one. New remove flag
  strips the hook entirely (captions re-applied, no metering). Burned hook
  recorded/cleared in clip metadata (auto_hook key) and returned as
  burned_hook. _canonical_clip_file resolves hooked_*/hook_* derivations.
- cloud/videos.py: archive the hooked_ intermediate of a captioned hook so
  restored projects keep the walk-back chain editable.
- ResultCard: stripBurns knows hooked_<ts>_ (the Remotion preview now
  composes over the truly clean clip — no more stacked preview), burned
  hook tracked in state, remove handler wired to the modal.
- HookModal: warning now says replace, plus a 'remove hook from clip'
  action when one is burned.
2026-08-19 16:01:53 +02:00

277 lines
13 KiB
JavaScript

import React, { useState } from 'react';
import { Loader2 } from 'lucide-react';
import RemotionPreview from './RemotionPreview';
import Modal from './ui/Modal';
import SegmentedControl from './ui/SegmentedControl';
const ENTRANCE_OPTIONS = [
{ value: 'spring', label: 'Bounce' },
{ value: 'fade', label: 'Fade' },
{ value: 'slide-up', label: 'Slide Up' },
{ value: 'none', label: 'None' },
];
// Must mirror hooks.py HOOK_STYLES.
const HOOK_STYLES = [
{ value: 'classic', label: 'Classic', box: 'rgba(255,255,255,0.94)', text: '#000' },
{ value: 'dark', label: 'Dark', box: 'rgba(18,18,20,0.92)', text: '#fff' },
{ value: 'yellow', label: 'Yellow', box: 'rgba(255,214,0,0.96)', text: '#000' },
{ value: 'red', label: 'Red', box: 'rgba(220,38,38,0.96)', text: '#fff' },
{ value: 'outline', label: 'Outline', box: 'transparent', text: '#fff', outline: true },
{ value: 'outline_yellow', label: 'Outline+', box: 'transparent', text: '#FFD600', outline: true },
];
const POSITION_OPTIONS = [
{ value: 'top', label: 'top' },
{ value: 'center', label: 'center' },
{ value: 'bottom', label: 'bottom' },
];
const SIZE_OPTIONS = [
{ value: 'S', label: 'Small' },
{ value: 'M', label: 'Medium' },
{ value: 'L', label: 'Large' },
];
// Last-used hook settings, restored on the next open (style always reset to
// classic otherwise, which users read as the picker being broken).
function loadHookPrefs() {
try { return JSON.parse(localStorage.getItem('os_hook_prefs')) || {}; } catch { return {}; }
}
export default function HookModal({ isOpen, onClose, onGenerate, onRemove, isProcessing, videoUrl, initialText, durationInSeconds, existingSubtitles, hasCaptions, serverRender, burnedHook }) {
const prefs = loadHookPrefs();
const [text, setText] = useState(initialText || 'POV: You are using the viral hook feature');
const [position, setPosition] = useState(prefs.position || 'top');
const [size, setSize] = useState(prefs.size || 'M');
const [style, setStyle] = useState(prefs.style || 'classic');
const [entranceAnimation, setEntranceAnimation] = useState(prefs.entranceAnimation || 'spring');
const [displayDuration, setDisplayDuration] = useState(5);
if (!isOpen) return null;
// Build hook config for Remotion preview
const hookConfig = {
text: text || 'Enter your text...',
position,
size,
style,
entranceAnimation,
displayDurationSec: displayDuration,
};
const useRemotionPreview = !!videoUrl;
// Fallback preview logic (same as original)
const getPositionClass = () => {
switch (position) {
case 'center': return 'items-center justify-center';
case 'bottom': return 'items-center justify-end pb-[20%]';
case 'top': default: return 'items-center justify-start pt-[20%]';
}
};
const getSizeStyle = () => {
switch (size) {
case 'S': return { fontSize: '14px', maxWidth: '80%' };
case 'L': return { fontSize: '24px', maxWidth: '95%' };
case 'M': default: return { fontSize: '18px', maxWidth: '90%' };
}
};
return (
<Modal isOpen={isOpen} onClose={onClose} size="lg" eyebrow="EDITOR · HOOK" title="viral hook">
<div className="flex flex-col md:flex-row gap-6">
{/* Left: Preview */}
<div className="flex-1 flex flex-col items-center justify-center bg-black rounded-card border border-rule overflow-hidden relative aspect-[9/16] max-h-[600px]">
{useRemotionPreview ? (
<RemotionPreview
videoUrl={videoUrl}
durationInSeconds={durationInSeconds || 30}
hook={hookConfig}
subtitles={existingSubtitles || null}
/>
) : (
<>
<video src={videoUrl} className="w-full h-full object-contain opacity-50" muted playsInline />
<div className={`absolute w-full px-8 text-center transition-all duration-300 pointer-events-none flex flex-col h-full ${getPositionClass()}`}>
<div
className="text-black font-bold px-3 py-2 rounded-xl shadow-2xl text-center whitespace-pre-wrap transition-all duration-200"
style={{
...getSizeStyle(),
backgroundColor: 'rgba(255, 255, 255, 0.82)',
fontFamily: 'Noto Serif, serif',
boxShadow: '0 4px 15px rgba(0,0,0,0.5)',
paddingTop: '10px',
paddingBottom: '10px',
paddingLeft: '12px',
paddingRight: '12px'
}}
>
{text || "Enter your text..."}
</div>
</div>
</>
)}
</div>
{/* Right: Controls */}
<div className="w-full md:w-80 flex flex-col">
<div className="space-y-5 flex-1 overflow-y-auto custom-scrollbar pr-1">
{/* Text Input */}
<div>
<p className="eyebrow mb-2">Text</p>
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
rows={4}
className="input-field resize-none font-serif"
style={{ fontFamily: 'Noto Serif, serif' }}
placeholder="Enter text that will stop the scroll..."
/>
</div>
{/* Style (new) */}
<div>
<p className="eyebrow mb-2">Style</p>
<div className="grid grid-cols-3 gap-1.5">
{HOOK_STYLES.map((s) => (
<button
key={s.value}
onClick={() => setStyle(s.value)}
className={`px-1 py-2 rounded-input border text-xs transition-colors
${style === s.value ? 'border-[color:var(--color-accent)]' : 'border-rule2 hover:border-[color:var(--color-accent)]'}`}
title={s.label}
>
<span
className="block rounded px-1 py-1 font-bold"
style={{
backgroundColor: s.box,
color: s.text,
textShadow: s.outline ? '-1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000' : 'none',
}}
>Aa</span>
<span className="block mt-1 text-muted">{s.label}</span>
</button>
))}
</div>
</div>
{/* Position Control */}
<div>
<p className="eyebrow mb-2">Position</p>
<SegmentedControl
options={POSITION_OPTIONS}
value={position}
onChange={setPosition}
size="sm"
/>
{position === 'bottom' && hasCaptions && (
<p className="text-[11px] text-warn mt-1.5 leading-relaxed">
This clip has captions near the bottom — the hook may
overlap them (and TikTok's UI). Top is the safe zone.
</p>
)}
</div>
{/* Size Control */}
<div>
<p className="eyebrow mb-2">Size</p>
<SegmentedControl
options={SIZE_OPTIONS}
value={size}
onChange={setSize}
size="sm"
/>
</div>
{/* Entrance Animation (new) */}
<div className={serverRender ? 'opacity-50' : ''}>
<p className="eyebrow mb-2">Entrance</p>
<SegmentedControl
options={ENTRANCE_OPTIONS}
value={entranceAnimation}
onChange={setEntranceAnimation}
columns={2}
size="sm"
/>
{serverRender && (
<p className="text-[11px] text-muted mt-1.5 leading-relaxed">
This clip re-renders on the server, where the hook is
static — the entrance animation won't apply.
</p>
)}
</div>
{/* Display Duration (new) */}
<div>
<div className="flex items-center justify-between mb-2">
<p className="eyebrow">Duration</p>
<span className="readout">{displayDuration}S</span>
</div>
<input
type="range"
min="2"
max="15"
value={displayDuration}
onChange={(e) => setDisplayDuration(parseInt(e.target.value))}
className="w-full accent-[var(--color-accent)]"
/>
<div className="flex justify-between">
<span className="readout">2S</span>
<span className="readout">15S</span>
</div>
</div>
{burnedHook && (
<div className="p-3 border border-rule rounded-input text-xs text-muted space-y-2">
<p>
This clip has a hook burned in ("{burnedHook}").
Generating replaces it with the new one.
</p>
{onRemove && (
<button
onClick={onRemove}
disabled={isProcessing}
className="text-warn underline underline-offset-2 hover:opacity-80 transition-opacity"
>
remove hook from clip
</button>
)}
</div>
)}
<div className="p-3 border border-rule rounded-input text-xs text-muted">
Tip: keep it short and punchy. Using "POV:" or specific questions works best for retention.
</div>
</div>
<div className="flex gap-2 mt-5 shrink-0">
<button onClick={onClose} className="btn-ghost">
cancel
</button>
<button
onClick={() => {
try {
localStorage.setItem('os_hook_prefs', JSON.stringify({
style, position, size, entranceAnimation,
}));
} catch { /* ignore */ }
onGenerate({
text, position, size, style,
// Remotion data
remotion: hookConfig,
});
}}
disabled={isProcessing || !text.trim()}
className="btn-primary flex-1"
>
{isProcessing && <Loader2 size={16} className="animate-spin text-brassink" />}
{isProcessing ? 'generating...' : 'add hook'}
</button>
</div>
</div>
</div>
</Modal>
);
}