import isPointWithinRadius from "https://esm.sh/geolib/es/isPointWithinRadius";
import { useLoaderData } from "https://esm.sh/react-router-dom@6.23.0?deps=react@18.2.0,react-dom@18.2.0";
import date_me_doc_locations_geo from "https://esm.town/v/stevekrouse/date_me_doc_locations_geo";
import { nominatimSearch } from "https://esm.town/v/stevekrouse/nominatimSearch";
function absoluteURL(url) {
if (url.startsWith("http://") || url.startsWith("https://"))
return url;
else return "https://" + url;
}
let headers = [
"Name",
"Age",
"Gender",
"InterestedIn",
"Style",
"Location",
"LocationFlexibility",
"Contact",
"LastUpdated",
];
let linkClass = "text-sky-600 hover:text-sky-500";
function httpsIfy(url: string) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
return `https://${url}`;
}
return url;
}
function renderCell(header, row) {
let data = row[header];
if (header === "Name") {
return <a className={linkClass} href={httpsIfy(row["Profile"])} target="_blank">{data}</a>;
} else if (Array.isArray(data)) {
return data.map(d => <span className="p-1 m-1 border rounded-md">{d}</span>);
} else if (header === "LastUpdated") {
return new Date(data).toISOString().split("T")[0];
}
else {
return data;
}
}
function convertParams(url) {
const params = new URL(url).searchParams;
const search = {
gender: params.get("gender"),
desiredGender: params.get("desired-gender"),
minAge: params.get("min-age")?.length ? params.get("min-age") : 0,
maxAge: params.get("max-age")?.length ? params.get("max-age") : 100,
location: params.get("location"),
radius: params.get("radius"),
};
return search;
}
const metersInMile = 1609.34;
function withinRadius(
profileLocation: string[],
searchLocation: { lat: number; lon: number },
radius: number,
) {
if (!profileLocation) return false;
if (!Array.isArray(profileLocation)) profileLocation = [profileLocation];
const profileLocations = profileLocation.map(l => date_me_doc_locations_geo[l]);
return profileLocations.some(location => {
return location && isPointWithinRadius(location, searchLocation, radius * metersInMile);
});
}
export async function loader({ request }) {
"use server";
const { zip } = await import("npm:lodash-es");
const { sqlite } = await import("https://esm.town/v/std/sqlite");
const search = convertParams(request.url);
let { columns, rows } = await sqlite.execute({
sql: `select * from datemedocs
where
Age >= ?
and Age <= ?
and Gender like ?
and InterestedIn like ?
order by LastUpdated desc
`,
args: [
search.minAge,
search.maxAge,
"%" + (search.desiredGender ?? "") + "%",
"%" + (search.gender ?? "") + "%",
],
});
const searchLocation = search["location"]?.length ? await nominatimSearch({ q: search["location"] }) : undefined;