import { Box } from "@mui/material";
import { LAYOUTS } from "./config";
import { groupSegmentsIntoLayouts } from "./utils";
import { useEffect, useRef } from "react";
import { Panel } from "./Panel";
// Component for displaying a page of panels
function ComicPage({ layout, layoutIndex }) {
// Calculer le nombre total d'images dans tous les segments de ce layout
const totalImages = layout.segments.reduce((total, segment) => {
return total + (segment.images?.length || 0);
}, 0);
return (
{LAYOUTS[layout.type].panels
.slice(0, totalImages)
.map((panel, panelIndex) => {
// Trouver le segment qui contient l'image pour ce panel
let currentImageIndex = 0;
let targetSegment = null;
let targetImageIndex = 0;
for (const segment of layout.segments) {
const segmentImageCount = segment.images?.length || 0;
if (currentImageIndex + segmentImageCount > panelIndex) {
targetSegment = segment;
targetImageIndex = panelIndex - currentImageIndex;
break;
}
currentImageIndex += segmentImageCount;
}
return (
);
})}
);
}
// Main comic layout component
export function ComicLayout({ segments }) {
const layouts = groupSegmentsIntoLayouts(segments);
const scrollContainerRef = useRef(null);
// Effect to scroll to the right when new layouts are added
useEffect(() => {
if (scrollContainerRef.current) {
scrollContainerRef.current.scrollTo({
left: scrollContainerRef.current.scrollWidth,
behavior: "smooth",
});
}
}, [layouts.length]); // Only run when the number of layouts changes
return (
{layouts.map((layout, layoutIndex) => (
))}
);
}