{"version":3,"file":"Geolocation.mjs","names":[],"sources":["../../../../src/components/Attachment/Geolocation.tsx"],"sourcesContent":["import type { ComponentType } from 'react';\nimport { useEffect } from 'react';\nimport { useRef, useState } from 'react';\nimport React from 'react';\nimport type { Coords, SharedLocationResponseData } from 'stream-chat';\nimport {\n  useChannel,\n  useChatContext,\n  useComponentContextIcons,\n  useTranslationContext,\n} from '../../context';\nimport { ExternalLinkIcon } from './icons';\nimport { Button } from '../Button';\nimport { convertTimestampToDate, nowNs, nsToMs } from 'stream-chat';\n\nexport type GeolocationMapProps = Coords;\n\n/** `setTimeout` silently clamps a longer delay to 1 ms, so longer waits are armed in steps. */\nconst MAX_TIMEOUT_MS = 2 ** 31 - 1;\n\n/** Whether a live location's expiry is a usable instant to schedule against. */\nconst isSchedulableExpiry = (endAt?: number): endAt is number =>\n  endAt != null && Number.isFinite(endAt);\n\nexport type GeolocationProps = {\n  location: SharedLocationResponseData;\n  GeolocationAttachmentMapPlaceholder?: ComponentType<GeolocationAttachmentMapPlaceholderProps>;\n  GeolocationMap?: ComponentType<GeolocationMapProps>;\n};\n\nexport const Geolocation = ({\n  GeolocationAttachmentMapPlaceholder = DefaultGeolocationAttachmentMapPlaceholder,\n  GeolocationMap,\n  location,\n}: GeolocationProps) => {\n  const { client } = useChatContext();\n  const channel = useChannel();\n  const { t } = useTranslationContext();\n\n  const [stoppedSharing, setStoppedSharing] = useState(\n    isSchedulableExpiry(location.end_at) && location.end_at < nowNs(),\n  );\n  const timeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n\n  const isMyLocation = location.user_id === client.userID;\n  const isLiveLocation = location.end_at != null;\n  const endAt = location.end_at;\n\n  useEffect(() => {\n    if (!isSchedulableExpiry(endAt)) return;\n\n    const arm = () => {\n      // Wire timestamps are nanoseconds: the difference must go through `nsToMs`, or `setTimeout`\n      // fires immediately and ends sharing on mount.\n      const remaining = Math.max(0, nsToMs(endAt - nowNs()));\n      timeoutRef.current =\n        remaining > MAX_TIMEOUT_MS\n          ? // Wait the maximum, then re-arm: one call is clamped to 1 ms, ending sharing on mount\n            // for any share longer than ~24.9 days.\n            setTimeout(arm, MAX_TIMEOUT_MS)\n          : setTimeout(() => setStoppedSharing(true), remaining);\n    };\n\n    arm();\n    return () => clearTimeout(timeoutRef.current);\n  }, [endAt]);\n\n  return (\n    <div\n      className='str-chat__message-attachment-geolocation'\n      data-testid='attachment-geolocation'\n    >\n      <div className='str-chat__message-attachment-geolocation__location-preview'>\n        {GeolocationMap ? (\n          <GeolocationMap latitude={location.latitude} longitude={location.longitude} />\n        ) : (\n          <GeolocationAttachmentMapPlaceholder location={location} />\n        )}\n      </div>\n      <div className='str-chat__message-attachment-geolocation__status'>\n        {isLiveLocation ? (\n          stoppedSharing ? (\n            t(\n              'attachment.geolocation.locationSharingEnded.text',\n              'Location sharing ended',\n            )\n          ) : isMyLocation ? (\n            <div className='str-chat__message-attachment-geolocation__status--active'>\n              <Button\n                appearance='outline'\n                className='str-chat__message-attachment-geolocation__stop-sharing-button'\n                onClick={() =>\n                  // Only `message_id`, not the whole response: `stopLiveLocationSharing` stamps\n                  // `end_at` itself.\n                  channel?.stopLiveLocationSharing({ message_id: location.message_id })\n                }\n                size='sm'\n                variant='secondary'\n              >\n                {t('attachment.geolocation.stopSharing.text', 'Stop sharing')}\n              </Button>\n              <div className='str-chat__message-attachment-geolocation__status--active-until'>\n                {t(\n                  'attachment.geolocation.liveUntil.text',\n                  'Live until {{ timestamp }}',\n                  {\n                    timestamp: t('timestamp.LiveLocation', {\n                      timestamp: convertTimestampToDate(location.end_at),\n                    }),\n                  },\n                )}\n              </div>\n            </div>\n          ) : (\n            <div className='str-chat__message-attachment-geolocation__status--active'>\n              <div className='str-chat__message-attachment-geolocation__status--active-status'>\n                {t('common.liveLocation.text', 'Live location')}\n              </div>\n              <div className='str-chat__message-attachment-geolocation__status--active-until'>\n                {t(\n                  'attachment.geolocation.liveUntil.text',\n                  'Live until {{ timestamp }}',\n                  {\n                    timestamp: t('timestamp.LiveLocation', {\n                      timestamp: convertTimestampToDate(location.end_at),\n                    }),\n                  },\n                )}\n              </div>\n            </div>\n          )\n        ) : (\n          t('common.currentLocation.text', 'Current location')\n        )}\n      </div>\n    </div>\n  );\n};\n\nexport type GeolocationAttachmentMapPlaceholderProps = {\n  location: SharedLocationResponseData;\n};\n\nconst DefaultGeolocationAttachmentMapPlaceholder = ({\n  location,\n}: GeolocationAttachmentMapPlaceholderProps) => {\n  const { IconLocation } = useComponentContextIcons();\n  const { t } = useTranslationContext();\n\n  return (\n    <div\n      className='str-chat__message-attachment-geolocation__placeholder'\n      data-testid='geolocation-attachment-map-placeholder'\n    >\n      <IconLocation />\n      <a\n        aria-label={t(\n          'attachment.geolocation.openLocationMap.ariaLabel',\n          'Open location in a map',\n        )}\n        className='str-chat__message-attachment-geolocation__placeholder-link'\n        href={`https://maps.google.com?q=${[location.latitude, location.longitude].join()}`}\n        rel='noreferrer'\n        target='_blank'\n      >\n        <ExternalLinkIcon />\n      </a>\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;AAkBA,IAAM,iBAAiB,KAAK,KAAK;;AAGjC,IAAM,uBAAuB,UAC3B,SAAS,QAAQ,OAAO,SAAS,KAAK;AAQxC,IAAa,eAAe,EAC1B,sCAAsC,4CACtC,gBACA,eACsB;CACtB,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,UAAU,WAAW;CAC3B,MAAM,EAAE,MAAM,sBAAsB;CAEpC,MAAM,CAAC,gBAAgB,qBAAqB,SAC1C,oBAAoB,SAAS,MAAM,KAAK,SAAS,SAAS,MAAM,CAClE;CACA,MAAM,aAAa,OAAkD,KAAA,CAAS;CAE9E,MAAM,eAAe,SAAS,YAAY,OAAO;CACjD,MAAM,iBAAiB,SAAS,UAAU;CAC1C,MAAM,QAAQ,SAAS;CAEvB,gBAAgB;EACd,IAAI,CAAC,oBAAoB,KAAK,GAAG;EAEjC,MAAM,YAAY;GAGhB,MAAM,YAAY,KAAK,IAAI,GAAG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