page.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use client';
  2. import { useState, useEffect, useRef } from 'react';
  3. import { useRouter } from 'next/navigation';
  4. import Link from 'next/link';
  5. import { fetchApi } from '@/lib/utils/client';
  6. import { useStudioContext } from '@/app/studio/context';
  7. import { useAlertConfigContext } from '../context';
  8. import { Separator } from '@/components/ui/separator';
  9. import AlertPreviewPanel from '../_components/AlertPreviewPanel';
  10. import AlertFormPanel from '../_components/AlertFormPanel';
  11. import { createEmptyForm } from '../types';
  12. import type { FormState, PendingFiles } from '../types';
  13. export default function AlertAddPage()
  14. {
  15. const router = useRouter();
  16. const { channelID, memberID } = useStudioContext();
  17. const { widgetToken, setSaving, fetchList } = useAlertConfigContext();
  18. const [form, setForm] = useState<FormState>(createEmptyForm());
  19. const [pendingFiles, setPendingFiles] = useState<PendingFiles>({ image: null, sound: null });
  20. const [localSaving, setLocalSaving] = useState(false);
  21. const iframeRef = useRef<HTMLIFrameElement>(null);
  22. const formRef = useRef<FormState>(form);
  23. formRef.current = form;
  24. // ── blob URL cleanup ─────────────────────────────
  25. const cleanupBlobUrls = (f: FormState) => {
  26. if (f.imageUrl?.startsWith('blob:')) {
  27. URL.revokeObjectURL(f.imageUrl);
  28. }
  29. if (f.soundUrl?.startsWith('blob:')) {
  30. URL.revokeObjectURL(f.soundUrl);
  31. }
  32. };
  33. // unmount 시 cleanup
  34. useEffect(() => {
  35. return () => {
  36. cleanupBlobUrls(formRef.current);
  37. };
  38. }, []);
  39. // ── 폼 → iframe 미리보기 동기화 ─────────────────
  40. useEffect(() => {
  41. if (!iframeRef.current?.contentWindow) {
  42. return;
  43. }
  44. iframeRef.current.contentWindow.postMessage({
  45. type: 'ALERT_PREVIEW',
  46. config: form,
  47. }, window.location.origin);
  48. }, [form]);
  49. // ── 폼 필드 변경 ────────────────────────────────
  50. const handleFormChange = <K extends keyof FormState>(field: K, value: FormState[K]) => {
  51. setForm(prev => {
  52. if ((field === 'imageUrl' || field === 'soundUrl') && typeof prev[field] === 'string' && (prev[field] as string).startsWith('blob:')) {
  53. URL.revokeObjectURL(prev[field] as string);
  54. }
  55. return { ...prev, [field]: value };
  56. });
  57. if (field === 'imageUrl' && value === null) {
  58. setPendingFiles(prev => ({ ...prev, image: null }));
  59. }
  60. if (field === 'soundUrl' && value === null) {
  61. setPendingFiles(prev => ({ ...prev, sound: null }));
  62. }
  63. };
  64. // ── 파일 업로드 헬퍼 ─────────────────────────────
  65. const uploadFile = async (file: File, type: 'image'|'sound'): Promise<string> => {
  66. const formData = new FormData();
  67. formData.append('file', file);
  68. formData.append('type', type);
  69. formData.append('channelID', channelID!.toString());
  70. const res = await fetchApi<{ url: string }>('/api/studio/donation/alert/config/upload', {
  71. method: 'POST',
  72. body: formData,
  73. });
  74. return res.data?.url ?? '';
  75. };
  76. // ── 저장 ─────────────────────────────────────────
  77. const handleSave = async () => {
  78. if (!channelID) {
  79. return;
  80. }
  81. if (!form.message.trim()) {
  82. alert('메시지를 입력해 주세요.');
  83. return;
  84. }
  85. if (form.amount < 1) {
  86. alert('금액은 1원 이상이어야 합니다.');
  87. return;
  88. }
  89. if (form.displayDurationSec < 1) {
  90. alert('노출 시간은 1초 이상이어야 합니다.');
  91. return;
  92. }
  93. setLocalSaving(true);
  94. setSaving(true);
  95. try {
  96. let finalImageUrl = form.imageUrl;
  97. let finalSoundUrl = form.soundUrl;
  98. if (pendingFiles.image) {
  99. finalImageUrl = await uploadFile(pendingFiles.image, 'image');
  100. }
  101. if (pendingFiles.sound) {
  102. finalSoundUrl = await uploadFile(pendingFiles.sound, 'sound');
  103. }
  104. const item = {
  105. id: null,
  106. ...form,
  107. imageUrl: finalImageUrl,
  108. soundUrl: finalSoundUrl,
  109. popupEffect: form.popupEffect || null,
  110. textEffect: form.textEffect || null,
  111. nicknameFontFamily: form.nicknameFontFamily || null,
  112. amountFontFamily: form.amountFontFamily || null,
  113. messageFontFamily: form.messageFontFamily || null,
  114. };
  115. await fetchApi('/api/studio/donation/alert/config/batch', {
  116. method: 'POST',
  117. body: { channelID, memberID, items: [item], deleteIDs: [] },
  118. });
  119. cleanupBlobUrls(form);
  120. alert('등록되었습니다.');
  121. fetchList();
  122. router.push('/studio/donation/alert/list');
  123. } catch (err) {
  124. alert(err instanceof Error ? err.message : '저장에 실패했습니다.');
  125. } finally {
  126. setLocalSaving(false);
  127. setSaving(false);
  128. }
  129. };
  130. // ── 취소 ─────────────────────────────────────────
  131. const handleCancel = () => {
  132. cleanupBlobUrls(form);
  133. router.push('/studio/donation/alert/list');
  134. };
  135. return (
  136. <>
  137. <div className="studio-page__title-row">
  138. <h1 className="studio-page__title">후원 알림 추가</h1>
  139. <Link href="/studio/donation/alert/list" className="alert-config__btn alert-config__btn--sm">< 목록으로</Link>
  140. </div>
  141. <div className='pt-5 pb-5'>
  142. <Separator orientation="horizontal" />
  143. </div>
  144. <div className="alert-config__layout">
  145. <AlertPreviewPanel
  146. widgetToken={widgetToken}
  147. iframeRef={iframeRef}
  148. />
  149. <Separator orientation="vertical" />
  150. <AlertFormPanel
  151. form={form}
  152. editingItem={null}
  153. saving={localSaving}
  154. pendingFiles={pendingFiles}
  155. onFileSelect={(file, type) => {
  156. const previewUrl = URL.createObjectURL(file);
  157. if (type === 'image') {
  158. setPendingFiles(prev => ({ ...prev, image: file }));
  159. handleFormChange('imageUrl', previewUrl);
  160. } else {
  161. setPendingFiles(prev => ({ ...prev, sound: file }));
  162. handleFormChange('soundUrl', previewUrl);
  163. }
  164. }}
  165. onFormChange={handleFormChange}
  166. onSave={handleSave}
  167. onCancel={handleCancel}
  168. />
  169. </div>
  170. </>
  171. );
  172. }