import React from "react" import { IGatsbyImageData } from "gatsby-plugin-image" import Map from "../core/Map" import News from "../News" import Stats from "../Stats" import Hero from "../Hero" import Image from "../core/Image" import Data from "../Data" import Maps from "../Maps" import Events from "../Events" import Iframe from "../core/Iframe" import Text from "../Text" import Partners from "../Partners" interface IAction { actions_id: { name: string url: string style: string } } interface IService { services_id: { name: string url: string description: string image: { imageFile?: { publicURL?: string } } } } interface Iimage { directus_files_id: { imageFile: IGatsbyImageData } } interface IComponent { type: string actions: IAction[] | [] mapdid: string title: string subtitle: string content: string url_dataviz: string url_map: string layout: string length: number size: string services: IService[] | [] images: Iimage[] | [] } const getActions = (actionsProps: IAction[] | []) => { if (!actionsProps.length) return undefined return actionsProps.map((action_id) => { let action = action_id.actions_id return { ...action, classname: action.style, } }) } const getServices = (servicesProps: IService[] | []) => { if (!servicesProps.length) return undefined return servicesProps.map((services_id) => { let service = services_id.services_id return { ...service, image: service.image?.imageFile?.publicURL ?? "", } }) } const getImages = (imagesProps: Iimage[] | []) => { if (!imagesProps.length) return undefined return imagesProps.map((images) => { return { src: images.directus_files_id.imageFile } }) } const Component = (props: IComponent) => { const { type, actions: actionsProps = [], services: servicesProps = [], images: imagesProps = [], mapdid, length, url_dataviz, url_map, ...restProps } = props const actions = getActions(actionsProps) const services = getServices(servicesProps) const images = getImages(imagesProps) switch (type) { case "news": let newsaction = actions?.length ? actions[0] : undefined return <News {...restProps} action={newsaction} /> case "maps": return <Maps limit={length} {...restProps} /> case "map": if (!mapdid) return <div>Map</div> return ( <div className="h-full min-h-[500px] w-full"> <Map {...restProps} mapid={parseInt(mapdid)} /> </div> ) case "data": return <Data {...restProps} /> case "stats": return <Stats {...restProps} /> case "hero": let heroaction = actions?.length ? actions[0] : { name: "", url: "" } return ( <Hero services={services!} {...restProps} action={heroaction!} size="base" bgImage={images} /> ) case "text": let textAction = actions?.length ? actions[0] : undefined return <Text {...restProps} action={textAction} /> case "image": let img = images?.length ? images[0] : undefined return <Image image={img} /> case "mapgl": return <div>Carte Maplibre</div> case "geoportal": return <Iframe {...restProps} url={url_map} /> case "dataviz": return <Iframe {...restProps} url={url_dataviz} /> case "events": return <Events {...restProps} /> case "partners": return <Partners /> } return <div></div> } export default Component