diff --git a/bun.lockb b/bun.lockb index c7dfb49..9b6bf30 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 4b85dbe..7503821 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "clsx": "^2.1.1", "dayjs": "^1.11.11", "htmx.org": "^1.9.12", + "hyperscript.org": "^0.9.12", "kysely": "^0.26.3", "leaflet": "^1.9.4", "lit": "^3.1.4", diff --git a/src/env.d.ts b/src/env.d.ts index 0cce2e4..9b73ba6 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -7,4 +7,10 @@ export declare global { interface Window { htmx: any; } + + namespace astroHTML.JSX { + interface HTMLAttributes { + _?: any; + } + } } diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 06682d2..8abea3f 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -41,8 +41,10 @@ diff --git a/src/pages/[id].astro b/src/pages/[id].astro index 94407b1..4dc412f 100644 --- a/src/pages/[id].astro +++ b/src/pages/[id].astro @@ -1,7 +1,10 @@ --- +// Styles import "@/styles/globals.css"; import "../styles/locked-page.css"; +import "../styles/locked-content.css"; +// Components import Layout from "../layouts/Layout.astro"; import ShareButton from "../components/ShareButton"; import { @@ -14,6 +17,8 @@ import { import { CalendarIcon } from "@radix-ui/react-icons"; import { Separator } from "@/components/ui/separator"; import type { ContentTable } from "@/lib/db"; + +// Dayjs import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import utc from "dayjs/plugin/utc"; @@ -70,6 +75,44 @@ const dateFromNow = dayjs.utc(data?.created_at).from(dayjs.utc()); imageURL={`http://localhost:3000/images/${data?.blob_url}`} targetPosition={data?.loc}> +
+
+ +
+ +
+
+

+ Ne kadar yaklaştığını görmek için aşağıdaki butona bas. +

+ +
+
+
+
+
+
- + diff --git a/src/scripts/lockedContent.ts b/src/scripts/lockedContent.ts new file mode 100644 index 0000000..a092f01 --- /dev/null +++ b/src/scripts/lockedContent.ts @@ -0,0 +1,83 @@ +import { + calculateDistance, + errorCallback, +} from "@/components/LockedContent/geolocation"; +import { onLocationSuccess } from "@/scripts/initMap"; +let watchId: number; + +const targetLocation = document.getElementById("locked-content-container") + ?.dataset.targetPosition; + +const descriptionElement = document.getElementById( + "locked-content-description" +); + +const locationPermissionButton = document.getElementById( + "location-permission-button" +); + +function generateDistanceText(distance: number) { + // Update the proximity text according to the distance remaining + if (distance > 1000) { + return `${(distance / 1000).toFixed()} KM`; + } else if (distance > 100) { + return `${distance.toFixed(0)} M`; + } +} + +function updateCurrentLocation(position: GeolocationPosition) { + const newPosition = position.coords; + + if (targetLocation) { + const distance = calculateDistance( + [newPosition.latitude, newPosition.longitude], + JSON.parse(targetLocation) + ); + + const distanceText = generateDistanceText(distance); + + if (descriptionElement) + descriptionElement.innerText = `Kalan mesafe: ${distanceText}`; + + if (locationPermissionButton) locationPermissionButton.remove(); + } + + onLocationSuccess(position); +} + +function startWatchingLocation() { + if (!watchId) { + watchId = window.navigator.geolocation.watchPosition( + updateCurrentLocation, + errorCallback + ); + } +} + +const lockedContentContainer = document.getElementById( + "locked-content-container" +); + +lockedContentContainer?.addEventListener( + "askpermission", + startWatchingLocation +); + +navigator.permissions + .query({ name: "geolocation" }) + .then((permissionStatus) => { + switch (permissionStatus.state) { + case "granted": + watchId = window.navigator.geolocation.watchPosition( + updateCurrentLocation, + errorCallback + ); + break; + case "denied": + case "prompt": + default: + break; + } + }); + +export { startWatchingLocation };