( url: string, onProgress: (loaded: number, total: number) => void )
| 140 | // FETCH WITH PROGRESS TRACKING |
| 141 | // ============================================================================ |
| 142 | const fetchWithProgress = async ( |
| 143 | url: string, |
| 144 | onProgress: (loaded: number, total: number) => void |
| 145 | ): Promise<Response> => { |
| 146 | const response = await fetch(url); |
| 147 | if (!response.ok || !response.body) return response; |
| 148 | |
| 149 | const contentLength = response.headers.get("content-length"); |
| 150 | const total = contentLength ? parseInt(contentLength, 10) : 0; |
| 151 | |
| 152 | let loaded = 0; |
| 153 | const reader = response.body.getReader(); |
| 154 | const stream = new ReadableStream({ |
| 155 | async start(controller) { |
| 156 | try { |
| 157 | while (true) { |
| 158 | const { done, value } = await reader.read(); |
| 159 | if (done) { |
| 160 | controller.close(); |
| 161 | break; |
| 162 | } |
| 163 | loaded += value.byteLength; |
| 164 | onProgress(loaded, total); |
| 165 | controller.enqueue(value); |
| 166 | } |
| 167 | } catch (err) { |
| 168 | controller.error(err); |
| 169 | } |
| 170 | } |
| 171 | }); |
| 172 | |
| 173 | return new Response(stream, { |
| 174 | headers: response.headers, |
| 175 | status: response.status, |
| 176 | statusText: response.statusText |
| 177 | }); |
| 178 | }; |
| 179 | |
| 180 | const Explore = () => { |
| 181 | const [searchParams] = useSearchParams(); |
no test coverage detected