From fd887d9fe8e2f5c7594f190ff2d378ab34e4065c Mon Sep 17 00:00:00 2001 From: Kedrihan Date: Mon, 15 Jul 2024 18:50:37 +0200 Subject: [PATCH] affichage rues proches sms --- .../[soz]/soz-phone/src/client/cl_messages.ts | 18 +++++-- .../components/modal/MessageBubble.tsx | 48 ++++++++++++++++--- .../components/modal/MessageImageModal.tsx | 4 +- .../components/NotificationAlert.tsx | 35 ++++++++++++-- resources/[soz]/soz-phone/typings/messages.ts | 1 + 5 files changed, 89 insertions(+), 17 deletions(-) diff --git a/resources/[soz]/soz-phone/src/client/cl_messages.ts b/resources/[soz]/soz-phone/src/client/cl_messages.ts index 8fc0fbc011..0142f325b7 100644 --- a/resources/[soz]/soz-phone/src/client/cl_messages.ts +++ b/resources/[soz]/soz-phone/src/client/cl_messages.ts @@ -29,19 +29,29 @@ RegisterNuiCB(MessageEvents.DELETE_WAYPOINT, async (any, cb) => { }); RegisterNuiCB(MessageEvents.GET_POSITION, async (position: any, cb) => { - const [posX, posY] = GetEntityCoords(PlayerPedId(), true); - cb({ data: { x: posX, y: posY } }); + const [posX, posY, posZ] = GetEntityCoords(PlayerPedId(), true); + cb({ data: { x: posX, y: posY, z: posZ } }); }); RegisterNuiCB(MessageEvents.GET_DESTINATION, async (position: any, cb) => { - const [posX, posY] = GetBlipInfoIdCoord(GetFirstBlipInfoId(8)); - cb({ data: { x: posX, y: posY } }); + const [posX, posY, posZ] = GetBlipInfoIdCoord(GetFirstBlipInfoId(8)); + cb({ data: { x: posX, y: posY, z: posZ } }); }); RegisterNuiCB(SocietyEvents.SEND_CLIENT_POLICE_NOTIFICATION, async (message: any, cb) => { cb(exports['soz-core'].SendPoliceNotification(message)); }); +RegisterNuiCB(MessageEvents.GET_STREET_NAME, async (position: any, cb) => { + const [streetA, streetB] = GetStreetNameAtCoord(Number(position.x), Number(position.y), Number(position.z)); + var street = `${GetStreetNameFromHashKey(streetA)}`; + + if (streetB && streetA !== streetB) { + street += ` & ${GetStreetNameFromHashKey(streetB)}`; + } + cb({ data: street }); +}); + onNet(MessageEvents.SEND_MESSAGE_SUCCESS, (messageDto: PreDBMessage) => { sendMessageEvent(MessageEvents.SEND_MESSAGE_SUCCESS, messageDto); }); diff --git a/resources/[soz]/soz-phone/src/nui/apps/messages/components/modal/MessageBubble.tsx b/resources/[soz]/soz-phone/src/nui/apps/messages/components/modal/MessageBubble.tsx index 47e54ec787..0c72d95cd5 100644 --- a/resources/[soz]/soz-phone/src/nui/apps/messages/components/modal/MessageBubble.tsx +++ b/resources/[soz]/soz-phone/src/nui/apps/messages/components/modal/MessageBubble.tsx @@ -15,14 +15,28 @@ import { setClipboard } from '../../../../os/phone/hooks/useClipboard'; import Emoji from '../../../../ui/components/Emoji'; import { Button } from '../../../../ui/old_components/Button'; -const isImage = url => { +const isImage = (url) => { return /(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|png|jpeg|gif)/g.test(url); }; -const isPosition = url => { +const isOldPosition = (url) => { return /vec2\((-?[0-9.]+),(-?[0-9.]+)\)/g.test(url); }; +const isPosition = (url) => { + return /vec3\((-?[0-9.]+),(-?[0-9.]+),(-?[0-9.]+)\)/g.test(url); +}; + +const getAddress = async (input: string) => { + const position = /vec3\((-?[0-9.]+),(-?[0-9.]+),(-?[0-9.]+)\)/g.exec(input); + const street_name = await fetchNui(MessageEvents.GET_STREET_NAME, { + x: position[1], + y: position[2], + z: position[3], + }); + return street_name.data; +}; + interface MessageBubbleProps { message: Message; } @@ -30,15 +44,30 @@ interface MessageBubbleProps { export const MessageBubble: React.FC = ({ message }) => { const config = useConfig(); const myNumber = usePhoneNumber(); - + const [address, setAddress] = React.useState(''); const setWaypoint = () => { - const position = /vec2\((-?[0-9.]+),(-?[0-9.]+)\)/g.exec(message.message); + const position = /vec3\((-?[0-9.]+),(-?[0-9.]+),(-?[0-9.]+)\)/g.exec(message.message); + const oldPosition = /vec2\((-?[0-9.]+),(-?[0-9.]+)\)/g.exec(message.message); fetchNui>(MessageEvents.SET_WAYPOINT, { - x: position[1], - y: position[2], + x: position ? position[1] : oldPosition[1], + y: position ? position[2] : oldPosition[2], }); }; + React.useEffect(() => { + const getAddressAsync = async () => { + try { + const address = await getAddress(message.message); + setAddress(address); + } catch (error) { + console.error(error); + setAddress('Destination'); + } + }; + if (isPosition(message.message)) { + getAddressAsync(); + } + }, [message.message]); const isMine = message.author === myNumber; @@ -63,11 +92,16 @@ export const MessageBubble: React.FC = ({ message }) => { )} {isPosition(message.message) && ( + + {address} + + )} + {isOldPosition(message.message) && ( Destination )} - {!isImage(message.message) && !isPosition(message.message) && ( + {!isImage(message.message) && !isPosition(message.message) && !isOldPosition(message.message) && (

>(MessageEvents.GET_POSITION, {}).then(resp => { sendMessage({ conversationId: messageGroupId, - message: `vec2(${resp.data.x},${resp.data.y})`, + message: `vec3(${resp.data.x},${resp.data.y},${resp.data.z})`, }); onClose(); }); @@ -78,7 +78,7 @@ export const MessageImageModal = ({ isOpen, messageGroupId, onClose, image }: IP if (resp.data.x !== 0 && resp.data.y !== 0) { sendMessage({ conversationId: messageGroupId, - message: `vec2(${resp.data.x},${resp.data.y})`, + message: `vec3(${resp.data.x},${resp.data.y},${resp.data.z})`, }); onClose(); } else { diff --git a/resources/[soz]/soz-phone/src/nui/os/notifications/components/NotificationAlert.tsx b/resources/[soz]/soz-phone/src/nui/os/notifications/components/NotificationAlert.tsx index affd4dc299..a9e3237594 100644 --- a/resources/[soz]/soz-phone/src/nui/os/notifications/components/NotificationAlert.tsx +++ b/resources/[soz]/soz-phone/src/nui/os/notifications/components/NotificationAlert.tsx @@ -4,13 +4,40 @@ import React from 'react'; import { useEmergency } from '../../../../nui/hooks/useEmergency'; import { useNotifications } from '../hooks/useNotifications'; +import { MessageEvents } from '@typings/messages'; +import { fetchNui } from '@utils/fetchNui'; + +const getAddress = async (input: string) => { + const position = /vec3\((-?[0-9.]+),(-?[0-9.]+),(-?[0-9.]+)\)/g.exec(input); + const street_name = await fetchNui(MessageEvents.GET_STREET_NAME, { + x: position[1], + y: position[2], + z: position[3], + }); + return street_name.data; +}; export const NotificationAlert = () => { const { currentAlert } = useNotifications(); const emergency = useEmergency(); - + const [address, setAddress] = React.useState(''); // TODO: improve notification hook - const isPosition = /vec2\((-?[\d.]+),(-?[\d.]+)\)/g.test(currentAlert?.content.toString()); + const isOldPosition = /vec2\((-?[\d.]+),(-?[\d.]+)\)/g.test(currentAlert?.content.toString()); + const isPosition = /vec3\((-?[\d.]+),(-?[\d.]+),(-?[\d.]+)\)/g.test(currentAlert?.content.toString()); + React.useEffect(() => { + const getAddressAsync = async () => { + try { + const address = await getAddress(currentAlert?.content.toString()); + setAddress(address); + } catch (error) { + console.error(error); + setAddress('Destination'); + } + }; + if (isPosition) { + getAddressAsync(); + } + }, [currentAlert?.content.toString()]); if (!currentAlert || emergency) { return null; @@ -28,8 +55,8 @@ export const NotificationAlert = () => { leaveFrom="translate-y-0" leaveTo="-translate-y-full" > - currentAlert?.onClickAlert(e)} icon={currentAlert?.notificationIcon || undefined}> - {isPosition ? 'Destination' : currentAlert?.content} + currentAlert?.onClickAlert(e)} icon={currentAlert?.notificationIcon || undefined}> + {isPosition ? address : isOldPosition ? 'Destination' : currentAlert?.content} ); diff --git a/resources/[soz]/soz-phone/typings/messages.ts b/resources/[soz]/soz-phone/typings/messages.ts index 9f7174e6ee..b4ad27c2f9 100644 --- a/resources/[soz]/soz-phone/typings/messages.ts +++ b/resources/[soz]/soz-phone/typings/messages.ts @@ -102,4 +102,5 @@ export enum MessageEvents { SET_WAYPOINT = 'phone:setWaypoint', DELETE_WAYPOINT = 'phone:deleteWaypoint', SET_CONVERSATION_ARCHIVED = 'phone:setConversationArchived', + GET_STREET_NAME = 'phone:getStreetName', }