-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: London Studios SmartSigne integration (#1760)
- Loading branch information
Showing
19 changed files
with
245 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/client/src/components/dispatch/map/smart-signs/render-map-smart-signs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SmartSignsMarker } from "./smart-sign-marker"; | ||
import { useSmartSigns } from "./use-smart-signs"; | ||
|
||
export function RenderMapSmartSigns() { | ||
const smartSigns = useSmartSigns(); | ||
return smartSigns.map((marker) => <SmartSignsMarker key={marker.id} marker={marker} />); | ||
} |
126 changes: 126 additions & 0 deletions
126
apps/client/src/components/dispatch/map/smart-signs/smart-sign-marker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import * as React from "react"; | ||
import { convertToMap } from "lib/map/utils"; | ||
import { Marker, Popup, Tooltip, useMap } from "react-leaflet"; | ||
import type { SmartSignMarker } from "types/map"; | ||
import { icon as leafletIcon } from "leaflet"; | ||
import { useTranslations } from "next-intl"; | ||
import { MapItem, useDispatchMapState, useSocketStore } from "state/mapState"; | ||
import { generateMarkerTypes } from "../render-map-blips"; | ||
import { Button, Input } from "@snailycad/ui"; | ||
import { Permissions, usePermission } from "hooks/usePermission"; | ||
import { toastMessage } from "lib/toastMessage"; | ||
|
||
interface Props { | ||
marker: SmartSignMarker; | ||
} | ||
|
||
export function SmartSignsMarker({ marker }: Props) { | ||
const map = useMap(); | ||
const socket = useSocketStore((state) => state.socket); | ||
const [markerText, setMarkerText] = React.useState< | ||
SmartSignMarker["defaultText"] & { editing?: boolean } | ||
>(marker.defaultText); | ||
|
||
const t = useTranslations("Leo"); | ||
const hiddenItems = useDispatchMapState((state) => state.hiddenItems); | ||
const markerTypes = React.useMemo(generateMarkerTypes, []); | ||
|
||
const { hasPermissions } = usePermission(); | ||
const hasManageSmartSignsPermissions = hasPermissions([Permissions.ManageSmartSigns]); | ||
|
||
const pos = React.useMemo(() => convertToMap(marker.x, marker.y, map), [marker.x, marker.y]); // eslint-disable-line react-hooks/exhaustive-deps | ||
const playerIcon = React.useMemo(() => { | ||
// eslint-disable-next-line prefer-destructuring | ||
const icon = markerTypes[780]; | ||
|
||
if (icon) { | ||
return leafletIcon(icon); | ||
} | ||
|
||
return undefined; | ||
}, [markerTypes]); | ||
|
||
React.useEffect(() => { | ||
if (!markerText.editing) setMarkerText(marker.defaultText); | ||
}, [marker.defaultText, markerText.editing]); | ||
|
||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) { | ||
event.preventDefault(); | ||
if (!socket?.connected) return; | ||
|
||
socket.emit("sna-live-map:update-smart-sign", { | ||
...marker, | ||
defaultText: { | ||
firstLine: markerText.firstLine.toLowerCase() || null, | ||
secondLine: markerText.secondLine.toLowerCase() || null, | ||
thirdLine: markerText.thirdLine.toLowerCase() || null, | ||
}, | ||
}); | ||
|
||
toastMessage({ | ||
icon: "success", | ||
title: t("smartSignUpdated"), | ||
message: t("smartSignUpdatedMessage"), | ||
}); | ||
|
||
setTimeout(() => { | ||
setMarkerText({ | ||
...markerText, | ||
editing: false, | ||
}); | ||
}, 500); // clear editing state after 500ms | ||
} | ||
|
||
if (hiddenItems[MapItem.SMART_SIGNS]) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Marker icon={playerIcon} key={marker.id} position={pos}> | ||
<Tooltip direction="top">SmartSign {marker.id}</Tooltip> | ||
|
||
<Popup minWidth={300}> | ||
<p style={{ margin: 2 }}> | ||
<strong>SmartSign {marker.id}</strong> | ||
</p> | ||
|
||
<form onSubmit={handleSubmit} className="mt-3"> | ||
<Input | ||
name="firstLine" | ||
readOnly={!hasManageSmartSignsPermissions} | ||
className="rounded-b-none !text-amber-600 font-bold text-[15px] uppercase" | ||
value={markerText.firstLine.toUpperCase()} | ||
onChange={(event) => | ||
setMarkerText({ ...markerText, editing: true, firstLine: event.target.value }) | ||
} | ||
maxLength={15} | ||
/> | ||
<Input | ||
name="secondLine" | ||
readOnly={!hasManageSmartSignsPermissions} | ||
className="rounded-none -my-1 !text-amber-600 font-bold text-[15px] uppercase" | ||
value={markerText.secondLine.toUpperCase()} | ||
onChange={(event) => | ||
setMarkerText({ ...markerText, editing: true, secondLine: event.target.value }) | ||
} | ||
maxLength={15} | ||
/> | ||
<Input | ||
name="thirdLine" | ||
readOnly={!hasManageSmartSignsPermissions} | ||
className="rounded-t-none !text-amber-600 font-bold text-[15px] uppercase" | ||
value={markerText.thirdLine.toUpperCase()} | ||
onChange={(event) => | ||
setMarkerText({ ...markerText, editing: true, thirdLine: event.target.value }) | ||
} | ||
maxLength={15} | ||
/> | ||
|
||
<Button className="mt-2" type="submit" disabled={!hasManageSmartSignsPermissions}> | ||
Save | ||
</Button> | ||
</form> | ||
</Popup> | ||
</Marker> | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/client/src/components/dispatch/map/smart-signs/use-smart-signs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as React from "react"; | ||
import { useDispatchMapState, useSocketStore } from "state/mapState"; | ||
import { SmartSignMarker } from "types/map"; | ||
|
||
export function useSmartSigns() { | ||
const socket = useSocketStore((state) => state.socket); | ||
const { smartSigns, setSmartSigns } = useDispatchMapState((state) => ({ | ||
smartSigns: state.smartSigns, | ||
setSmartSigns: state.setSmartSigns, | ||
})); | ||
|
||
const onInitialize = React.useCallback((data: { smartSigns: SmartSignMarker[] }) => { | ||
setSmartSigns(data.smartSigns); | ||
}, []); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
React.useEffect(() => { | ||
const s = socket; | ||
|
||
if (s) { | ||
s.on("sna-live-map:smart-signs", onInitialize); | ||
} | ||
|
||
return () => { | ||
s?.off("sna-live-map:smart-signs", onInitialize); | ||
}; | ||
}, [socket, onInitialize]); | ||
|
||
return smartSigns; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.