Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Brief] Ajout de la vue globale des zones sélectionnées #2061

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions frontend/src/features/Dashboard/components/Pdf/v1/Brief.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/* eslint-disable import/no-absolute-path */

// TODO (04/11/2024) : use monitor-ui fonts instead of imported/duplicated ones

import { Document, Page, View } from '@react-pdf/renderer'

import { Amps } from './Amps'
Expand Down Expand Up @@ -37,25 +33,50 @@ export function Brief({ author, brief, description, title }: BriefProps) {
<Comments comments={brief.comments} />
</View>
<View style={layoutStyle.section}>
<Reportings reportings={brief.reportings} subThemes={brief.subThemes} themes={brief.themes} />
</View>
<View style={layoutStyle.section}>
<AreaTable amps={brief.amps} regulatoryAreas={brief.regulatoryAreas} vigilanceAreas={brief.vigilanceAreas} />
</View>
<View style={layoutStyle.section}>
<RegulatoryAreas regulatoryAreas={brief.regulatoryAreas} />
</View>
<View style={layoutStyle.section}>
<Amps amps={brief.amps} />
</View>
<View style={layoutStyle.section}>
<VigilanceAreas
linkedAMPs={brief.allLinkedAMPs}
linkedRegulatoryAreas={brief.allLinkedRegulatoryAreas}
<AreaTable
amps={brief.amps}
image={brief.images?.find(image => image.featureId === 'WHOLE_DASHBOARD')}
regulatoryAreas={brief.regulatoryAreas}
vigilanceAreas={brief.vigilanceAreas}
/>
</View>
</Page>
{brief.regulatoryAreas.length > 0 && (
<Page style={layoutStyle.page}>
<Headings name={brief.name} />
<View style={layoutStyle.section}>
<RegulatoryAreas regulatoryAreas={brief.regulatoryAreas} />
</View>
</Page>
)}
{brief.amps.length > 0 && (
<Page style={layoutStyle.page}>
<Headings name={brief.name} />
<View style={layoutStyle.section}>
<Amps amps={brief.amps} />
</View>
</Page>
)}
{brief.vigilanceAreas.length > 0 && (
<Page style={layoutStyle.page}>
<Headings name={brief.name} />
<View style={layoutStyle.section}>
<VigilanceAreas
linkedAMPs={brief.allLinkedAMPs}
linkedRegulatoryAreas={brief.allLinkedRegulatoryAreas}
vigilanceAreas={brief.vigilanceAreas}
/>
</View>
</Page>
)}
{brief.reportings.length > 0 && (
<Page style={layoutStyle.page}>
<Headings name={brief.name} />
<View style={layoutStyle.section}>
<Reportings reportings={brief.reportings} subThemes={brief.subThemes} themes={brief.themes} />
</View>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Je ne suis pas sur que dans cette version il faille mettre les signalements a la fin

</Page>
)}
</Document>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getControlUnitsByIds } from '@api/controlUnitsAPI'
import { getRegulatoryAreasByIds } from '@api/regulatoryLayersAPI'
import { useGetReportingsByIdsQuery } from '@api/reportingsAPI'
import { getVigilanceAreasByIds } from '@api/vigilanceAreasAPI'
import { useExportImages } from '@features/Dashboard/hooks/useExportImages'
import { useAppSelector } from '@hooks/useAppSelector'
import { useGetControlPlans } from '@hooks/useGetControlPlans'
import { Button, Icon } from '@mtes-mct/monitor-ui'
Expand Down Expand Up @@ -45,14 +46,16 @@ export function GeneratePdfButton({ dashboard }: GeneratePdfButtonProps) {

const allLinkedAMPs = useAppSelector(state => getAmpsByIds(state, allLinkedAMPIds))

const { images, loading } = useExportImages({ triggerExport: shouldTriggerExport })

const brief: Dashboard.Brief = useMemo(
() => ({
allLinkedAMPs,
allLinkedRegulatoryAreas,
amps,
comments: dashboard.comments,
controlUnits,
images: [],
images,
name: dashboard.name,
regulatoryAreas,
reportings: Object.values(reportings?.entities ?? []),
Expand All @@ -69,6 +72,7 @@ export function GeneratePdfButton({ dashboard }: GeneratePdfButtonProps) {
dashboard.name,
dashboard.updatedAt,
controlUnits,
images,
regulatoryAreas,
reportings?.entities,
subThemes,
Expand All @@ -83,7 +87,7 @@ export function GeneratePdfButton({ dashboard }: GeneratePdfButtonProps) {

useEffect(() => {
const renderPdf = async () => {
if (shouldTriggerExport) {
if (brief.images && !loading && shouldTriggerExport) {
setIsOpening(true)

const blob = await renderPDFV1({ brief })
Expand All @@ -102,7 +106,7 @@ export function GeneratePdfButton({ dashboard }: GeneratePdfButtonProps) {
}
}
renderPdf()
}, [brief, dashboard.name, shouldTriggerExport])
}, [brief, dashboard.name, loading, shouldTriggerExport])

const getLoadingText = () => {
if (isOpening) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { THEME } from '@mtes-mct/monitor-ui'
import { StyleSheet, Text, View } from '@react-pdf/renderer'
import { StyleSheet, Text, View, Image } from '@react-pdf/renderer'

import { layoutStyle } from '../style'

import type { ExportImageType } from '@features/Dashboard/hooks/useExportImages'
import type { VigilanceArea } from '@features/VigilanceArea/types'
import type { AMPFromAPI } from 'domain/entities/AMPs'
import type { RegulatoryLayerWithMetadata } from 'domain/entities/regulatory'
Expand Down Expand Up @@ -45,10 +46,12 @@ const styles = StyleSheet.create({

export function AreaTable({
amps,
image,
regulatoryAreas,
vigilanceAreas
}: {
amps: AMPFromAPI[]
image: ExportImageType | undefined
regulatoryAreas: RegulatoryLayerWithMetadata[]
vigilanceAreas: VigilanceArea.VigilanceArea[]
}) {
Expand Down Expand Up @@ -100,6 +103,7 @@ export function AreaTable({
</View>
</View>
</View>
{image && <Image src={image.image} style={{ marginTop: 4.3 }} />}
</>
)
}
51 changes: 30 additions & 21 deletions frontend/src/features/Dashboard/hooks/useExportImages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type ExportLayerProps = {
}

export function useExportImages({ triggerExport }: ExportLayerProps) {
const isBriefWithImagesEnabled = import.meta.env.FRONTEND_DASHBOARD_BRIEF_IMAGES_ENABLED === 'true'

const [images, setImages] = useState<ExportImageType[]>()
const [loading, setLoading] = useState(false)
const mapRef = useRef(null) as MutableRefObject<OpenLayerMap | null>
Expand Down Expand Up @@ -169,29 +171,35 @@ export function useExportImages({ triggerExport }: ExportLayerProps) {
return allImages
}

// eslint-disable-next-line no-restricted-syntax
for (const feature of features) {
mapContext.clearRect(0, 0, mapCanvas.width, mapCanvas.height)
layersVectorSourceRef.current.clear()
layersVectorSourceRef.current.addFeature(feature)

if (isBriefWithImagesEnabled) {
// eslint-disable-next-line no-restricted-syntax
for (const feature of features) {
mapContext.clearRect(0, 0, mapCanvas.width, mapCanvas.height)
layersVectorSourceRef.current.clear()
layersVectorSourceRef.current.addFeature(feature)

dashboardFeature.setStyle([measurementStyle, measurementStyleWithCenter])
layersVectorSourceRef.current.addFeature(dashboardFeature)

// eslint-disable-next-line no-await-in-loop
await zoomToFeatures([dashboardFeature, feature])

mapRef.current
.getViewport()
.querySelectorAll('canvas')
.forEach(canvas => {
mapContext.drawImage(canvas, 0, 0)
allImages.push({
featureId: feature.getId(),
image: mapCanvas.toDataURL('image/png')
})
})
}
} else {
dashboardFeature.setStyle([measurementStyle, measurementStyleWithCenter])
layersVectorSourceRef.current.addFeature(dashboardFeature)

// eslint-disable-next-line no-await-in-loop
await zoomToFeatures([dashboardFeature, feature])

mapRef.current
.getViewport()
.querySelectorAll('canvas')
.forEach(canvas => {
mapContext.drawImage(canvas, 0, 0)
allImages.push({
featureId: feature.getId(),
image: mapCanvas.toDataURL('image/png')
})
})
}

extractReportingFeatures(features)

layersVectorSourceRef.current.clear(true)
Expand All @@ -213,7 +221,7 @@ export function useExportImages({ triggerExport }: ExportLayerProps) {

return allImages
},
[extractReportingFeatures]
[extractReportingFeatures, isBriefWithImagesEnabled]
)

useEffect(() => {
Expand Down Expand Up @@ -242,6 +250,7 @@ export function useExportImages({ triggerExport }: ExportLayerProps) {
const generateImages = async () => {
setLoading(true)
const allImages = await exportImages(features, dashboardFeature)

setImages(allImages)
setLoading(false)
}
Expand Down
Loading