Mallar, bokinställningar och kartmarkeringar skickar med version och visar en krockruta. Kopplingsfält kan ha flera val. Scheman och kartregister kan importeras från Word, och nya ID hoppar över upptagna. Äldre födelseår får fler namn genom två SCB-tabeller och grannår. Migrering 0008. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
4.1 KiB
TypeScript
109 lines
4.1 KiB
TypeScript
import { notFound } from "next/navigation";
|
|
import { deleteMap, renameMap } from "@/actions/maps";
|
|
import { ConfirmSubmit } from "@/components/confirm-submit";
|
|
import { MapLoader } from "@/components/map-loader";
|
|
import { PageBody, PageHeader } from "@/components/page";
|
|
import { getT } from "@/i18n/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { fileUrl } from "@/lib/files";
|
|
import { hasRole } from "@/lib/access";
|
|
import { versionOf } from "@/lib/conflict";
|
|
import { requireBook } from "@/lib/session";
|
|
|
|
// Standard är OpenStreetMaps publika tjänster. Byt via miljövariabler vid tyngre användning.
|
|
const TILE_URL = process.env.MAP_TILE_URL || "https://tile.openstreetmap.org/{z}/{x}/{y}.png";
|
|
const TILE_ATTRIBUTION = process.env.MAP_TILE_ATTRIBUTION || "© OpenStreetMap-bidragsgivare";
|
|
const GEOCODER_URL = process.env.MAP_GEOCODER_URL || "https://nominatim.openstreetmap.org/search";
|
|
|
|
export default async function MapPage({
|
|
params,
|
|
searchParams,
|
|
}: {
|
|
params: Promise<{ bookId: string; mapId: string }>;
|
|
searchParams: Promise<{ place?: string; marker?: string }>;
|
|
}) {
|
|
const { bookId, mapId } = await params;
|
|
const { place, marker } = await searchParams;
|
|
const canEdit = hasRole((await requireBook(bookId)).role, "EDITOR");
|
|
const { t } = await getT();
|
|
|
|
const [map, entities] = await Promise.all([
|
|
prisma.bookMap.findFirst({ where: { id: mapId, bookId }, include: { markers: { orderBy: { createdAt: "asc" } } } }),
|
|
prisma.entity.findMany({ where: { bookId }, select: { id: true, name: true, kind: true }, orderBy: { name: "asc" } }),
|
|
]);
|
|
if (!map) notFound();
|
|
|
|
const center: [number, number] | null =
|
|
map.centerLat !== null && map.centerLng !== null ? [map.centerLat, map.centerLng] : null;
|
|
|
|
return (
|
|
<>
|
|
<PageHeader
|
|
title={map.name}
|
|
pretitle={
|
|
<>
|
|
{t("map.title")}
|
|
{map.code ? <span className="badge ms-2">{map.code}</span> : null}
|
|
</>
|
|
}
|
|
/>
|
|
<PageBody>
|
|
<MapLoader
|
|
readOnly={!canEdit}
|
|
bookId={bookId}
|
|
mapId={map.id}
|
|
kind={map.kind}
|
|
imageUrl={fileUrl(map.imageFile)}
|
|
center={center}
|
|
zoom={map.zoom}
|
|
markers={map.markers.map((m) => ({
|
|
id: m.id,
|
|
code: m.code,
|
|
label: m.label,
|
|
note: m.note,
|
|
color: m.color,
|
|
lat: m.lat,
|
|
lng: m.lng,
|
|
entityId: m.entityId,
|
|
version: versionOf(m.updatedAt),
|
|
}))}
|
|
entities={entities}
|
|
tileUrl={TILE_URL}
|
|
tileAttribution={TILE_ATTRIBUTION}
|
|
geocoderUrl={GEOCODER_URL}
|
|
preselectEntityId={entities.some((e) => e.id === place) ? (place ?? null) : null}
|
|
initialMarkerId={marker ?? null}
|
|
/>
|
|
|
|
<div className="card mt-4" hidden={!canEdit}>
|
|
<div className="card-header">
|
|
<h3 className="card-title">{t("map.settings")}</h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<div className="row g-3">
|
|
<div className="col-md-6">
|
|
<form action={renameMap} className="input-group">
|
|
<input type="hidden" name="bookId" value={bookId} />
|
|
<input type="hidden" name="mapId" value={map.id} />
|
|
<input name="name" className="form-control" defaultValue={map.name} required maxLength={200} aria-label={t("map.rename")} />
|
|
<button type="submit" className="btn">
|
|
{t("map.rename")}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
<div className="col-md-6 d-flex align-items-center gap-3">
|
|
<form action={deleteMap}>
|
|
<input type="hidden" name="bookId" value={bookId} />
|
|
<input type="hidden" name="mapId" value={map.id} />
|
|
<ConfirmSubmit className="btn btn-outline-danger">{t("map.deleteMap")}</ConfirmSubmit>
|
|
</form>
|
|
<span className="text-secondary small">{t("map.deleteMapHelp")}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</PageBody>
|
|
</>
|
|
);
|
|
}
|