Skip to content

Commit d6e5a54

Browse files
Move OSM back into an atom family
1 parent d6f87bf commit d6e5a54

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

apps/osm-merge/src/hooks/osm.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { addLogMessageAtom } from "@/state/log"
22
import { osmWorkerAtom } from "@/state/worker"
33
import * as Comlink from "comlink"
4-
import { useAtomValue, useSetAtom } from "jotai"
4+
import { useAtom, useAtomValue, useSetAtom } from "jotai"
55
import { Osm } from "osm.ts"
66
import { useEffect, useMemo, useState } from "react"
77
import useStartTask from "./log"
88
import { useFitBoundsOnChange } from "./map"
9+
import { osmAtomFamily } from "@/state/osm"
910

1011
export function useOsmWorker() {
1112
const osmWorker = useAtomValue(osmWorkerAtom)
@@ -20,8 +21,8 @@ export function useOsmWorker() {
2021
return osmWorker
2122
}
2223

23-
export function useOsmFile(file: File | null, id?: string) {
24-
const [osm, setOsm] = useState<Osm | null>(null)
24+
export function useOsmFile(id: string, file: File | null) {
25+
const [osm, setOsm] = useAtom(osmAtomFamily(id))
2526
const [isLoading, setIsLoading] = useState(false)
2627
const osmWorker = useOsmWorker()
2728
const startTask = useStartTask()
@@ -49,7 +50,7 @@ export function useOsmFile(file: File | null, id?: string) {
4950
.finally(() => {
5051
setIsLoading(false)
5152
})
52-
}, [file, id, osmWorker, startTask])
53+
}, [file, id, osmWorker, setOsm, startTask])
5354

5455
return { osm, setOsm, isLoading }
5556
}

apps/osm-merge/src/pages/inspect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function ViewPage() {
2121
const [file, setFile] = useState<File | null>(null)
2222
const osmId = useMemo(() => file?.name ?? "default", [file])
2323
const map = useAtomValue(mapAtom)
24-
const { osm, isLoading: isLoadingFile } = useOsmFile(file)
24+
const { osm, isLoading: isLoadingFile } = useOsmFile("inspect", file)
2525
const bbox = useMemo(() => osm?.bbox(), [osm])
2626
const logMessage = useSetAtom(addLogMessageAtom)
2727
const setSelectedEntity = useSetAtom(selectedEntityAtom)

apps/osm-merge/src/pages/merge.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export default function Merge() {
4343
osm: baseOsm,
4444
setOsm: setBaseOsm,
4545
isLoading: baseOsmIsLoading,
46-
} = useOsmFile(baseFile, "base")
46+
} = useOsmFile("base", baseFile)
4747
const {
4848
osm: patchOsm,
4949
setOsm: setPatchOsm,
5050
isLoading: patchOsmIsLoading,
51-
} = useOsmFile(patchFile, "patch")
52-
const [mergedOsm, setMergedOsm] = useState<Osm | null>(null)
51+
} = useOsmFile("patch", patchFile)
52+
const { osm: mergedOsm, setOsm: setMergedOsm } = useOsmFile("inspect", null)
5353
const osmWorker = useOsmWorker()
5454
const [isTransitioning, startTransition] = useTransition()
5555
const [changes, setChanges] = useState<OsmChanges | null>(null)

apps/osm-merge/src/state/osm.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { atom } from "jotai"
2+
import { atomFamily } from "jotai/utils"
3+
import type { Osm } from "osm.ts"
4+
5+
export const osmAtomFamily = atomFamily((id: string) => atom<Osm | null>(null))

packages/osm.ts/src/osm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class Osm {
297297
return this.nodes.bbox ?? this.headerBbox()
298298
}
299299

300-
generateChangeset(other: Osm, options: OsmMergeOptions) {
300+
generateChangeset(other: Osm, options?: OsmMergeOptions) {
301301
const changeset = new OsmChangeset(this)
302302
changeset.generateFullChangeset(other, options)
303303
return changeset

0 commit comments

Comments
 (0)