'use client'; import { useEffect, useState } from 'react'; import { fetchApi } from '@/lib/utils/client'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import type { StudioSettingsResponse } from '@/types/response/studio/settings'; const CODE_PATTERN = /^[A-Za-z0-9]{4,7}$/; type IssueResponse = { donationCode: string; }; export default function StudioDonationCodePage() { const [loading, setLoading] = useState(true); const [existingCode, setExistingCode] = useState(null); const [input, setInput] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { fetchApi('/api/studio/settings') .then(res => { if (res.data) { setExistingCode(res.data.donationCode); } }) .catch(() => {}) .finally(() => { setLoading(false); }); }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const trimmed = input.trim(); if (!CODE_PATTERN.test(trimmed)) { setError('후원 코드는 4~7자 영문/숫자만 가능합니다.'); return; } const normalized = trimmed.toUpperCase(); const ok = window.confirm(`후원 코드 "${normalized}" 로 등록합니다.\n한 번 등록하면 변경할 수 없습니다. 진행하시겠습니까?`); if (!ok) { return; } setSubmitting(true); setError(null); const res = await fetchApi('/api/studio/donation-code', { method: 'POST', body: { code: normalized }, silent: true }); if (res.success && res.data) { setExistingCode(res.data.donationCode); setInput(''); window.alert(`후원 코드 "${res.data.donationCode}" 가 정상적으로 등록되었습니다.`); } else { setError(res.message || '후원 코드 발급에 실패했습니다.'); } setSubmitting(false); }; if (loading) { return (

불러오는 중...

); } return (

후원 코드

후원 코드는 시청자가 후원 채널을 빠르게 찾도록 도와줍니다. 4~7자 영문/숫자만 가능하며, 대소문자 구분 없이 대문자로 저장됩니다.
한 번 등록하면 변경할 수 없습니다.

{existingCode !== null ? (

* 이미 등록되어 변경할 수 없습니다.

) : (
{ setInput(e.target.value); setError(null); }} placeholder="예: ABC123" maxLength={7} autoComplete="off" spellCheck={false} className="mt-2 font-mono text-base tracking-widest uppercase" disabled={submitting} />

* 4~7자 영문/숫자만 사용 가능. 특수문자·한글 불가.

{error && (

{error}

)}
)}
); }