-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathGeolocation.tsx
More file actions
128 lines (118 loc) · 4.38 KB
/
Copy pathGeolocation.tsx
File metadata and controls
128 lines (118 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { ComponentType } from 'react';
import { useEffect } from 'react';
import { useRef, useState } from 'react';
import React from 'react';
import type { Coords, SharedLocationResponse } from 'stream-chat';
import {
useChatContext,
useComponentContext,
useTranslationContext,
} from '../../context';
import { ExternalLinkIcon } from './icons';
import { IconLocation as DefaultIconLocation } from '../Icons';
import { Button } from '../Button';
export type GeolocationMapProps = Coords;
export type GeolocationProps = {
location: SharedLocationResponse;
GeolocationAttachmentMapPlaceholder?: ComponentType<GeolocationAttachmentMapPlaceholderProps>;
GeolocationMap?: ComponentType<GeolocationMapProps>;
};
export const Geolocation = ({
GeolocationAttachmentMapPlaceholder = DefaultGeolocationAttachmentMapPlaceholder,
GeolocationMap,
location,
}: GeolocationProps) => {
const { channel, client } = useChatContext();
const { t } = useTranslationContext();
const [stoppedSharing, setStoppedSharing] = useState(
!!location.end_at && new Date(location.end_at).getTime() < new Date().getTime(),
);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const isMyLocation = location.user_id === client.userID;
const isLiveLocation = !!location.end_at;
useEffect(() => {
if (!location.end_at) return;
clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(
() => setStoppedSharing(true),
new Date(location.end_at).getTime() - Date.now(),
);
}, [location.end_at]);
return (
<div
className='str-chat__message-attachment-geolocation'
data-testid='attachment-geolocation'
>
<div className='str-chat__message-attachment-geolocation__location-preview'>
{GeolocationMap ? (
<GeolocationMap latitude={location.latitude} longitude={location.longitude} />
) : (
<GeolocationAttachmentMapPlaceholder location={location} />
)}
</div>
<div className='str-chat__message-attachment-geolocation__status'>
{isLiveLocation ? (
stoppedSharing ? (
t('Location sharing ended')
) : isMyLocation ? (
<div className='str-chat__message-attachment-geolocation__status--active'>
<Button
appearance='outline'
className='str-chat__message-attachment-geolocation__stop-sharing-button'
onClick={() => channel?.stopLiveLocationSharing(location)}
size='sm'
variant='secondary'
>
{t('Stop sharing')}
</Button>
<div className='str-chat__message-attachment-geolocation__status--active-until'>
{t('Live until {{ timestamp }}', {
timestamp: t('timestamp/LiveLocation', { timestamp: location.end_at }),
})}
</div>
</div>
) : (
<div className='str-chat__message-attachment-geolocation__status--active'>
<div className='str-chat__message-attachment-geolocation__status--active-status'>
{t('Live location')}
</div>
<div className='str-chat__message-attachment-geolocation__status--active-until'>
{t('Live until {{ timestamp }}', {
timestamp: t('timestamp/LiveLocation', { timestamp: location.end_at }),
})}
</div>
</div>
)
) : (
t('Current location')
)}
</div>
</div>
);
};
export type GeolocationAttachmentMapPlaceholderProps = {
location: SharedLocationResponse;
};
const DefaultGeolocationAttachmentMapPlaceholder = ({
location,
}: GeolocationAttachmentMapPlaceholderProps) => {
const { t } = useTranslationContext();
const { icons: { IconLocation = DefaultIconLocation } = {} } = useComponentContext();
return (
<div
className='str-chat__message-attachment-geolocation__placeholder'
data-testid='geolocation-attachment-map-placeholder'
>
<IconLocation />
<a
aria-label={t('Open location in a map')}
className='str-chat__message-attachment-geolocation__placeholder-link'
href={`https://maps.google.com?q=${[location.latitude, location.longitude].join()}`}
rel='noreferrer'
target='_blank'
>
<ExternalLinkIcon />
</a>
</div>
);
};