From bc9e35977e5defe16397f3fb4e43090f708ceaf4 Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Thu, 6 Feb 2025 17:47:15 +0100 Subject: [PATCH] fix: refactor and fix typing --- .../src/components/inbox/components/Chat.tsx | 25 +---- .../inbox/components/GeolocationMessage.tsx | 91 +++++-------------- .../components/inbox/helpers/mapMessages.tsx | 11 ++- frontend/src/types/message.types.ts | 1 - 4 files changed, 35 insertions(+), 93 deletions(-) diff --git a/frontend/src/components/inbox/components/Chat.tsx b/frontend/src/components/inbox/components/Chat.tsx index 3d678824..edcf7611 100644 --- a/frontend/src/components/inbox/components/Chat.tsx +++ b/frontend/src/components/inbox/components/Chat.tsx @@ -33,9 +33,6 @@ import { useInfinitedLiveMessages } from "../hooks/useInfiniteLiveMessages"; import { ChatActions } from "./ChatActions"; import { ChatHeader } from "./ChatHeader"; -import GeolocationMessage, { - GeolocationMessageProps, -} from "./GeolocationMessage"; export function Chat() { const { apiUrl } = useConfig(); @@ -94,33 +91,13 @@ export function Chat() { autoScrollToBottom={true} autoScrollToBottomOnMount={true} > - {messages.map((message: any, i) => { + {messages.map((message, i) => { const position = getMessagePosition( message, messages[i - 1], messages[i + 1], ); - if (message?.message?.type === "location") { - const msg = { - type: message.message.type, - data: { - coordinates: { - lat: message.message.coordinates.lat, - lng: message.message.coordinates.lon, - }, - }, - author: message.sender, - read: message.read, - mid: message.mid, - createdAt: message.createdAt, - direction: "sent", - delivery: false, - } as GeolocationMessageProps["message"]; - - return ; - } - return ( = ({ message }) => { - // const { colors: allColors } = useColors(); - // const widget = useWidget(); - const allColors = { - header: { bg: "#1BA089", text: "#fff" }, - launcher: { bg: "#1BA089" }, - messageList: { bg: "#fff" }, - sent: { bg: "#1BA089", text: "#fff" }, - received: { bg: "#f6f8f9", text: "#000" }, - userInput: { bg: "#fff", text: "#000" }, - button: { bg: "#ffffff", text: "#1BA089", border: "#1BA089" }, - messageStatus: { bg: "#1BA089" }, - messageTime: { text: "#9C9C9C" }, - }; - const [isSeen, setIsSeen] = useState(false); const iframeRef = useRef(null); - useEffect(() => { - const observer = new IntersectionObserver((entries) => { - if (!isSeen && entries[0].intersectionRatio > 0) { - setIsSeen(true); - } - }); - - if (iframeRef.current) { - observer.observe(iframeRef.current); - } - - return () => { - if (iframeRef.current) { - // eslint-disable-next-line react-hooks/exhaustive-deps - observer.unobserve(iframeRef.current); - } - }; - }, [isSeen]); - - if (!("coordinates" in message.data)) { + if (!("coordinates" in message)) { throw new Error("Unable to find coordinates"); } - const coordinates = message.data?.coordinates || { lat: 0.0, lng: 0.0 }; + + const coordinates = { + lat: message?.coordinates?.lat || 0.0, + lng: message?.coordinates?.lon || 0.0, + }; const openStreetMapUrl = `https://www.openstreetmap.org/export/embed.html?bbox=${ coordinates.lng - 0.1 },${coordinates.lat - 0.1},${coordinates.lng + 0.1},${ coordinates.lat + 0.1 }&layer=mapnik&marker=${coordinates.lat},${coordinates.lng}`; - const colors = allColors[message.direction || Direction.received]; return (
- {isSeen && ( -