diff --git a/README.md b/README.md index 7f2b8f2..db77d4d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # PixelChat -Real-time 1:1 chat — friend requests, presence, typing indicators, and file -attachments. Built with Next.js (App Router) and Supabase. +Real-time 1:1 chat — friend requests, presence, typing indicators, file +attachments and location sharing. Built with Next.js (App Router) and Supabase. ## Stack @@ -12,6 +12,7 @@ attachments. Built with Next.js (App Router) and Supabase. | Data | Supabase Postgres, access controlled entirely by Row Level Security | | Real-time | Supabase Realtime — private Broadcast channels and Presence | | Files | Supabase Storage, private bucket with signed URLs | +| Maps | Leaflet with OpenStreetMap tiles — no API key, no account | | Errors | Sentry (`@sentry/nextjs`) | | Hosting | Vercel | @@ -131,6 +132,7 @@ src/ chat-shell.tsx owns chat state and wires the pieces together use-realtime.ts channel subscriptions, presence and typing conversation-list.tsx, message-thread.tsx, attachment.tsx + location.tsx location bubble and the map picker friends.tsx, profile-dialog.tsx, settings-dialog.tsx, app-menu.tsx content/ page copy shared between routes and in-place dialogs user-avatar.tsx avatar with initial fallback, used everywhere diff --git a/package-lock.json b/package-lock.json index c2a85c3..4b6dc70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@supabase/supabase-js": "2.112.4", "class-variance-authority": "0.7.1", "clsx": "2.1.1", + "leaflet": "^1.9.4", "lucide-react": "1.34.0", "next": "16.3.2", "next-themes": "0.4.6", @@ -30,6 +31,7 @@ }, "devDependencies": { "@tailwindcss/postcss": "^4", + "@types/leaflet": "^1.9.22", "@types/node": "^20", "@types/react": "^19", "@types/react-dom": "^19", @@ -3472,6 +3474,13 @@ "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", "license": "MIT" }, + "node_modules/@types/geojson": { + "version": "7946.0.16", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.16.tgz", + "integrity": "sha512-6C8nqWur3j98U6+lXDfTUWIfgvZU+EumvpHKcYjujKH7woYyLj2sUmff0tRhrqM7BohUw7Pz3ZB1jj2gW9Fvmg==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -3485,6 +3494,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/leaflet": { + "version": "1.9.22", + "resolved": "https://registry.npmjs.org/@types/leaflet/-/leaflet-1.9.22.tgz", + "integrity": "sha512-h3lhECYEKDasG7LFHu+GiHqAvsgLuQvlJvVZzJDGONo3sEL+wUOqSFLnwkZlK0qVxnxbuGFW8iBlJNYs5wgndA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/geojson": "*" + } + }, "node_modules/@types/node": { "version": "20.19.43", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.43.tgz", @@ -7109,6 +7128,12 @@ "node": ">=0.10" } }, + "node_modules/leaflet": { + "version": "1.9.4", + "resolved": "https://registry.npmjs.org/leaflet/-/leaflet-1.9.4.tgz", + "integrity": "sha512-nxS1ynzJOmOlHp+iL3FyWqK89GtNL8U8rvlMOsQdTTssxZwCXh8N2NB3GDQOL+YR3XnWyZAxwQixURb+FA74PA==", + "license": "BSD-2-Clause" + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", diff --git a/package.json b/package.json index 8d842b1..6edc7ed 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@supabase/supabase-js": "2.112.4", "class-variance-authority": "0.7.1", "clsx": "2.1.1", + "leaflet": "^1.9.4", "lucide-react": "1.34.0", "next": "16.3.2", "next-themes": "0.4.6", @@ -33,6 +34,7 @@ }, "devDependencies": { "@tailwindcss/postcss": "^4", + "@types/leaflet": "^1.9.22", "@types/node": "^20", "@types/react": "^19", "@types/react-dom": "^19", diff --git a/src/components/chat/chat-shell.tsx b/src/components/chat/chat-shell.tsx index 283098e..33adcd2 100644 --- a/src/components/chat/chat-shell.tsx +++ b/src/components/chat/chat-shell.tsx @@ -12,7 +12,7 @@ import { useConversationChannels, useFriendNotifications } from './use-realtime' import { beep } from '@/lib/prefs' import { fetchPendingRequests } from '@/lib/queries' import { previewOf, type Conversation, type FriendRequest, type Message } from '@/lib/types' -import { usernameSchema } from '@/lib/validation' +import { formatLatLng, usernameSchema } from '@/lib/validation' const PAGE_SIZE = 50 @@ -171,7 +171,9 @@ export function ChatShell({ setHasMore((data?.length ?? 0) === PAGE_SIZE) } - async function send(body: string) { + // A location is just a body under a different kind, so it takes the same path + // -- including the optimistic row the realtime echo reconciles against. + async function send(body: string, kind: 'text' | 'location' = 'text') { if (!activeId) return // Client-generated id so the optimistic row and the broadcast echo match. const id = crypto.randomUUID() @@ -182,7 +184,7 @@ export function ChatShell({ body, attachment_path: null, attachment_name: null, - kind: 'text', + kind, created_at: new Date().toISOString(), } setMessages((prev) => [...prev, optimistic]) @@ -192,7 +194,7 @@ export function ChatShell({ conversation_id: activeId, sender_id: me, body, - kind: 'text', + kind, }) if (error) { @@ -346,6 +348,7 @@ export function ChatShell({ hasMore={hasMore} onSend={send} onUpload={upload} + onSendLocation={(lat, lng) => send(formatLatLng(lat, lng), 'location')} onTyping={handleTyping} onLoadOlder={loadOlder} onBack={() => setActiveId(null)} diff --git a/src/components/chat/location.tsx b/src/components/chat/location.tsx new file mode 100644 index 0000000..f87108e --- /dev/null +++ b/src/components/chat/location.tsx @@ -0,0 +1,186 @@ +'use client' + +import { useEffect, useRef, useState } from 'react' +import { LocateFixed, MapPin } from 'lucide-react' +import { toast } from 'sonner' +import 'leaflet/dist/leaflet.css' +import type { Map as LeafletMap } from 'leaflet' +import { Button } from '@/components/ui/button' +import { + Dialog, DialogContent, DialogDescription, DialogFooter, + DialogHeader, DialogTitle, +} from '@/components/ui/dialog' +import { parseLatLng } from '@/lib/validation' +import type { Message } from '@/lib/types' + +/* Opens on the whole world rather than guessing: the first thing anyone does is + press "Use my location" or pan somewhere they already have in mind. */ +const DEFAULT_VIEW = { lat: 20, lng: 0, zoom: 2 } as const +const PICKED_ZOOM = 16 + +/** + * One point-in-time fix. Shared by the composer's "Current location" and the + * dialog's recentre button, so the failure messages are written once. + * + * Rejects with a message meant for a toast -- the caller never sees a + * GeolocationPositionError. + */ +export function locate(): Promise<{ lat: number; lng: number }> { + return new Promise((resolve, reject) => { + // Geolocation is gated on a secure context. Over plain http it is not + // merely denied, it is absent, so this is not the same case as a refusal. + if (!navigator.geolocation) { + reject(new Error('This browser will not share a location over an insecure connection')) + return + } + navigator.geolocation.getCurrentPosition( + (position) => + resolve({ lat: position.coords.latitude, lng: position.coords.longitude }), + (error) => + reject( + new Error( + error.code === error.PERMISSION_DENIED + ? 'Location permission was denied' + : 'Could not work out where you are', + ), + ), + { enableHighAccuracy: true, timeout: 10_000, maximumAge: 60_000 }, + ) + }) +} + +/** + * A received location. The map is an OpenStreetMap embed rather than a second + * Leaflet instance: a thread can hold many of these, and an iframe costs no + * JavaScript per bubble. + */ +export function LocationMessage({ message }: { message: Message }) { + const point = parseLatLng(message.body) + if (!point) return

Location unavailable

+ + const { lat, lng } = point + // Roughly a 900 m box, so the pin has recognisable streets around it. + const d = 0.004 + const bbox = [lng - d, lat - d, lng + d, lat + d].join(',') + + return ( +
+