Skip to content

Commit d6c26e8

Browse files
committed
preview image uses first file if not manually set
1 parent d003a4d commit d6c26e8

3 files changed

Lines changed: 58 additions & 29 deletions

File tree

src/backend/src/prisma/seed-data/parts.seed.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const basicPart = (projectId: string, userCreatedId: string, assigneeIds: string
1111
index: 1,
1212
commonName: 'Basic Part',
1313
description: 'Basic part with all fields populated',
14-
previewImageId: 'https://NER.com/basicpart.jpg',
1514
status: 'IN_PROGRESS',
1615
createdAt: new Date('2025-01-01T10:00:00Z'),
1716
project: {
@@ -32,7 +31,6 @@ const partWithoutDescription = (projectId: string, userCreatedId: string, assign
3231
data: {
3332
index: 2,
3433
commonName: 'Part without description',
35-
previewImageId: 'https://NER.com/partwithoutdes.jpg',
3634
status: 'IN_PROGRESS',
3735
createdAt: new Date('2025-01-01T10:00:00Z'),
3836
project: {
@@ -74,7 +72,6 @@ const partWithEmptyHistory = (projectId: string, userCreatedId: string, assignee
7472
index: 4,
7573
commonName: 'Part with empty history',
7674
description: 'Basic part but with empty history',
77-
previewImageId: 'https://NER.com/partemptyhistory.jpg',
7875
status: 'IN_PROGRESS',
7976
createdAt: new Date('2025-01-01T10:00:00Z'),
8077
project: {
@@ -95,7 +92,6 @@ const partWithLongName = (projectId: string, userCreatedId: string, assigneeIds:
9592
index: 5,
9693
commonName: 'ThisPartHasANameThatIsWayTooLongAndMightCauseProblemsWithVisibilityOnTheWebsiteMaybeIDK',
9794
description: 'part with super long name',
98-
previewImageId: 'https://NER.com/partwithlongname.jpg',
9995
status: 'IN_PROGRESS',
10096
createdAt: new Date('2025-01-01T10:00:00Z'),
10197
project: {
@@ -116,7 +112,6 @@ const partIndexNegative = (projectId: string, userCreatedId: string, assigneeIds
116112
index: -1,
117113
commonName: 'Part with negative index',
118114
description: 'This parts index is negative',
119-
previewImageId: 'https://NER.com/negativeindexpart.jpg',
120115
status: 'IN_PROGRESS',
121116
createdAt: new Date('2025-01-01T10:00:00Z'),
122117
project: {
@@ -137,7 +132,6 @@ const partIndexZero = (projectId: string, userCreatedId: string, assigneeIds: st
137132
index: 0,
138133
commonName: 'Part with index 0',
139134
description: 'This parts index is 0',
140-
previewImageId: 'https://NER.com/zeroindexpart.jpg',
141135
status: 'IN_PROGRESS',
142136
createdAt: new Date('2025-01-01T10:00:00Z'),
143137
project: {
@@ -158,7 +152,6 @@ const partIndexLarge = (projectId: string, userCreatedId: string, assigneeIds: s
158152
index: 99999999,
159153
commonName: 'Part with very large index',
160154
description: 'This part index is very large',
161-
previewImageId: 'https://NER.com/largeindexpart.jpg',
162155
status: 'IN_PROGRESS',
163156
createdAt: new Date('2025-01-01T10:00:00Z'),
164157
project: {
@@ -179,7 +172,6 @@ const partReadyForReview = (projectId: string, userCreatedId: string, assigneeId
179172
index: 9,
180173
commonName: 'Part with READY_FOR_REVIEW status',
181174
description: 'This part is ready for review',
182-
previewImageId: 'https://NER.com/testimage.jpg',
183175
status: 'READY_FOR_REVIEW',
184176
createdAt: new Date('2025-01-01T10:00:00Z'),
185177
project: {
@@ -200,7 +192,6 @@ const partInReview = (projectId: string, userCreatedId: string, assigneeIds: str
200192
index: 10,
201193
commonName: 'Part with IN_REVIEW status',
202194
description: 'This part is in review',
203-
previewImageId: 'https://NER.com/testimage.jpg',
204195
status: 'IN_REVIEW',
205196
createdAt: new Date('2025-01-01T10:00:00Z'),
206197
project: {
@@ -221,7 +212,6 @@ const partReviewed = (projectId: string, userCreatedId: string, assigneeIds: str
221212
index: 11,
222213
commonName: 'Part with REVIEWED status',
223214
description: 'This part is reviewed.',
224-
previewImageId: 'https://NER.com/testimage.jpg',
225215
status: 'REVIEWED',
226216
createdAt: new Date('2025-01-01T10:00:00Z'),
227217
project: {
@@ -242,7 +232,6 @@ const partApproved = (projectId: string, userCreatedId: string, assigneeIds: str
242232
index: 12,
243233
commonName: 'Part with APPROVED status',
244234
description: 'This part is approved.',
245-
previewImageId: 'https://NER.com/testimage.jpg',
246235
status: 'APPROVED',
247236
createdAt: new Date('2025-01-01T10:00:00Z'),
248237
project: {
@@ -263,7 +252,6 @@ const partCurrentDate = (projectId: string, userCreatedId: string, assigneeIds:
263252
index: 13,
264253
commonName: 'Part with current date',
265254
description: 'This part has the current date.',
266-
previewImageId: 'https://NER.com/testimage.jpg',
267255
status: 'APPROVED',
268256
createdAt: new Date(),
269257
project: {
@@ -284,7 +272,6 @@ const partPastDate = (projectId: string, userCreatedId: string, assigneeIds: str
284272
index: 14,
285273
commonName: 'Part with past date',
286274
description: 'This part is old.',
287-
previewImageId: 'https://NER.com/testimage.jpg',
288275
status: 'APPROVED',
289276
createdAt: new Date('2000-01-01T00:00:00Z'),
290277
project: {
@@ -305,7 +292,6 @@ const partUnixEpochDate = (projectId: string, userCreatedId: string, assigneeIds
305292
index: 15,
306293
commonName: 'Part with date of Unix Epoch',
307294
description: 'This part is was made at the unix epoch.',
308-
previewImageId: 'https://NER.com/testimage.jpg',
309295
status: 'APPROVED',
310296
createdAt: new Date('1970-01-01T00:00:00Z'),
311297
project: {
@@ -326,7 +312,6 @@ const partFutureDate = (projectId: string, userCreatedId: string, assigneeIds: s
326312
index: 16,
327313
commonName: 'Part with date of future',
328314
description: 'This part is was made in the future.',
329-
previewImageId: 'https://NER.com/testimage.jpg',
330315
status: 'APPROVED',
331316
createdAt: new Date('2100-12-31T23:59:59Z'),
332317
project: {
@@ -347,7 +332,6 @@ const partLeapYearDate = (projectId: string, userCreatedId: string, assigneeIds:
347332
index: 17,
348333
commonName: 'Part with date with a leap year',
349334
description: 'This part is was during a leap year.',
350-
previewImageId: 'https://NER.com/testimage.jpg',
351335
status: 'APPROVED',
352336
createdAt: new Date('2024-02-29T12:00:00Z'),
353337
project: {

src/backend/src/services/part-review.services.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,17 @@ export default class PartReviewService {
534534
...getPartSubmissionQueryArgs(organizationId)
535535
});
536536

537+
if (!part.previewImageId && fileIds.length > 0) {
538+
await prisma.part.update({
539+
where: {
540+
partId: part.partId
541+
},
542+
data: {
543+
previewImageId: fileIds[0]
544+
}
545+
});
546+
}
547+
537548
return partSubmissionTransformer(submission);
538549
}
539550

src/frontend/src/pages/ProjectDetailPage/ProjectViewContainer/PartReview/PartReviewComponents/PartPreviewCard.tsx

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { PartPreview } from 'shared';
33
import { grey } from '@mui/material/colors';
44
import { Card, CardContent, Typography, Box, Chip, Link } from '@mui/material';
55
import { useGetImageUrl } from '../../../../../hooks/onboarding.hook';
66
import { Link as RouterLink } from 'react-router-dom';
77
import DownloadButton from '../../../../../components/DownloadButton';
88
import { formatPartStatus, getStatusColor } from '../../../../../utils/part.utils';
9+
import { Document, Page, pdfjs } from 'react-pdf';
10+
import { useDownloadFile } from '../../../../../hooks/part-review.hooks';
911

1012
interface PartPreviewCardProps {
1113
partPreview: PartPreview;
1214
projectName: string;
1315
redirectUrl: string;
1416
}
1517

18+
pdfjs.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString();
19+
1620
export function PartPreviewCard({ partPreview, projectName, redirectUrl }: PartPreviewCardProps) {
1721
const { commonName, index, previewImageId, status, assignees, reviewRequests } = partPreview;
1822
const { data: previewUrl } = useGetImageUrl(previewImageId ?? null);
23+
const { data: pdf } = useDownloadFile(previewImageId ?? '');
24+
const [pdfLoadError, setPdfLoadError] = useState(false);
25+
26+
useEffect(() => {
27+
return () => {
28+
// Clean up worker when component unmounts
29+
if (pdfjs.GlobalWorkerOptions.workerPort) {
30+
pdfjs.GlobalWorkerOptions.workerPort.terminate();
31+
}
32+
};
33+
}, []);
1934

2035
return (
2136
<Link component={RouterLink} to={redirectUrl} sx={{ textDecoration: 'none', color: 'inherit' }}>
@@ -37,7 +52,7 @@ export function PartPreviewCard({ partPreview, projectName, redirectUrl }: PartP
3752
}}
3853
>
3954
<Box sx={{ px: 2, pt: 2, bgcolor: grey[800] }}>
40-
{previewImageId && previewUrl ? (
55+
{previewImageId && (previewUrl || pdf?.type === 'application/pdf') ? (
4156
<Box
4257
sx={{
4358
height: '15vw',
@@ -46,17 +61,36 @@ export function PartPreviewCard({ partPreview, projectName, redirectUrl }: PartP
4661
overflow: 'hidden'
4762
}}
4863
>
49-
<Box
50-
component="img"
51-
sx={{
52-
width: '100%',
53-
height: '100%',
54-
objectFit: 'cover',
55-
display: 'block'
56-
}}
57-
alt={`${commonName} Preview`}
58-
src={previewUrl}
59-
/>
64+
{pdf && !pdfLoadError ? (
65+
<Document
66+
file={pdf}
67+
onLoadError={() => {
68+
setPdfLoadError(true);
69+
}}
70+
>
71+
<Page
72+
pageNumber={1}
73+
width={300}
74+
renderTextLayer={false}
75+
renderAnnotationLayer={false}
76+
onLoadSuccess={() => {
77+
setPdfLoadError(false);
78+
}}
79+
/>
80+
</Document>
81+
) : (
82+
<Box
83+
component="img"
84+
sx={{
85+
width: '100%',
86+
height: '100%',
87+
objectFit: 'cover',
88+
display: 'block'
89+
}}
90+
alt={`${commonName} Preview`}
91+
src={previewUrl}
92+
/>
93+
)}
6094
</Box>
6195
) : (
6296
<Box

0 commit comments

Comments
 (0)