File size: 6,809 Bytes
303d067
9b70d7b
 
41178f4
15a4294
9b70d7b
 
 
 
 
 
 
 
58c8c26
ed33547
9b70d7b
41178f4
9b70d7b
 
 
 
 
 
 
 
58c8c26
ed33547
9b70d7b
41178f4
9b70d7b
 
 
ed33547
41178f4
ed33547
303d067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b70d7b
 
41178f4
 
 
 
9b70d7b
41178f4
 
 
 
 
303d067
41178f4
 
303d067
41178f4
 
 
303d067
 
 
41178f4
 
303d067
41178f4
 
 
 
ed33547
 
 
15a4294
 
 
 
 
 
 
 
 
 
 
ed33547
41178f4
 
 
9b70d7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41178f4
 
 
 
 
 
 
 
 
 
 
 
 
15a4294
41178f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import type { CSSProperties } from "react";

import type { FileMeta, Page, Session, TemplateFields } from "../types/session";
import { BASE_H, BASE_W } from "../lib/report";
import { JobSheetTemplate } from "./JobSheetTemplate";

type ReportPageCanvasProps = {
  session: Session | null;
  page: Page | null;
  pageIndex: number;
  pageCount: number;
  scale: number;
  template?: TemplateFields;
  sectionLabel?: string;
  className?: string;
  adaptive?: boolean;
};

export function ReportPageCanvas({
  session,
  page,
  pageIndex,
  pageCount,
  scale,
  template,
  sectionLabel,
  className = "",
  adaptive = false,
}: ReportPageCanvasProps) {
  const items = page?.items ?? [];
  const safeScale = Number.isFinite(scale) && scale > 0 ? scale : 1;
  const pageVariant = page?.variant ?? "full";
  const photos = resolvePagePhotos(session, page, pageIndex);
  const sheets = adaptive ? [photos] : [photos];
  const sheetRefs = useRef<Array<HTMLDivElement | null>>([]);
  const [sheetHeights, setSheetHeights] = useState<number[]>([]);

  const defaultHeight = BASE_H * safeScale;
  const resolvedHeights = useMemo(() => {
    if (sheetHeights.length !== sheets.length) {
      return sheets.map(() => defaultHeight);
    }
    return sheetHeights.map((height) => (height > 0 ? height : defaultHeight));
  }, [defaultHeight, sheetHeights, sheets.length]);

  const offsets = useMemo(() => {
    const values: number[] = [];
    let running = 0;
    resolvedHeights.forEach((height) => {
      values.push(running);
      running += height;
    });
    return values;
  }, [resolvedHeights]);

  const containerHeight = resolvedHeights.reduce((sum, height) => sum + height, 0);

  const measureHeights = () => {
    const next = sheets.map((_, index) => {
      const node = sheetRefs.current[index];
      if (!node) return defaultHeight;
      const rect = node.getBoundingClientRect();
      return rect.height || defaultHeight;
    });
    setSheetHeights((prev) => {
      if (prev.length === next.length && prev.every((val, idx) => Math.abs(val - next[idx]) < 1)) {
        return prev;
      }
      return next;
    });
  };

  useLayoutEffect(() => {
    measureHeights();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [sheets.length, safeScale]);

  useEffect(() => {
    const observer = new ResizeObserver(() => measureHeights());
    sheetRefs.current.forEach((node) => {
      if (node) observer.observe(node);
    });
    return () => observer.disconnect();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [sheets.length]);

  return (
    <div
      className={["relative w-full", className].join(" ")}
      style={{ height: `${containerHeight}px` }}
    >
      <div className="absolute inset-0 pointer-events-none">
        {sheets.map((sheetPhotos, sheetIndex) => (
          <div
            key={`sheet-${sheetIndex}`}
            style={{
              position: "absolute",
              top: `${offsets[sheetIndex] ?? 0}px`,
              left: 0,
              width: `${BASE_W * safeScale}px`,
              height: `${resolvedHeights[sheetIndex] ?? defaultHeight}px`,
            }}
          >
            <div
              ref={(node) => {
                sheetRefs.current[sheetIndex] = node;
              }}
              style={{
                width: `${BASE_W}px`,
                minHeight: `${BASE_H}px`,
                transform: `scale(${safeScale})`,
                transformOrigin: "top left",
              }}
            >
              {page?.blank ? (
                <div className="w-full h-full bg-white" />
              ) : (
                  <JobSheetTemplate
                    session={session}
                    pageIndex={pageIndex}
                    pageCount={pageCount}
                    template={template}
                    photos={sheetPhotos}
                    orderLocked={page?.photo_order_locked ?? false}
                    variant={pageVariant}
                    sectionLabel={sectionLabel}
                    photoLayout={page?.photo_layout}
                  />
              )}
            </div>
          </div>
        ))}
      </div>

      {items
        .slice()
        .sort((a, b) => (a.z ?? 0) - (b.z ?? 0))
        .map((item) => {
          const itemStyle: CSSProperties = {
            position: "absolute",
            left: `${item.x * safeScale}px`,
            top: `${item.y * safeScale}px`,
            width: `${item.w * safeScale}px`,
            height: `${item.h * safeScale}px`,
            zIndex: item.z ?? 0,
          };

          if (item.type === "text") {
            return (
              <div key={item.id} style={itemStyle}>
                <div
                  className="w-full h-full p-2 overflow-hidden"
                  style={{
                    whiteSpace: "pre-wrap",
                    fontSize: `${(item.style?.fontSize ?? 14) * safeScale}px`,
                    fontWeight: item.style?.bold ? 700 : 400,
                    fontStyle: item.style?.italic ? "italic" : "normal",
                    textDecoration: item.style?.underline ? "underline" : "none",
                    color: item.style?.color ?? "#111827",
                    textAlign: item.style?.align ?? "left",
                  }}
                >
                  {item.content ?? ""}
                </div>
              </div>
            );
          }

          if (item.type === "image") {
            return (
              <div key={item.id} style={itemStyle}>
                <img
                  src={item.src}
                  alt={item.name ?? "Image"}
                  className="w-full h-full object-contain bg-white"
                  loading="eager"
                />
              </div>
            );
          }

          return (
            <div key={item.id} style={itemStyle}>
              <div
                className="w-full h-full"
                style={{
                  background: item.style?.fill ?? "#ffffff",
                  borderStyle: "solid",
                  borderColor: item.style?.stroke ?? "#111827",
                  borderWidth: `${(item.style?.strokeWidth ?? 1) * safeScale}px`,
                }}
              />
            </div>
          );
        })}
    </div>
  );
}

function resolvePagePhotos(
  session: Session | null,
  page: Page | null | undefined,
  pageIndex: number,
): FileMeta[] {
  if (!session) return [];
  const uploads = session.uploads?.photos ?? [];
  const byId = new Map(uploads.map((photo) => [photo.id, photo]));
  const explicit = page?.photo_ids ?? [];
  if (explicit.length) {
    return explicit.map((id) => byId.get(id)).filter(Boolean) as FileMeta[];
  }
  return [];
}