Skip to content

Commit

Permalink
Use browser blobs API to download large JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
sid-kap committed Feb 12, 2024
1 parent 1042766 commit 5ae086e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 24 additions & 5 deletions lib/PlotsTemplate.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect, useRef, useState } from "react"

Check failure on line 1 in lib/PlotsTemplate.tsx

View workflow job for this annotation

GitHub Actions / Lint Code Base

'useState' is defined but never used

Check failure on line 1 in lib/PlotsTemplate.tsx

View workflow job for this annotation

GitHub Actions / Lint Code Base

'useState' is defined but never used

import BarPlot from "lib/BarPlot"
import { CurrentYearExtrapolationInfo } from "lib/projections"
import { useFetch } from "lib/queries"
Expand Down Expand Up @@ -42,9 +44,8 @@ export default function PlotsTemplate({
jsonRoot: string
countyList?: JSX.Element
}): JSX.Element {
const { data } = useFetch(
selected != null ? jsonRoot + selected.value + ".json" : null
)
const url = selected != null ? jsonRoot + selected.value + ".json" : null
const { data } = useFetch(url)

const { selectedUnits, unitsSelect } = useUnitsSelect()

Expand Down Expand Up @@ -81,7 +82,11 @@ export default function PlotsTemplate({
{selected?.has_ca_hcd_data && <HcdDataInfo />}
</div>
</div>
<DownloadData data={data} name={selected?.name + ".json"} />
<DownloadData
data={data}
name={selected?.name + ".json"}
selected={url}
/>
</div>
</div>
)
Expand All @@ -90,13 +95,27 @@ export default function PlotsTemplate({
export function DownloadData({
data,
name,
selected,
}: {
data: object
name: string
selected: Any
}): JSX.Element {
let url = useRef("#")

Check failure on line 104 in lib/PlotsTemplate.tsx

View workflow job for this annotation

GitHub Actions / Lint Code Base

'url' is never reassigned. Use 'const' instead
useEffect(() => {
url.current = URL.createObjectURL(
new Blob([JSON.stringify(data)], { type: "octet/stream" })
)
// Cleanup function
return () => {
if (url.current != "#" && typeof window !== "undefined") {
URL.revokeObjectURL(url.current)
}
}
}, [selected])

Check warning on line 115 in lib/PlotsTemplate.tsx

View workflow job for this annotation

GitHub Actions / Lint Code Base

React Hook useEffect has a missing dependency: 'data'. Either include it or remove the dependency array
return (
<a
href={"data:application/json," + JSON.stringify(data)}
href={url.current}
className="text-sm text-blue-500 hover:text-blue-300"
download={name}
>
Expand Down
6 changes: 5 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ export default function Home(): JSX.Element {
{perCapitaInput}
{groupingInput}
{selectedLocations.some((l) => l.has_ca_hcd_data) && preferHcdDataInput}
<DownloadData data={data} name="housing data comparisons.json" />
<DownloadData
data={data}
name="housing data comparisons.json"
selected={selectedLocations}
/>
{selectedLocations.some((l) => l.has_ca_hcd_data) && <HcdDataInfo />}
</div>
</Page>
Expand Down

0 comments on commit 5ae086e

Please sign in to comment.