|
| 1 | +import { notFound } from 'next/navigation'; |
| 2 | +import { isStaffViewer } from '@/lib/auth/identity'; |
| 3 | +import { getViewer } from '@/lib/auth/viewer'; |
| 4 | +import { signInHref } from '@/lib/billing/links'; |
| 5 | +import { getSqlRunner } from '@/lib/db/connect'; |
| 6 | +import { coupleTemplatesOpen, personalEditSheetOpen, petTemplatesOpen, templateLookOpen } from '@/lib/flags'; |
| 7 | +import { getDictionary } from '@/lib/i18n/dictionaries'; |
| 8 | +import { getLocaleFromHeaders } from '@/lib/i18n/server'; |
| 9 | +import { identityVerifyHref } from '@/lib/identity/links'; |
| 10 | +import { aspectsForMedium, defaultAspect, estimateImageUsd, isImageEngine, resolveEngine } from '@/lib/images/engines'; |
| 11 | +import { defaultTimelineKeyframes, loadPromptKeyframesBySlug, splitKeyframes } from '@/lib/prompts/keyframes'; |
| 12 | +import { toPromptCard } from '@/lib/prompts/catalog'; |
| 13 | +import { adminPromptPath, templatesHref } from '@/lib/prompts/links'; |
| 14 | +import { loadPromptBySlug } from '@/lib/prompts/repo'; |
| 15 | +import { USE_CASES } from '@/lib/prompts/schemas'; |
| 16 | +import { loadTemplateRecipe } from '@/lib/recipes/repo'; |
| 17 | +import { makeInputsFrom } from '@/lib/renders/cast-inputs'; |
| 18 | +import { requestSurface } from '@/lib/routing/request-surface'; |
| 19 | +import { partRows, templatePartsView } from '@/lib/template-director/parts-view'; |
| 20 | +import { formatTemplatePrice } from '@/lib/twings/products'; |
| 21 | +import { formatResolutionPrice, offeredResolutions, supportedCreatorResolutions } from '@/lib/twings/resolution'; |
| 22 | +import { templateImageSetInfo } from '@/lib/twings/selective-frames'; |
| 23 | +import { AppHeader } from '@/components/app/app-header'; |
| 24 | +import { PersonalIdentity } from '@/components/audience/personal-identity'; |
| 25 | +import { LookMismatch } from '@/components/template-look/look-mismatch'; |
| 26 | +import { PromptDetailMedia } from '@/components/prompts/detail-media'; |
| 27 | +import { promptLabels, useCaseLabel } from '@/components/prompts/labels'; |
| 28 | +import { TypographicPoster, posterRatio } from '@/components/prompts/poster'; |
| 29 | +import { TemplateGallery } from '@/components/prompts/template-gallery'; |
| 30 | +import { TwingsStudio, type StudioStyle } from '@/components/twings/studio'; |
| 31 | + |
| 32 | +/** |
| 33 | + * `/renders/preview/[slug]` — Staff-only preview for testing `pending` and `published` personal |
| 34 | + * templates on the internal Sunra test lane using consumer-identical studio controls and resolution options. |
| 35 | + */ |
| 36 | +export const dynamic = 'force-dynamic'; |
| 37 | + |
| 38 | +export default async function StaffTemplatePreviewPage({ |
| 39 | + params, |
| 40 | +}: { |
| 41 | + params: Promise<{ slug: string }>; |
| 42 | +}) { |
| 43 | + const { slug } = await params; |
| 44 | + const [locale, viewer, sql, { origin }] = await Promise.all([ |
| 45 | + getLocaleFromHeaders(), |
| 46 | + getViewer(), |
| 47 | + getSqlRunner(), |
| 48 | + requestSurface(), |
| 49 | + ]); |
| 50 | + if (!isStaffViewer(viewer) || sql === null) notFound(); |
| 51 | + |
| 52 | + const row = await loadPromptBySlug(sql, slug); |
| 53 | + if (row === null || row.audience === 'brand' || (row.status !== 'pending' && row.status !== 'published')) { |
| 54 | + notFound(); |
| 55 | + } |
| 56 | + |
| 57 | + const recipe = await loadTemplateRecipe(sql, row.id); |
| 58 | + const makeInputs = makeInputsFrom(row, recipe); |
| 59 | + if (makeInputs === null) notFound(); |
| 60 | + |
| 61 | + const card = toPromptCard( |
| 62 | + row, |
| 63 | + { coupleOpen: coupleTemplatesOpen(), petOpen: petTemplatesOpen() }, |
| 64 | + new Map(), |
| 65 | + new Set(), |
| 66 | + new Map(recipe ? [[row.id, recipe]] : []), |
| 67 | + ); |
| 68 | + |
| 69 | + const dict = getDictionary(locale); |
| 70 | + const t = dict.templates; |
| 71 | + const labels = promptLabels(dict); |
| 72 | + const title = locale === 'ko' && card.titleKo ? card.titleKo : card.title; |
| 73 | + const useCaseLabels = Object.fromEntries(USE_CASES.map((value) => [value, useCaseLabel(dict, value)])); |
| 74 | + |
| 75 | + const offered = offeredResolutions(card); |
| 76 | + const availableResolutions = offered.length > 0 ? offered : supportedCreatorResolutions(card); |
| 77 | + const resolutionOptions = availableResolutions.map((id) => ({ |
| 78 | + id, |
| 79 | + price: offered.includes(id) ? formatResolutionPrice(card, id, locale) : '', |
| 80 | + })); |
| 81 | + |
| 82 | + const gallery = card.gallery ?? []; |
| 83 | + const setSize = gallery.length > 1 ? gallery.length : null; |
| 84 | + const resolvedEngine = resolveEngine(card.engine, card.medium); |
| 85 | + const mediaAspect = card.aspectDefault ?? defaultAspect(card.medium); |
| 86 | + const ratio = card.poster?.w && card.poster.h ? `${card.poster.w} / ${card.poster.h}` : posterRatio(card.slug); |
| 87 | + |
| 88 | + const storedKeyframes = card.medium === 'video' ? await loadPromptKeyframesBySlug(sql, card.slug) : null; |
| 89 | + const { beats: storedBeats, marks } = splitKeyframes(storedKeyframes ?? []); |
| 90 | + const videoKeyframes = |
| 91 | + card.medium === 'video' |
| 92 | + ? storedBeats.length |
| 93 | + ? storedBeats |
| 94 | + : defaultTimelineKeyframes(card.runtimeSeconds) |
| 95 | + : null; |
| 96 | + |
| 97 | + const declaredLocks = card.locks ?? null; |
| 98 | + const declaredStyling = card.styling ?? null; |
| 99 | + const declares = declaredLocks !== null || declaredStyling !== null; |
| 100 | + const partsView = declares |
| 101 | + ? templatePartsView({ |
| 102 | + locks: declaredLocks, |
| 103 | + styling: declaredStyling, |
| 104 | + marks: card.medium === 'video' ? marks : [], |
| 105 | + copy: dict.editSheet, |
| 106 | + locale, |
| 107 | + }) |
| 108 | + : []; |
| 109 | + const fixedAll = declaredLocks?.all === true; |
| 110 | + const fixed = |
| 111 | + card.medium === 'video' && declares && (partsView.length > 0 || fixedAll) |
| 112 | + ? { parts: partsView, all: fixedAll, copy: { ...t.fixed, lockedAll: dict.editSheet.lockedAll } } |
| 113 | + : null; |
| 114 | + |
| 115 | + const e = dict.editSheet; |
| 116 | + const makeParts = |
| 117 | + declares && (partsView.length > 0 || fixedAll) |
| 118 | + ? { |
| 119 | + rows: partRows(partsView), |
| 120 | + all: fixedAll, |
| 121 | + copy: { |
| 122 | + heading: t.fixed.heading, |
| 123 | + lockedAll: e.lockedAll, |
| 124 | + locked: t.fixed.locked, |
| 125 | + styled: t.fixed.styled, |
| 126 | + photo: t.fixed.photo, |
| 127 | + choiceLabel: e.styleChoiceLabel, |
| 128 | + template: e.styleTemplate, |
| 129 | + mine: e.styleMine, |
| 130 | + templateNote: card.medium === 'image' ? e.styleTemplateNoteImage : e.styleTemplateNote, |
| 131 | + mineHair: e.styleMineHair, |
| 132 | + mineMakeup: e.styleMineMakeup, |
| 133 | + overridden: e.styleOverridden, |
| 134 | + outfitPhoto: e.styleOutfitPhoto, |
| 135 | + outfitPhotoCta: e.styleOutfitPhotoCta, |
| 136 | + outfitAttached: e.styleOutfitAttached, |
| 137 | + tooLong: e.tooLong, |
| 138 | + }, |
| 139 | + } |
| 140 | + : null; |
| 141 | + |
| 142 | + const templateLook = templateLookOpen() ? card.look ?? null : null; |
| 143 | + const petOnly = makeInputs.pet && !makeInputs.selfie; |
| 144 | + |
| 145 | + const style: StudioStyle = { |
| 146 | + castInputs: { selfie: makeInputs.selfie, pet: makeInputs.pet, partner: makeInputs.partner }, |
| 147 | + id: card.slug, |
| 148 | + title, |
| 149 | + price: offered.length > 0 || card.priceCents > 0 ? formatTemplatePrice(card, locale) : '', |
| 150 | + runtime: card.runtimeSeconds ? String(card.runtimeSeconds) : '', |
| 151 | + blurb: card.useCase.map((value) => useCaseLabels[value] ?? value).join(' · '), |
| 152 | + posterSrc: card.poster?.src ?? null, |
| 153 | + videoSrc: card.video?.src ?? null, |
| 154 | + moreHref: templatesHref(locale, card.medium), |
| 155 | + aspects: aspectsForMedium(card.medium).map((id) => ({ |
| 156 | + id, |
| 157 | + label: t.aspects[id as keyof typeof t.aspects] ?? id, |
| 158 | + })), |
| 159 | + defaultAspect: card.aspectDefault ?? defaultAspect(card.medium), |
| 160 | + resolutions: resolutionOptions, |
| 161 | + imageSetSpec: |
| 162 | + card.medium === 'image' && setSize |
| 163 | + ? { |
| 164 | + totalFrames: setSize, |
| 165 | + perFrameProviderUsd: isImageEngine(resolvedEngine) ? estimateImageUsd(resolvedEngine, 1) : 0.09, |
| 166 | + royaltyKrw: templateImageSetInfo(card, null).royaltyKrw, |
| 167 | + frameThumbnails: gallery.map((f) => f.src), |
| 168 | + } |
| 169 | + : null, |
| 170 | + }; |
| 171 | + |
| 172 | + const previewPath = `/renders/preview/${slug}`; |
| 173 | + const stageFrames = card.medium === 'image' ? (setSize ? gallery : card.poster ? [card.poster] : null) : null; |
| 174 | + const stageStart = |
| 175 | + setSize && card.poster ? Math.max(0, gallery.findIndex((frame) => frame.src === card.poster!.src)) : 0; |
| 176 | + const backToAdminHref = |
| 177 | + origin && /^https:\/\/(?:www\.)?twings\.app$/i.test(origin) |
| 178 | + ? `https://admin.twings.app${adminPromptPath(row.id)}` |
| 179 | + : adminPromptPath(row.id); |
| 180 | + |
| 181 | + return ( |
| 182 | + <PersonalIdentity> |
| 183 | + <div data-staff-preview={card.slug} data-status={row.status} data-section data-theme="light" className="section min-h-screen"> |
| 184 | + <AppHeader locale={locale} dict={dict} rest={previewPath} /> |
| 185 | + <main id="main" className="mx-auto w-full max-w-[calc(var(--container-audience)+2*var(--spacing-gutter))] px-[var(--spacing-gutter)] pt-8 pb-24"> |
| 186 | + <div className="mb-6 flex flex-wrap items-center justify-between gap-3 border-b border-hairline pb-4"> |
| 187 | + <div className="flex flex-wrap items-center gap-2.5 text-sm"> |
| 188 | + <span className="inline-flex items-center rounded-full bg-[#F4F4F5] px-3 py-1 font-medium text-[#09090B]"> |
| 189 | + {dict.renders.studio.previewHeading} |
| 190 | + </span> |
| 191 | + {row.status === 'pending' && ( |
| 192 | + <span className="text-xs text-[#52525B]">{dict.renders.studio.previewPendingNotice}</span> |
| 193 | + )} |
| 194 | + </div> |
| 195 | + {viewer.isAdmin && ( |
| 196 | + <a href={backToAdminHref} className="inline-flex min-h-10 items-center text-sm text-[#52525B] underline-offset-4 hover:text-[#09090B] hover:underline"> |
| 197 | + ← {dict.renders.studio.previewBackToAdmin} |
| 198 | + </a> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + |
| 202 | + <div className="grid gap-7 lg:grid-cols-[minmax(0,1fr)_minmax(22rem,25rem)] lg:items-start lg:gap-12 xl:grid-cols-[minmax(0,1fr)_26rem] xl:gap-16"> |
| 203 | + <div className="min-w-0"> |
| 204 | + {stageFrames ? ( |
| 205 | + <TemplateGallery |
| 206 | + frames={stageFrames} |
| 207 | + initialIndex={stageStart} |
| 208 | + setSize={setSize} |
| 209 | + title={title} |
| 210 | + aspect={mediaAspect} |
| 211 | + makeHref="#make" |
| 212 | + selfieTile={!petOnly} |
| 213 | + labels={{ |
| 214 | + ...t.gallery, |
| 215 | + lead: petOnly ? dict.cast.pet.transform : setSize ? t.gallery.transformSet : t.gallery.transformImage, |
| 216 | + position: dict.renders.frames.position, |
| 217 | + selfie: dict.creatorJourney.evidenceSelfie, |
| 218 | + }} |
| 219 | + /> |
| 220 | + ) : ( |
| 221 | + <PromptDetailMedia |
| 222 | + poster={card.poster} |
| 223 | + video={card.video} |
| 224 | + alt={labels.resultLabel} |
| 225 | + ratio={ratio} |
| 226 | + aspect={mediaAspect} |
| 227 | + keyframes={videoKeyframes} |
| 228 | + {...(fixed ? { fixed } : {})} |
| 229 | + locale={locale} |
| 230 | + controls={{ |
| 231 | + play: dict.campaigns.landing.reelPlay, |
| 232 | + pause: dict.campaigns.landing.reelPause, |
| 233 | + unmute: dict.campaigns.landing.reelUnmute, |
| 234 | + mute: dict.campaigns.landing.reelMute, |
| 235 | + }} |
| 236 | + fallback={<TypographicPoster title={card.title} slug={card.slug} kicker={dict.nav[card.medium]} ratio={ratio} />} |
| 237 | + /> |
| 238 | + )} |
| 239 | + </div> |
| 240 | + |
| 241 | + <section id="make" className="min-w-0"> |
| 242 | + <p className="text-[13px] font-medium text-[#71717A]">{dict.nav[card.medium]}</p> |
| 243 | + <h1 className="mt-2 font-display text-[clamp(1.6rem,2.6vw,2.2rem)] leading-[1.16] font-bold tracking-[-0.028em] text-balance break-keep"> |
| 244 | + {title} |
| 245 | + </h1> |
| 246 | + <TwingsStudio |
| 247 | + styles={[style]} |
| 248 | + initialStyleId={style.id} |
| 249 | + shopOpenHint={true} |
| 250 | + stickyBar={false} |
| 251 | + live={{ |
| 252 | + endpoint: '/api/renders', |
| 253 | + accessEndpoint: '/api/renders/access', |
| 254 | + interestEndpoint: '/api/notify/interest', |
| 255 | + template: card.slug, |
| 256 | + locale, |
| 257 | + signinHref: signInHref(previewPath, locale), |
| 258 | + identityHref: identityVerifyHref(previewPath), |
| 259 | + selfie: dict.selfie, |
| 260 | + cast: dict.cast.photo, |
| 261 | + subscription: { copy: dict.creatorSubscription, editorCopy: dict.premiumEditor }, |
| 262 | + ...(personalEditSheetOpen() ? { editSheet: dict.editSheet } : {}), |
| 263 | + ...(card.locks ? { locks: card.locks, lockCopy: dict.editSheet } : {}), |
| 264 | + ...(card.styling ? { styling: card.styling, lockCopy: dict.editSheet } : {}), |
| 265 | + ...(card.editsClosed ? { editsClosed: t.editsClosed } : {}), |
| 266 | + ...(card.editRoom ? { editRoom: card.editRoom } : {}), |
| 267 | + ...(makeParts ? { parts: makeParts } : {}), |
| 268 | + ...(templateLook ? { templateLook: { look: templateLook, note: dict.templateLook.mismatch } } : {}), |
| 269 | + labels: { |
| 270 | + badge: dict.renders.studio.badge, |
| 271 | + operatorBadge: dict.renders.studio.operatorBadge, |
| 272 | + freeCta: dict.renders.studio.freeCta, |
| 273 | + submitting: dict.renders.studio.submitting, |
| 274 | + identityNotice: dict.renders.studio.identityNotice, |
| 275 | + identityPending: dict.renders.studio.identityPending, |
| 276 | + identityCta: dict.renders.studio.identityCta, |
| 277 | + errors: dict.renders.studio.errors, |
| 278 | + }, |
| 279 | + }} |
| 280 | + labels={{ |
| 281 | + stepPhoto: dict.studio.stepPhoto, |
| 282 | + stepStyle: dict.studio.stepStyle, |
| 283 | + stepPay: dict.studio.stepPay, |
| 284 | + stepResult: dict.studio.stepResult, |
| 285 | + dropZone: dict.studio.dropZone, |
| 286 | + dropHint: dict.studio.dropHint, |
| 287 | + changePhoto: dict.studio.changePhoto, |
| 288 | + chooseStyle: dict.studio.chooseStyle, |
| 289 | + payCta: dict.studio.payCta, |
| 290 | + payNote: dict.studio.payNote, |
| 291 | + closedTitle: dict.studio.closedTitle, |
| 292 | + closedBody: dict.studio.closedBody, |
| 293 | + closedCta: dict.studio.closedCta, |
| 294 | + closedDone: dict.studio.closedDone, |
| 295 | + signedOutCta: t.makeCta, |
| 296 | + pending: dict.studio.pending, |
| 297 | + makeCta: t.makeCta, |
| 298 | + makeCtaClosed: t.makeCtaClosed, |
| 299 | + otherAspect: dict.studio.otherAspect, |
| 300 | + runtimeLabel: dict.studio.runtimeLabel, |
| 301 | + needPhoto: dict.studio.needPhoto, |
| 302 | + noStyles: dict.studio.noStyles, |
| 303 | + moreTemplates: dict.studio.moreTemplates, |
| 304 | + aspectLabel: dict.studio.aspectLabel, |
| 305 | + resolutionLabel: dict.studio.resolutionLabel, |
| 306 | + resolutionDefault: dict.studio.resolutionDefault, |
| 307 | + resolutionNote: dict.studio.resolutionNote, |
| 308 | + }} |
| 309 | + /> |
| 310 | + {templateLook && <LookMismatch template={templateLook} note={dict.templateLook.mismatch} className="mt-3" />} |
| 311 | + </section> |
| 312 | + </div> |
| 313 | + </main> |
| 314 | + </div> |
| 315 | + </PersonalIdentity> |
| 316 | + ); |
| 317 | +} |
0 commit comments