import { DownOutlined, CopyOutlined, StopOutlined } from '@ant-design/icons'; import { Button, Dropdown, Menu, Segmented, Modal } from 'antd'; import copy from 'copy-to-clipboard'; import React, { useState } from 'react'; import { observer } from 'mobx-react-lite'; import { useStore } from 'App/mstore'; import { formatExpirationTime, HOUR_SECS, DAY_SECS, WEEK_SECS } from 'App/utils/index'; enum Intervals { hour, threeHours, day, week, } function AccessModal() { const { spotStore } = useStore(); const [isCopied, setIsCopied] = useState(false); const [isPublic, setIsPublic] = useState(!!spotStore.pubKey); const [generated, setGenerated] = useState(!!spotStore.pubKey); const [selectedInterval, setSelectedInterval] = useState(Intervals.hour); const [loadingKey, setLoadingKey] = useState(false); const expirationValues = { [Intervals.hour]: HOUR_SECS, [Intervals.threeHours]: 3 * HOUR_SECS, [Intervals.day]: DAY_SECS, [Intervals.week]: WEEK_SECS, }; const spotId = spotStore.currentSpot!.spotId!; const spotLink = `${window.location.origin}/view-spot/${spotId}${ spotStore.pubKey ? `?pub_key=${spotStore.pubKey.value}` : '' }`; const menuItems = [ { key: Intervals.hour.toString(), label:
1 Hour
, }, { key: Intervals.threeHours.toString(), label:
3 Hours
, }, { key: Intervals.day.toString(), label:
1 Day
, }, { key: Intervals.week.toString(), label:
1 Week
, }, ]; const onMenuClick = async (info: { key: string }) => { const val = expirationValues[Number(info.key) as Intervals]; setSelectedInterval(Number(info.key) as Intervals); await spotStore.generateKey(spotId, val); }; const changeAccess = async (toPublic: boolean) => { if (isPublic && !toPublic && spotStore.pubKey) { await spotStore.generateKey(spotId, 0); setIsPublic(toPublic); } else { setIsPublic(toPublic); } }; const revokeKey = async () => { await spotStore.generateKey(spotId, 0); setGenerated(false); setIsPublic(false); }; const generateInitial = async () => { setLoadingKey(true); const k = await spotStore.generateKey( spotId, expirationValues[Intervals.hour] ); setGenerated(!!k); setLoadingKey(false); }; const onCopy = () => { setIsCopied(true); copy(spotLink); setTimeout(() => setIsCopied(false), 2000); }; return (
changeAccess(value === 'public')} />
{!isPublic ? ( <>
Link for internal team members
{spotLink}
) : !generated ? (
) : ( <>
Anyone with the following link can access this Spot
{spotLink}
Link expires in
}>
{loadingKey ? 'Loading' : formatExpirationTime(expirationValues[selectedInterval])}
)}
); } export default observer(AccessModal);