kennzeichen-seite/frontend/lib/pb.ts
2026-05-20 20:47:07 +02:00

150 lines
4.4 KiB
TypeScript

import PocketBase from "pocketbase";
// ── Singleton Client (Server + Client) ────────────────────────────────────────
const PB_URL =
typeof window === "undefined"
? (process.env.PB_INTERNAL_URL ?? "http://pocketbase:8090")
: (process.env.NEXT_PUBLIC_PB_URL ?? "http://localhost:8090");
export const pb = new PocketBase(PB_URL);
pb.autoCancellation(false);
// ── Typen ─────────────────────────────────────────────────────────────────────
export interface Kennzeichen {
id: string;
code: string;
name: string;
derivation: string;
region: string;
note: string;
active: boolean;
country: string;
lat: number | null;
lon: number | null;
population: number | null;
points: number | null;
alt_code: string;
}
export interface Diplomatenkennzeichen {
id: string;
code: string;
country: string;
organisation: string;
type: "embassy" | "consulate" | "io";
city: string;
note: string;
base_country: string;
}
export interface BlogPost {
id: string;
slug: string;
titel: string;
inhalt: string;
date: string;
tags: string[];
}
export interface Gesehen {
id: string;
kennzeichen_code: string;
kennzeichen_name: string;
land: string;
datum: string;
}
// ── Hilfsfunktionen ───────────────────────────────────────────────────────────
export const COUNTRY_LABELS: Record<string, string> = {
germany: "Deutschland",
austria: "Österreich",
switzerland: "Schweiz",
luxembourg: "Luxemburg",
poland: "Polen",
norway: "Norwegen",
uk: "Vereinigtes Königreich",
italy: "Italien",
france: "Frankreich",
greece: "Griechenland",
slovakia: "Slowakei",
croatia: "Kroatien",
ukraine: "Ukraine",
serbia: "Serbien",
russia: "Russland",
belarus: "Belarus",
montenegro: "Montenegro",
northmacedonia: "Nordmazedonien",
ireland: "Irland",
bulgaria: "Bulgarien",
romania: "Rumänien",
moldova: "Moldawien",
czechia: "Tschechien",
turkey: "Türkei",
kosovo: "Kosovo",
slovenia: "Slowenien",
};
export const COUNTRY_FLAGS: Record<string, string> = {
germany: "🇩🇪", austria: "🇦🇹", switzerland: "🇨🇭", luxembourg: "🇱🇺",
poland: "🇵🇱", norway: "🇳🇴", uk: "🇬🇧", italy: "🇮🇹",
france: "🇫🇷", greece: "🇬🇷", slovakia: "🇸🇰", croatia: "🇭🇷",
ukraine: "🇺🇦", serbia: "🇷🇸", russia: "🇷🇺", belarus: "🇧🇾",
montenegro: "🇲🇪", northmacedonia: "🇲🇰", ireland: "🇮🇪",
bulgaria: "🇧🇬", romania: "🇷🇴", moldova: "🇲🇩", czechia: "🇨🇿",
turkey: "🇹🇷", kosovo: "🇽🇰", slovenia: "🇸🇮",
};
export async function getKennzeichen(opts?: {
country?: string;
search?: string;
page?: number;
perPage?: number;
activeOnly?: boolean;
}) {
const {
country,
search,
page = 1,
perPage = 50,
activeOnly = true,
} = opts ?? {};
const filters: string[] = [];
if (country) filters.push(`country="${country}"`);
if (activeOnly) filters.push(`active=true`);
if (search) {
const s = search.replace(/["\\]/g, "");
filters.push(`(code~"${s}" || name~"${s}" || region~"${s}")`);
}
return pb.collection("kennzeichen").getList<Kennzeichen>(page, perPage, {
filter: filters.join(" && ") || undefined,
sort: "country,code",
});
}
export async function getKennzeichenByCode(code: string) {
return pb.collection("kennzeichen").getFirstListItem<Kennzeichen>(
`code="${code.toUpperCase()}"`,
);
}
export async function getDiplomatenkennzeichen(baseCountry = "germany") {
return pb.collection("diplomatenkennzeichen").getFullList<Diplomatenkennzeichen>({
filter: `base_country="${baseCountry}"`,
sort: "code",
});
}
export async function getBlogPosts() {
return pb.collection("blog_posts").getList<BlogPost>(1, 20, {
sort: "-created",
});
}
export async function getGesehen() {
return pb.collection("gesehen").getFullList<Gesehen>({
sort: "-datum",
});
}