dyomedea/src/components/gpx/Gpx.tsx

101 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-01-09 13:23:41 +00:00
import { Component, createEffect, For, onCleanup, Suspense } from 'solid-js';
2022-11-27 14:50:08 +00:00
import OlMap from 'ol/Map';
2022-11-27 15:09:08 +00:00
import Trk from '../trk';
2022-11-27 16:12:25 +00:00
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
2022-11-28 07:38:21 +00:00
import style from './styles';
2022-11-28 10:52:50 +00:00
import Wpt from '../wpt';
2022-12-09 16:31:56 +00:00
import Rte from '../rte';
import {
createCachedSignal,
destroyCachedSignal,
} from '../../workers/cached-signals';
2022-11-27 14:50:08 +00:00
interface Props {
gpxId: string;
map: () => OlMap | null;
}
export const Gpx: Component<Props> = ({ map, gpxId }) => {
const params = {
id: gpxId,
method: 'getGpx',
};
const gpx = createCachedSignal(params);
2022-11-27 14:50:08 +00:00
onCleanup(() => {
2023-01-09 13:23:41 +00:00
console.log({
caller: 'Gpx / onCleanup',
gpxId,
gpx: gpx(),
params,
});
destroyCachedSignal(params);
});
2022-11-27 16:12:25 +00:00
const vectorSource = new VectorSource();
const vectorLayer = new VectorLayer({
source: vectorSource,
style,
2022-12-13 11:54:46 +00:00
declutter: true,
});
2022-11-27 16:12:25 +00:00
createEffect(() => map()?.addLayer(vectorLayer));
2022-11-27 14:50:08 +00:00
createEffect(() => {
2022-11-27 16:12:25 +00:00
console.log({ caller: 'Gpx', map: map(), gpxId, gpx: gpx(), vectorLayer });
2022-11-27 14:50:08 +00:00
});
2022-11-27 15:09:08 +00:00
return (
2022-11-28 10:52:50 +00:00
<>
<For each={gpx() ? gpx().trk || [] : []}>
{(trkId: string) => {
console.log({ caller: 'Gpx / loop', trkId });
return (
<Suspense>
<Trk
vectorSource={vectorSource}
trkId={trkId}
context={{ gpx, gpxId }}
/>
</Suspense>
);
2022-11-28 10:52:50 +00:00
}}
</For>
2022-12-09 16:31:56 +00:00
<For each={gpx() ? gpx().rte || [] : []}>
{(rteId: string) => {
console.log({ caller: 'Gpx / loop', rteId });
return (
<Suspense>
<Rte
vectorSource={vectorSource}
rteId={rteId}
context={{ gpx, gpxId }}
/>
2022-12-09 16:31:56 +00:00
</Suspense>
);
}}
</For>
2022-11-28 10:52:50 +00:00
<For each={gpx() ? gpx().wpt || [] : []}>
{(wptId: string) => {
console.log({ caller: 'Gpx / loop', wptId });
return (
<Suspense>
<Wpt
vectorSource={vectorSource}
wptId={wptId}
context={{ gpx, gpxId }}
/>
</Suspense>
);
2022-11-28 10:52:50 +00:00
}}
</For>
</>
2022-11-27 15:09:08 +00:00
);
2022-11-27 14:50:08 +00:00
};
export default Gpx;