mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: refactor and fix typing
This commit is contained in:
parent
aee19e8b6c
commit
bc9e35977e
@ -33,9 +33,6 @@ import { useInfinitedLiveMessages } from "../hooks/useInfiniteLiveMessages";
|
|||||||
|
|
||||||
import { ChatActions } from "./ChatActions";
|
import { ChatActions } from "./ChatActions";
|
||||||
import { ChatHeader } from "./ChatHeader";
|
import { ChatHeader } from "./ChatHeader";
|
||||||
import GeolocationMessage, {
|
|
||||||
GeolocationMessageProps,
|
|
||||||
} from "./GeolocationMessage";
|
|
||||||
|
|
||||||
export function Chat() {
|
export function Chat() {
|
||||||
const { apiUrl } = useConfig();
|
const { apiUrl } = useConfig();
|
||||||
@ -94,33 +91,13 @@ export function Chat() {
|
|||||||
autoScrollToBottom={true}
|
autoScrollToBottom={true}
|
||||||
autoScrollToBottomOnMount={true}
|
autoScrollToBottomOnMount={true}
|
||||||
>
|
>
|
||||||
{messages.map((message: any, i) => {
|
{messages.map((message, i) => {
|
||||||
const position = getMessagePosition(
|
const position = getMessagePosition(
|
||||||
message,
|
message,
|
||||||
messages[i - 1],
|
messages[i - 1],
|
||||||
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 <GeolocationMessage message={msg} key={message.id} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Message
|
<Message
|
||||||
key={message.id}
|
key={message.id}
|
||||||
|
|||||||
@ -6,85 +6,43 @@
|
|||||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Direction } from "hexabot-chat-widget/src/types/message.types";
|
import { useRef } from "react";
|
||||||
import { useEffect, useRef, useState } from "react";
|
|
||||||
|
|
||||||
export interface GeolocationMessageProps {
|
export interface GeolocationMessageProps {
|
||||||
message: {
|
message: {
|
||||||
type: string;
|
type: "location";
|
||||||
data: {
|
|
||||||
coordinates: {
|
coordinates: {
|
||||||
lat: number;
|
lat: number;
|
||||||
lng: number;
|
lon: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
author: string;
|
|
||||||
read: boolean;
|
|
||||||
mid: string;
|
|
||||||
createdAt: string | Date;
|
|
||||||
direction: Direction;
|
|
||||||
delivery: boolean;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const GeolocationMessage: React.FC<GeolocationMessageProps> = ({ message }) => {
|
const GeolocationMessage: React.FC<GeolocationMessageProps> = ({ 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<HTMLDivElement>(null);
|
const iframeRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
if (!("coordinates" in message)) {
|
||||||
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)) {
|
|
||||||
throw new Error("Unable to find coordinates");
|
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=${
|
const openStreetMapUrl = `https://www.openstreetmap.org/export/embed.html?bbox=${
|
||||||
coordinates.lng - 0.1
|
coordinates.lng - 0.1
|
||||||
},${coordinates.lat - 0.1},${coordinates.lng + 0.1},${
|
},${coordinates.lat - 0.1},${coordinates.lng + 0.1},${
|
||||||
coordinates.lat + 0.1
|
coordinates.lat + 0.1
|
||||||
}&layer=mapnik&marker=${coordinates.lat},${coordinates.lng}`;
|
}&layer=mapnik&marker=${coordinates.lat},${coordinates.lng}`;
|
||||||
const colors = allColors[message.direction || Direction.received];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
borderRadius: "0.5rem",
|
borderRadius: "0.5rem",
|
||||||
backgroundColor: colors.bg,
|
|
||||||
color: colors.text,
|
|
||||||
width: "200px",
|
width: "200px",
|
||||||
}}
|
}}
|
||||||
ref={iframeRef}
|
ref={iframeRef}
|
||||||
>
|
>
|
||||||
{isSeen && (
|
|
||||||
<iframe
|
<iframe
|
||||||
style={{
|
style={{
|
||||||
width: "200px",
|
width: "200px",
|
||||||
@ -98,7 +56,6 @@ const GeolocationMessage: React.FC<GeolocationMessageProps> = ({ message }) => {
|
|||||||
marginWidth={0}
|
marginWidth={0}
|
||||||
src={openStreetMapUrl}
|
src={openStreetMapUrl}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import { Message, MessageModel } from "@chatscope/chat-ui-kit-react";
|
import { Message, MessageModel } from "@chatscope/chat-ui-kit-react";
|
||||||
import MenuRoundedIcon from "@mui/icons-material/MenuRounded";
|
import MenuRoundedIcon from "@mui/icons-material/MenuRounded";
|
||||||
import ReplyIcon from "@mui/icons-material/Reply";
|
import ReplyIcon from "@mui/icons-material/Reply";
|
||||||
@ -20,6 +19,7 @@ import { buildURL } from "@/utils/URL";
|
|||||||
|
|
||||||
import { MessageAttachmentsViewer } from "../components/AttachmentViewer";
|
import { MessageAttachmentsViewer } from "../components/AttachmentViewer";
|
||||||
import { Carousel } from "../components/Carousel";
|
import { Carousel } from "../components/Carousel";
|
||||||
|
import GeolocationMessage from "../components/GeolocationMessage";
|
||||||
|
|
||||||
function hasSameSender(
|
function hasSameSender(
|
||||||
m1: IMessage | IMessageFull,
|
m1: IMessage | IMessageFull,
|
||||||
@ -69,6 +69,14 @@ export function getMessageContent(
|
|||||||
const message = messageEntity.message;
|
const message = messageEntity.message;
|
||||||
let content: ReactNode[] = [];
|
let content: ReactNode[] = [];
|
||||||
|
|
||||||
|
if ("coordinates" in message) {
|
||||||
|
content.push(
|
||||||
|
<Message.CustomContent>
|
||||||
|
<GeolocationMessage message={message} key={message.type} />
|
||||||
|
</Message.CustomContent>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if ("text" in message) {
|
if ("text" in message) {
|
||||||
content.push(
|
content.push(
|
||||||
<Message.TextContent key={messageEntity.id} text={message.text} />,
|
<Message.TextContent key={messageEntity.id} text={message.text} />,
|
||||||
@ -85,6 +93,7 @@ export function getMessageContent(
|
|||||||
chips = message.quickReplies as { title: string }[];
|
chips = message.quickReplies as { title: string }[];
|
||||||
chipsIcon = <ReplyIcon color="disabled" />;
|
chipsIcon = <ReplyIcon color="disabled" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chips.length > 0)
|
if (chips.length > 0)
|
||||||
content.push(
|
content.push(
|
||||||
<Message.Footer style={{ marginTop: "5px" }}>
|
<Message.Footer style={{ marginTop: "5px" }}>
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import { EntityType } from "@/services/types";
|
import { EntityType } from "@/services/types";
|
||||||
|
|
||||||
import { IAttachment } from "./attachment.types";
|
import { IAttachment } from "./attachment.types";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user