|
| 1 | +import { useState } from 'react' |
| 2 | +import StyledModal, { StyledModalProps } from './StyledModal' |
| 3 | +import Input from './inputs/Input' |
| 4 | +import api from '~/lib/api' |
| 5 | + |
| 6 | +export type DownloadURLModalProps = Omit< |
| 7 | + StyledModalProps, |
| 8 | + 'onConfirm' | 'open' | 'confirmText' | 'cancelText' | 'confirmVariant' | 'children' |
| 9 | +> & { |
| 10 | + suggestedURL?: string |
| 11 | + onPreflightSuccess?: (url: string) => void |
| 12 | +} |
| 13 | + |
| 14 | +const DownloadURLModal: React.FC<DownloadURLModalProps> = ({ |
| 15 | + suggestedURL, |
| 16 | + onPreflightSuccess, |
| 17 | + ...modalProps |
| 18 | +}) => { |
| 19 | + const [url, setUrl] = useState<string>(suggestedURL || '') |
| 20 | + const [messages, setMessages] = useState<string[]>([]) |
| 21 | + const [passedPreflight, setPassedPreflight] = useState<boolean>(false) |
| 22 | + |
| 23 | + async function runPreflightCheck(downloadUrl: string) { |
| 24 | + try { |
| 25 | + setMessages([`Running preflight check for URL: ${downloadUrl}`]) |
| 26 | + const res = await api.downloadRemoteMapRegionPreflight(downloadUrl) |
| 27 | + if ('message' in res) { |
| 28 | + throw new Error(res.message) |
| 29 | + } |
| 30 | + |
| 31 | + setMessages((prev) => [ |
| 32 | + ...prev, |
| 33 | + `Preflight check passed. Filename: ${res.filename}, Size: ${(res.size / (1024 * 1024)).toFixed(2)} MB`, |
| 34 | + ]) |
| 35 | + setPassedPreflight(true) |
| 36 | + } catch (error) { |
| 37 | + console.error('Preflight check failed:', error) |
| 38 | + setMessages((prev) => [...prev, `Preflight check failed: ${error.message}`]) |
| 39 | + setPassedPreflight(false) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return ( |
| 44 | + <StyledModal |
| 45 | + {...modalProps} |
| 46 | + onConfirm={() => runPreflightCheck(url)} |
| 47 | + open={true} |
| 48 | + confirmText="Download" |
| 49 | + confirmIcon="ArrowDownTrayIcon" |
| 50 | + cancelText="Cancel" |
| 51 | + confirmVariant="primary" |
| 52 | + large |
| 53 | + > |
| 54 | + <div className="flex flex-col pb-4"> |
| 55 | + <p className="text-gray-700 mb-8"> |
| 56 | + Enter the URL of the map region file you wish to download. The URL must be publicly |
| 57 | + reachable and end with .pmtiles. A preflight check will be run to verify the file's |
| 58 | + availability, type, and approximate size. |
| 59 | + </p> |
| 60 | + <Input |
| 61 | + name="download-url" |
| 62 | + label="" |
| 63 | + placeholder={suggestedURL || 'Enter download URL...'} |
| 64 | + className="mb-4" |
| 65 | + value={url} |
| 66 | + onChange={(e) => setUrl(e.target.value)} |
| 67 | + /> |
| 68 | + <div className="min-h-24 max-h-96 overflow-y-auto bg-gray-50 p-4 rounded border border-gray-300 text-left"> |
| 69 | + {messages.map((message, idx) => ( |
| 70 | + <p |
| 71 | + key={idx} |
| 72 | + className="text-sm text-gray-900 font-mono leading-relaxed break-words mb-3" |
| 73 | + > |
| 74 | + {message} |
| 75 | + </p> |
| 76 | + ))} |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </StyledModal> |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +export default DownloadURLModal |
0 commit comments