105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
import { PUBLIC_GMAPS_API_KEY } from "$env/static/public";
|
|
import type { Database } from "./database.d.ts";
|
|
|
|
export type Table<T extends keyof Database["public"]["Tables"]> =
|
|
Database["public"]["Tables"][T]["Row"];
|
|
export type Enum<T extends keyof Database["public"]["Enums"]> = Database["public"]["Enums"][T];
|
|
|
|
export const availableStudySpaceTags = [
|
|
"Crowded",
|
|
"Group study",
|
|
"Food allowed",
|
|
"No food allowed",
|
|
"Well lit",
|
|
"Poorly lit",
|
|
"Whiteboard",
|
|
"Restricted access",
|
|
"Hot",
|
|
"Air conditioned",
|
|
"Cold",
|
|
"PCs",
|
|
"Rodent-ridden"
|
|
];
|
|
|
|
export const volumeTags = ["Silent", "Some Noise", "Loud"];
|
|
export const wifiTags = ["Good WiFi", "Moderate WiFi", "Bad/No WiFi"];
|
|
export const powerOutletTags = ["Many Outlets", "Some Outlets", "No Outlets"];
|
|
|
|
export const allTags = [...availableStudySpaceTags, ...volumeTags, ...wifiTags, ...powerOutletTags];
|
|
|
|
export const reportTypes = [
|
|
"Inappropriate content",
|
|
"Duplicate content",
|
|
"Incorrect content",
|
|
"Other"
|
|
];
|
|
|
|
export async function gmapsLoader() {
|
|
const { Loader } = await import("@googlemaps/js-api-loader");
|
|
return new Loader({
|
|
apiKey: PUBLIC_GMAPS_API_KEY,
|
|
version: "weekly",
|
|
libraries: ["places"]
|
|
});
|
|
}
|
|
|
|
export function formatTime(time: string) {
|
|
const [h, m] = time.split(":").map(Number);
|
|
const date = new Date();
|
|
date.setHours(h, m);
|
|
return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
}
|
|
|
|
export const daysOfWeek = [
|
|
"Sunday",
|
|
"Monday",
|
|
"Tuesday",
|
|
"Wednesday",
|
|
"Thursday",
|
|
"Friday",
|
|
"Saturday",
|
|
"All Days",
|
|
"All Other Days"
|
|
];
|
|
|
|
// Convert "HH:MM" or "HH:MM:SS" to minutes since midnight
|
|
export function timeToMins(timeStr: string): number {
|
|
const [h, m] = timeStr.slice(0, 5).split(":").map(Number);
|
|
return h * 60 + m;
|
|
}
|
|
|
|
export function haversineDistance(
|
|
lat1Deg: number,
|
|
lng1Deg: number,
|
|
lat2Deg: number,
|
|
lng2Deg: number,
|
|
radius: number = 6371e3
|
|
): number {
|
|
const lat1 = lat1Deg * (Math.PI / 180);
|
|
const lat2 = lat2Deg * (Math.PI / 180);
|
|
const deltaLat = (lat2Deg - lat1Deg) * (Math.PI / 180);
|
|
const deltaLng = (lng2Deg - lng1Deg) * (Math.PI / 180);
|
|
const e1 =
|
|
Math.pow(Math.sin(deltaLat / 2), 2) +
|
|
Math.pow(Math.sin(deltaLng / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
return radius * 2 * Math.asin(Math.sqrt(e1));
|
|
}
|
|
|
|
export function collectTimings(study_space_hours: Partial<Table<"study_space_hours">>[]) {
|
|
// Collect all timing entries
|
|
const timingsPerDay: Record<number, Table<"study_space_hours">[]> = {
|
|
0: [],
|
|
1: [],
|
|
2: [],
|
|
3: [],
|
|
4: [],
|
|
5: [],
|
|
6: []
|
|
};
|
|
|
|
for (const entry of study_space_hours) {
|
|
timingsPerDay[entry.day_of_week].push(entry);
|
|
}
|
|
return timingsPerDay;
|
|
}
|