From 06b3bd32a913da58c4a97a0cb570d4cf0692ceee Mon Sep 17 00:00:00 2001 From: Anton Arnautov Date: Fri, 20 Dec 2024 00:03:31 +0100 Subject: [PATCH] Initial commit --- examples/vite/src/App.tsx | 81 ++++++++++++++++++- src/components/Attachment/Attachment.tsx | 1 + .../hooks/useLiveLocationSharingManager.ts | 35 ++++++++ src/components/Attachment/index.ts | 1 + 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/components/Attachment/hooks/useLiveLocationSharingManager.ts diff --git a/examples/vite/src/App.tsx b/examples/vite/src/App.tsx index f8df1ed80..deed3e23e 100644 --- a/examples/vite/src/App.tsx +++ b/examples/vite/src/App.tsx @@ -1,4 +1,9 @@ -import { ChannelFilters, ChannelOptions, ChannelSort } from 'stream-chat'; +import { + ChannelFilters, + ChannelOptions, + ChannelSort, + LiveLocationManagerConstructorParameters, +} from 'stream-chat'; import { AIStateIndicator, Channel, @@ -13,6 +18,9 @@ import { useCreateChatClient, ThreadList, ChatView, + useChatContext, + useLiveLocationSharingManager, + Attachment, } from 'stream-chat-react'; import 'stream-chat-react/css/v2/index.css'; @@ -64,6 +72,53 @@ type StreamChatGenerics = { userType: LocalUserType; }; +const ShareLiveLocation = () => { + const { channel } = useChatContext(); + + return ( + + ); +}; + +const watchLocationNormal: LiveLocationManagerConstructorParameters['watchLocation'] = ( + watcher, +) => { + const watch = navigator.geolocation.watchPosition((position) => { + watcher({ latitude: position.coords.latitude, longitude: position.coords.longitude }); + }); + + return () => navigator.geolocation.clearWatch(watch); +}; + +const watchLocationTimed: LiveLocationManagerConstructorParameters['watchLocation'] = (watcher) => { + const timer = setInterval(() => { + navigator.geolocation.getCurrentPosition((position) => { + watcher({ latitude: position.coords.latitude, longitude: position.coords.longitude }); + }); + }, 5000); + + return () => clearInterval(timer); +}; + const App = () => { const chatClient = useCreateChatClient({ apiKey, @@ -71,6 +126,13 @@ const App = () => { userData: { id: userId }, }); + const manager = useLiveLocationSharingManager({ + client: chatClient ?? undefined, + watchLocation: watchLocationTimed, + }); + + // const s = useStateStore(manager?.state) + if (!chatClient) return <>Loading...; return ( @@ -86,12 +148,27 @@ const App = () => { showChannelSearch additionalChannelSearchProps={{ searchForChannels: true }} /> - + { + const [attachment] = props.attachments ?? []; + + if (attachment?.type === 'live_location') { + return ( +
+ lat: {attachment.latitude}, lng: {attachment.longitude} +
+ ); + } + + return ; + }} + > +
diff --git a/src/components/Attachment/Attachment.tsx b/src/components/Attachment/Attachment.tsx index f1c32c43a..a29c4d4d4 100644 --- a/src/components/Attachment/Attachment.tsx +++ b/src/components/Attachment/Attachment.tsx @@ -41,6 +41,7 @@ const CONTAINER_MAP = { media: MediaContainer, unsupported: UnsupportedAttachmentContainer, voiceRecording: VoiceRecordingContainer, + // geolocation: () =>
, } as const; export const ATTACHMENT_GROUPS_ORDER = [ diff --git a/src/components/Attachment/hooks/useLiveLocationSharingManager.ts b/src/components/Attachment/hooks/useLiveLocationSharingManager.ts new file mode 100644 index 000000000..4b9c0b94e --- /dev/null +++ b/src/components/Attachment/hooks/useLiveLocationSharingManager.ts @@ -0,0 +1,35 @@ +import { LiveLocationManager } from 'stream-chat'; +import type { LiveLocationManagerConstructorParameters } from 'stream-chat'; +import { useEffect, useMemo } from 'react'; + +type PartialKeys = { + [L in keyof T]: L extends K ? T[L] | undefined : T[L]; +}; + +export const useLiveLocationSharingManager = ({ + client, + retrieveAndDeserialize, + serializeAndStore, + watchLocation, +}: PartialKeys) => { + const manager = useMemo(() => { + if (!client) return null; + + return new LiveLocationManager({ + client, + retrieveAndDeserialize, + serializeAndStore, + watchLocation, + }); + }, [client, retrieveAndDeserialize, serializeAndStore, watchLocation]); + + useEffect(() => { + if (!manager) return; + + manager.registerSubscriptions(); + + return () => manager.unregisterSubscriptions(); + }, [manager]); + + return manager; +}; diff --git a/src/components/Attachment/index.ts b/src/components/Attachment/index.ts index f1385fe7d..31fce5f01 100644 --- a/src/components/Attachment/index.ts +++ b/src/components/Attachment/index.ts @@ -8,3 +8,4 @@ export * from './components'; export * from './UnsupportedAttachment'; export * from './FileAttachment'; export * from './utils'; +export * from './hooks/useLiveLocationSharingManager';