feat: main 병합 + 인스타 올리기 API(/v1/card/{id}/instagram), 관리자 올릴 곳 선택(로컬·킹서버), 만든 카드 화면
This commit is contained in:
commit
572b2a05c8
188 files changed
+16605
-741
No files matched your search
+108
-24
@@ -11,6 +11,8 @@ import { CATEGORIES, categoryOf, questionOf } from '../cards/topics'
|
||||
import { GAK_DECK, gakByCode } from '../cards/gaks'
|
||||
import type { IndexSummary } from '../api/client'
|
||||
import { neededImages } from '../cards/images'
|
||||
import { cardAction, postCard, useTargets } from '../api/targets'
|
||||
import type { CardSummary, Target } from '../api/targets'
|
||||
import { DEFAULT_STYLE, OOTD_STYLES } from '../cards/ootd'
|
||||
import type { Copy, Template } from '../cards/templates'
|
||||
|
||||
@@ -314,21 +316,14 @@ function Editor({ template, data, overrides, onEdit }: { template: Template; dat
|
||||
|
||||
<PostBox
|
||||
disabled={busy !== null || !made}
|
||||
onPost={async (publish) => {
|
||||
buildBody={async (publish) => {
|
||||
const images = []
|
||||
for (const i of slides.map((_, k) => k)) {
|
||||
const node = refs.current[i]?.firstElementChild as HTMLElement | null
|
||||
if (node) images.push(await toJpeg(node))
|
||||
}
|
||||
const { title, caption } = template.post(data, copy)
|
||||
const res = await fetch('/api/post', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ template: template.id, topic: copy.topic ?? null, title, caption, texts: copy, images, publish }),
|
||||
})
|
||||
const body = await res.json()
|
||||
if (!res.ok || body.result?.success === false) throw new Error(body.error ?? body.result?.desc ?? `HTTP ${res.status}`)
|
||||
return body as { card_id: number; status: number; post_id?: number }
|
||||
return { template: template.id, topic: copy.topic ?? null, title, caption, texts: copy, images, publish }
|
||||
}}
|
||||
/>
|
||||
<CommandBox command={command} />
|
||||
@@ -662,37 +657,126 @@ function HowBox({ template }: { template: Template }) {
|
||||
)
|
||||
}
|
||||
|
||||
/** 날씨각 피드에 올리기: 바로 게시하거나, 승인 대기로 저장(나중에 Teams 승인으로 게시) */
|
||||
function PostBox({ disabled, onPost }: { disabled: boolean; onPost: (publish: boolean) => Promise<{ card_id: number; post_id?: number }> }) {
|
||||
const [state, setState] = useState<{ busy?: boolean; done?: string; error?: string }>({})
|
||||
type Posted = Record<string, { busy?: boolean; card?: CardSummary; error?: string; ig?: string; igBusy?: boolean; igError?: string }>
|
||||
|
||||
/** 날씨각 피드에 올리기: 올릴 곳(로컬·킹서버)을 골라 바로 게시하거나 승인 대기로 저장. 올린 뒤 인스타(@nalssigak)에도 */
|
||||
function PostBox({ disabled, buildBody }: { disabled: boolean; buildBody: (publish: boolean) => Promise<unknown> }) {
|
||||
const { data: targets = [] } = useTargets()
|
||||
const [picked, setPicked] = useState<Target['id'][] | null>(null)
|
||||
const chosen = picked ?? targets.map((t) => t.id)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [posted, setPosted] = useState<Posted>({})
|
||||
const [error, setError] = useState('')
|
||||
|
||||
const run = async (publish: boolean) => {
|
||||
setState({ busy: true })
|
||||
setBusy(true)
|
||||
setError('')
|
||||
try {
|
||||
const r = await onPost(publish)
|
||||
setState({ done: publish ? `날씨각 피드에 올렸어요 (카드 ${r.card_id}, 포스트 ${r.post_id}). 오늘 피드 날씨 바로 아래에 보여요.` : `승인 대기로 저장했어요 (카드 ${r.card_id}).` })
|
||||
const body = await buildBody(publish)
|
||||
await Promise.all(
|
||||
chosen.map(async (id) => {
|
||||
setPosted((p) => ({ ...p, [id]: { busy: true } }))
|
||||
try {
|
||||
const card = await postCard(id, body)
|
||||
setPosted((p) => ({ ...p, [id]: { card } }))
|
||||
} catch (e) {
|
||||
setPosted((p) => ({ ...p, [id]: { error: e instanceof Error ? e.message : String(e) } }))
|
||||
}
|
||||
}),
|
||||
)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
setState({ error: e instanceof Error ? e.message : String(e) })
|
||||
setError(`이미지를 만들지 못했어요: ${e instanceof Error ? e.message : String(e)}`)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
// 인스타는 이미지가 공개 주소여야 해서 킹서버에 올린 카드로만 올린다
|
||||
const igTarget = targets.find((t) => t.id === 'king') ?? targets[0]
|
||||
const igCard = igTarget ? posted[igTarget.id]?.card : undefined
|
||||
const toInstagram = async () => {
|
||||
if (!igTarget || !igCard) return
|
||||
setPosted((p) => ({ ...p, [igTarget.id]: { ...p[igTarget.id], igBusy: true, igError: undefined } }))
|
||||
try {
|
||||
const r = await cardAction(igTarget.id, igCard.card_id, 'instagram')
|
||||
setPosted((p) => ({ ...p, [igTarget.id]: { ...p[igTarget.id], igBusy: false, ig: r.ig_media_id } }))
|
||||
} catch (e) {
|
||||
setPosted((p) => ({ ...p, [igTarget.id]: { ...p[igTarget.id], igBusy: false, igError: e instanceof Error ? e.message : String(e) } }))
|
||||
}
|
||||
}
|
||||
const ig = igTarget ? posted[igTarget.id] : undefined
|
||||
|
||||
return (
|
||||
<section className="flex flex-col gap-2 border-t border-line pt-5">
|
||||
<section className="flex flex-col gap-3 border-t border-line pt-5">
|
||||
<h2 className="text-[15px] font-bold">날씨각에 올리기</h2>
|
||||
<p className="text-[12px] leading-relaxed text-muted">지금 카드를 이미지로 만들어 오늘 전국 피드에 올려요. 내일 03시에 피드에서 빠져요.</p>
|
||||
{targets.length === 0 ? (
|
||||
<p className="text-[12px] text-muted">올릴 곳이 없어요. 관리자 서버에 ADMIN_API_KEY(로컬)·KING_ADMIN_API_KEY(킹서버)를 환경변수로 주고 다시 켜 주세요.</p>
|
||||
) : (
|
||||
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-[13px]">
|
||||
<span className="text-muted">올릴 곳</span>
|
||||
{targets.map((t) => (
|
||||
<label key={t.id} className="flex items-center gap-1.5 font-semibold">
|
||||
<input type="checkbox" id={`target-${t.id}`} checked={chosen.includes(t.id)} onChange={(e) => setPicked(e.target.checked ? [...chosen, t.id] : chosen.filter((x) => x !== t.id))} className="accent-ink" />
|
||||
{t.label}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<p className="text-[12px] leading-relaxed text-muted">지금 카드를 이미지로 만들어 고른 곳의 오늘 전국 피드에 올려요. 내일 03시에 피드에서 빠져요.</p>
|
||||
<div className="flex gap-2">
|
||||
<button onClick={() => run(true)} disabled={disabled || state.busy} className="flex-1 rounded-xl bg-ink px-4 py-2.5 text-[14px] font-bold text-white hover:opacity-90 disabled:opacity-40">
|
||||
{state.busy ? '올리는 중…' : '지금 올리기'}
|
||||
<button onClick={() => run(true)} disabled={disabled || busy || chosen.length === 0} className="flex-1 rounded-xl bg-ink px-4 py-2.5 text-[14px] font-bold text-white hover:opacity-90 disabled:opacity-40">
|
||||
{busy ? '올리는 중…' : '지금 올리기'}
|
||||
</button>
|
||||
<button onClick={() => run(false)} disabled={disabled || state.busy} className="rounded-xl border border-ink px-3 py-2.5 text-[13px] font-bold text-ink hover:bg-rail disabled:opacity-40">
|
||||
<button onClick={() => run(false)} disabled={disabled || busy || chosen.length === 0} className="rounded-xl border border-ink px-3 py-2.5 text-[13px] font-bold text-ink hover:bg-rail disabled:opacity-40">
|
||||
승인 대기로 저장
|
||||
</button>
|
||||
</div>
|
||||
{state.done && <p className="text-[12px] font-semibold text-ink-soft">{state.done}</p>}
|
||||
{state.error && (
|
||||
{error && (
|
||||
<p role="alert" className="text-[12px] text-bad">
|
||||
올리지 못했어요: {state.error}
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{Object.keys(posted).length > 0 && (
|
||||
<ul className="m-0 flex list-none flex-col gap-1.5 p-0 text-[12px]">
|
||||
{targets
|
||||
.filter((t) => posted[t.id])
|
||||
.map((t) => {
|
||||
const r = posted[t.id]
|
||||
return (
|
||||
<li key={t.id} className="flex flex-wrap items-center gap-x-2 rounded-lg bg-page px-3 py-2">
|
||||
<b>{t.label}</b>
|
||||
{r.busy && <span className="text-muted">올리는 중…</span>}
|
||||
{r.card && (
|
||||
<>
|
||||
<span className="text-ink-soft">
|
||||
{r.card.status === 2 ? `올렸어요 · 카드 ${r.card.card_id} · 포스트 ${r.card.post_id}` : `승인 대기로 저장 · 카드 ${r.card.card_id}`}
|
||||
</span>
|
||||
{r.card.status === 2 && (
|
||||
<a href={t.site} target="_blank" rel="noreferrer" className="font-semibold text-rust underline-offset-2 hover:underline">
|
||||
피드에서 보기
|
||||
</a>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{r.error && <span className="text-bad">올리지 못했어요: {r.error}</span>}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
{igTarget && igCard?.status === 2 && (
|
||||
<div className="flex flex-col gap-1.5 border-t border-line pt-3">
|
||||
<button onClick={toInstagram} disabled={ig?.igBusy || !!ig?.ig} className="rounded-xl border border-ink px-4 py-2.5 text-[14px] font-bold text-ink hover:bg-rail disabled:opacity-40">
|
||||
{ig?.ig ? `인스타에 올렸어요 (${ig.ig})` : ig?.igBusy ? '인스타에 올리는 중… (1분쯤)' : `인스타(@nalssigak)에도 올리기 · ${igTarget.label} 이미지로`}
|
||||
</button>
|
||||
{ig?.igError && (
|
||||
<p role="alert" className="text-[12px] text-bad">
|
||||
인스타에 올리지 못했어요: {ig.igError}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user