Skip to content

Commit c46b94e

Browse files
okamoto-awsShintaro Okamoto
andauthored
fix: Style Gallery CORS error and blank preview (#12)
- Add /styles/{name} API Gateway resource (was missing, causing CORS preflight 403 → net::ERR_FAILED) - Fix StylePreview ResizeObserver deadlock: replace useEffect+early-return pattern with callback ref so container width is measured immediately when the DOM node mounts after loading→content transition - Fix double-slash in style API URLs (base already ends with /) Co-authored-by: Shintaro Okamoto <shintaok@amazon.com>
1 parent 529b640 commit c46b94e

3 files changed

Lines changed: 44 additions & 26 deletions

File tree

‎infra/lib/web-ui-stack.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ export class WebUiStack extends cdk.Stack {
187187
api.root.addResource("chat").addResource("{session_id}").addMethod("GET", integration, auth);
188188
const slides = api.root.addResource("slides");
189189
slides.addResource("search").addMethod("GET", integration, auth);
190-
api.root.addResource("styles").addMethod("GET", integration, auth);
190+
const styles = api.root.addResource("styles");
191+
styles.addMethod("GET", integration, auth);
192+
styles.addResource("{name}").addMethod("GET", integration, auth);
191193

192194
// --- Deploy web-ui static files to S3 ---
193195
const deployment = new s3deploy.BucketDeployment(this, "DeploySite", {

‎web-ui/src/components/deck/StyleGalleryModal.tsx‎

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,23 @@ function StylePreview({ html, loading }: { html: string; loading: boolean }) {
146146
const containerRef = useRef<HTMLDivElement>(null)
147147
const [containerWidth, setContainerWidth] = useState(0)
148148

149-
useEffect(() => {
150-
const el = containerRef.current
151-
if (!el) return
152-
const ro = new ResizeObserver(([entry]) => {
153-
setContainerWidth(entry.contentRect.width)
154-
})
155-
ro.observe(el)
156-
return () => ro.disconnect()
149+
// Use callback ref to handle DOM element changes (e.g. after loading→content transition)
150+
const roRef = useRef<ResizeObserver | null>(null)
151+
const measuredRef = useCallback((node: HTMLDivElement | null) => {
152+
if (roRef.current) {
153+
roRef.current.disconnect()
154+
roRef.current = null
155+
}
156+
if (node) {
157+
// Immediately measure
158+
const w = node.getBoundingClientRect().width
159+
if (w > 0) setContainerWidth(w)
160+
// Observe future resizes
161+
roRef.current = new ResizeObserver(([entry]) => {
162+
setContainerWidth(entry.contentRect.width)
163+
})
164+
roRef.current.observe(node)
165+
}
157166
}, [])
158167

159168
if (loading || !html) {
@@ -164,23 +173,30 @@ function StylePreview({ html, loading }: { html: string; loading: boolean }) {
164173
)
165174
}
166175

167-
const ratio = containerWidth > 0 ? containerWidth / 1920 : 1
176+
const ratio = containerWidth > 0 ? containerWidth / 1920 : 0
177+
const slideCount = (html.match(/class="slide"/g) || []).length || 5
168178

169179
return (
170-
<div ref={containerRef} className="overflow-x-hidden">
171-
<div style={{ width: containerWidth, height: 1080 * ratio * 10, overflow: "hidden" }}>
172-
<iframe
173-
srcDoc={html}
174-
sandbox="allow-same-origin"
175-
title="Style Preview"
176-
style={{
177-
width: 1920,
178-
height: 10800,
179-
border: "none",
180-
transformOrigin: "top left",
181-
transform: `scale(${ratio})`,
182-
}}
183-
/>
180+
<div ref={measuredRef} className="w-full overflow-x-hidden">
181+
<div style={{ width: "100%", height: ratio > 0 ? 1080 * ratio * slideCount : 400, overflow: "hidden" }}>
182+
{ratio > 0 ? (
183+
<iframe
184+
srcDoc={html}
185+
sandbox="allow-same-origin"
186+
title="Style Preview"
187+
style={{
188+
width: 1920,
189+
height: 1080 * slideCount,
190+
border: "none",
191+
transformOrigin: "top left",
192+
transform: `scale(${ratio})`,
193+
}}
194+
/>
195+
) : (
196+
<div className="flex items-center justify-center h-full">
197+
<div className="w-6 h-6 border-2 border-brand-teal/30 border-t-brand-teal rounded-full animate-spin" />
198+
</div>
199+
)}
184200
</div>
185201
</div>
186202
)

‎web-ui/src/services/deckService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export interface StyleEntry {
404404
*/
405405
export async function fetchStyles(idToken: string): Promise<StyleEntry[]> {
406406
const base = await getApiBaseUrl()
407-
const res = await fetch(`${base}/styles`, {
407+
const res = await fetch(`${base}styles`, {
408408
headers: { Authorization: `Bearer ${idToken}` },
409409
})
410410
if (!res.ok) return []
@@ -421,7 +421,7 @@ export async function fetchStyles(idToken: string): Promise<StyleEntry[]> {
421421
*/
422422
export async function fetchStyleHtml(name: string, idToken: string): Promise<string> {
423423
const base = await getApiBaseUrl()
424-
const res = await fetch(`${base}/styles/${encodeURIComponent(name)}`, {
424+
const res = await fetch(`${base}styles/${encodeURIComponent(name)}`, {
425425
headers: { Authorization: `Bearer ${idToken}` },
426426
})
427427
if (!res.ok) return ""

0 commit comments

Comments
 (0)