79 lines
2.8 KiB
TypeScript
79 lines
2.8 KiB
TypeScript
import React, { useMemo, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { InsufficientCreditError } from '../../utils/api';
|
|
import { createPlayreelJob, parseGoodsId } from '../../utils/playreelApi';
|
|
import PlayreelAutoApprovePanel from './PlayreelAutoApprovePanel';
|
|
|
|
interface PlayreelCreateFormProps {
|
|
/** 생성 요청이 접수되면 호출. 폴링 소유는 GenerationFlow (썰박스·포스터와 같은 규칙) */
|
|
onSubmitted: (jobId: string) => void;
|
|
/** 크레딧 부족(402) 시 충전 화면으로 유도 */
|
|
onNeedCredit: () => void;
|
|
/** 시작 선택 화면으로 돌아간다 */
|
|
onBack: () => void;
|
|
}
|
|
|
|
/**
|
|
* 롱컷(공연 상품페이지 → 30초 예고편) 생성 폼. 진입 화면의 '롱컷' 탭에 들어간다.
|
|
*
|
|
* 입력 도중의 안내(상품 번호 인식)는 `parseGoodsId` 가 하고, 판정은 서버가 같은 규칙으로 한다.
|
|
*/
|
|
const PlayreelCreateForm: React.FC<PlayreelCreateFormProps> = ({ onSubmitted, onNeedCredit, onBack }) => {
|
|
const { t } = useTranslation();
|
|
|
|
const [url, setUrl] = useState('');
|
|
const [submitting, setSubmitting] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const goodsId = useMemo(() => parseGoodsId(url), [url]);
|
|
|
|
const submit = async () => {
|
|
if (!goodsId || submitting) return;
|
|
setSubmitting(true);
|
|
setError(null);
|
|
try {
|
|
const { id } = await createPlayreelJob(url);
|
|
onSubmitted(id);
|
|
} catch (e) {
|
|
if (e instanceof InsufficientCreditError) {
|
|
onNeedCredit();
|
|
} else {
|
|
setError((e as Error).message);
|
|
}
|
|
setSubmitting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p2v-scope p2v-create">
|
|
<button type="button" className="p2v-back" onClick={onBack}>{t('poster.start.back')}</button>
|
|
|
|
<span className="p2v-step">{t('playreel.create.step')}</span>
|
|
|
|
<input
|
|
className="p2v-input p2v-input--center"
|
|
placeholder={t('playreel.create.placeholder')}
|
|
value={url}
|
|
onChange={(e) => setUrl(e.target.value)}
|
|
onKeyDown={(e) => { if (e.key === 'Enter') submit(); }}
|
|
/>
|
|
|
|
{goodsId && (
|
|
<p className="p2v-note">
|
|
<span className="p2v-tag">{t('playreel.create.recognized', { id: goodsId })}</span>
|
|
</p>
|
|
)}
|
|
{url.trim() && !goodsId && <div className="p2v-note warn">{t('playreel.create.unsupported')}</div>}
|
|
{error && <div className="p2v-note warn">{error}</div>}
|
|
|
|
<button type="button" className="p2v-btn-cta" disabled={!goodsId || submitting} onClick={submit}>
|
|
{submitting ? t('playreel.create.submitting') : t('playreel.create.submit')}
|
|
</button>
|
|
|
|
{/* 게이트 ①·③ 자동 승인 — 시작 전에 정하고, 되돌리는 자리도 여기다 */}
|
|
<PlayreelAutoApprovePanel />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default PlayreelCreateForm;
|