Skip to content

Commit a35c440

Browse files
committed
Fix unified search functionality and improve UI
- Fix JavaScript error in useUnifiedSearch.ts (session -> user typo) - Remove isPublic field filtering since all pages are now public - Add case-insensitive search (lowercase 'mexico' finds 'Mexico') - Improve search result alignment (center 'by username' text) - Simplify search queries to avoid complex Firestore index requirements - Add comprehensive debugging logs for search troubleshooting Search now works properly with both pages and users, handles case variations, and has clean visual alignment in results.
1 parent 5446007 commit a35c440

38 files changed

+2506
-1166
lines changed

app/[id]/location/page.tsx

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import LocationPickerPage from '../../components/map/LocationPickerPage';
77
interface Location {
88
lat: number;
99
lng: number;
10+
zoom?: number;
1011
}
1112

1213
function LocationPickerContent() {
@@ -15,6 +16,9 @@ function LocationPickerContent() {
1516
const [initialLocation, setInitialLocation] = useState<Location | null>(null);
1617
const [returnPath, setReturnPath] = useState<string>('/');
1718
const [pageId, setPageId] = useState<string>('');
19+
const [pageTitle, setPageTitle] = useState<string>('');
20+
const [isOwner, setIsOwner] = useState<boolean>(false);
21+
const [loading, setLoading] = useState<boolean>(true);
1822

1923
useEffect(() => {
2024
console.log('🗺️ LocationPicker: useEffect called with searchParams');
@@ -50,6 +54,54 @@ function LocationPickerContent() {
5054
}
5155
}, [searchParams]);
5256

57+
// Fetch page data to get title and ownership info
58+
useEffect(() => {
59+
async function fetchPageData() {
60+
console.log('🗺️ LocationPicker: useEffect triggered, pageId:', pageId);
61+
if (!pageId) {
62+
console.log('🗺️ LocationPicker: No pageId, setting loading to false');
63+
setLoading(false);
64+
return;
65+
}
66+
67+
try {
68+
console.log('🗺️ LocationPicker: Fetching page data for:', pageId);
69+
const response = await fetch(`/api/pages/${pageId}?userId=dev_admin_user`);
70+
if (response.ok) {
71+
const pageData = await response.json();
72+
console.log('🗺️ LocationPicker: Page data received:', pageData);
73+
setPageTitle(pageData.title || 'Untitled');
74+
75+
// Check if current user is the owner by checking if we can edit
76+
// For dev environment, assume dev_admin_user is always the owner
77+
const currentUser = await fetch('/api/auth/session');
78+
if (currentUser.ok) {
79+
const sessionData = await currentUser.json();
80+
console.log('🗺️ LocationPicker: Current user:', sessionData.user?.uid);
81+
console.log('🗺️ LocationPicker: Page owner:', pageData.userId);
82+
const isPageOwner = sessionData.user?.uid === pageData.userId;
83+
console.log('🗺️ LocationPicker: Is owner?', isPageOwner);
84+
setIsOwner(isPageOwner);
85+
} else {
86+
// In dev environment, if no session, assume dev_admin_user for pages owned by dev_admin_user
87+
console.log('🗺️ LocationPicker: No session, checking if dev page');
88+
const isDevPage = pageData.userId === 'dev_admin_user';
89+
console.log('🗺️ LocationPicker: Is dev page?', isDevPage);
90+
setIsOwner(isDevPage);
91+
}
92+
} else {
93+
console.error('🗺️ LocationPicker: Failed to fetch page data:', response.status);
94+
}
95+
} catch (error) {
96+
console.error('🗺️ LocationPicker: Error fetching page data:', error);
97+
} finally {
98+
setLoading(false);
99+
}
100+
}
101+
102+
fetchPageData();
103+
}, [pageId]);
104+
53105
const handleSave = async (location: Location | null) => {
54106
console.log('🗺️ LocationPicker: handleSave called with:', location);
55107
console.log('🗺️ LocationPicker: pageId:', pageId);
@@ -83,8 +135,12 @@ function LocationPickerContent() {
83135
const responseData = await response.json();
84136
console.log('🗺️ LocationPicker: Location saved successfully:', responseData);
85137

86-
// Navigate back to the page
138+
// Navigate back to the page and refresh to show updated location
87139
router.push(returnPath);
140+
// Force a refresh to reload the page data with the new location
141+
setTimeout(() => {
142+
router.refresh();
143+
}, 100);
88144
} catch (error) {
89145
console.error('🗺️ LocationPicker: Error saving location:', error);
90146
// Still navigate back even if save failed
@@ -96,12 +152,28 @@ function LocationPickerContent() {
96152
router.push(returnPath);
97153
};
98154

155+
if (loading) {
156+
return (
157+
<div className="min-h-screen bg-background flex items-center justify-center">
158+
<div className="text-center">
159+
<div className="h-8 w-8 mx-auto animate-spin rounded-full border-2 border-primary border-t-transparent mb-4" />
160+
<p className="text-muted-foreground">Loading page...</p>
161+
</div>
162+
</div>
163+
);
164+
}
165+
166+
const displayTitle = isOwner
167+
? `Set Location - ${pageTitle}`
168+
: pageTitle;
169+
99170
return (
100171
<LocationPickerPage
101172
initialLocation={initialLocation}
102173
onSave={handleSave}
103174
onCancel={handleCancel}
104-
pageTitle="Set Location"
175+
pageTitle={displayTitle}
176+
isOwner={isOwner}
105177
/>
106178
);
107179
}

app/[id]/location/view/page.tsx

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@ import React, { useState, useEffect, Suspense } from 'react';
44
import { useRouter, useSearchParams } from 'next/navigation';
55
import { ArrowLeft, MapPin } from 'lucide-react';
66
import { Button } from '../../../components/ui/button';
7-
import OpenStreetMapPicker from '../../../components/map/OpenStreetMapPicker';
7+
import MapPicker from '../../../components/map/MapPicker';
88

99
interface Location {
1010
lat: number;
1111
lng: number;
12+
zoom?: number;
1213
}
1314

1415
function LocationViewContent() {
1516
const router = useRouter();
1617
const searchParams = useSearchParams();
1718
const [location, setLocation] = useState<Location | null>(null);
1819
const [returnPath, setReturnPath] = useState<string>('/');
20+
const [pageTitle, setPageTitle] = useState<string>('');
21+
const [pageId, setPageId] = useState<string>('');
22+
const [titleLoading, setTitleLoading] = useState<boolean>(true);
1923

2024
useEffect(() => {
2125
// Get return path
2226
const returnParam = searchParams.get('return');
2327
if (returnParam) {
24-
setReturnPath(decodeURIComponent(returnParam));
28+
const decodedPath = decodeURIComponent(returnParam);
29+
setReturnPath(decodedPath);
30+
31+
// Extract page ID from return path
32+
const pathParts = decodedPath.split('/');
33+
const id = pathParts[pathParts.length - 1];
34+
setPageId(id);
2535
}
2636

2737
// Get location data
@@ -36,6 +46,30 @@ function LocationViewContent() {
3646
}
3747
}, [searchParams]);
3848

49+
// Fetch page data to get title
50+
useEffect(() => {
51+
async function fetchPageData() {
52+
if (!pageId) {
53+
setTitleLoading(false);
54+
return;
55+
}
56+
57+
try {
58+
const response = await fetch(`/api/pages/${pageId}`);
59+
if (response.ok) {
60+
const pageData = await response.json();
61+
setPageTitle(pageData.title || 'Untitled');
62+
}
63+
} catch (error) {
64+
console.error('Error fetching page data:', error);
65+
} finally {
66+
setTitleLoading(false);
67+
}
68+
}
69+
70+
fetchPageData();
71+
}, [pageId]);
72+
3973
const handleBack = () => {
4074
// Go to previous page - users can reach home via WeWrite logo
4175
try {
@@ -55,6 +89,8 @@ function LocationViewContent() {
5589
return `${loc.lat.toFixed(6)}, ${loc.lng.toFixed(6)}`;
5690
};
5791

92+
// Remove loading state - show map immediately
93+
5894
if (!location) {
5995
return (
6096
<div className="min-h-screen bg-background flex items-center justify-center">
@@ -69,7 +105,7 @@ function LocationViewContent() {
69105
}
70106

71107
return (
72-
<div className="min-h-screen bg-background flex flex-col">
108+
<div className="fixed inset-0 bg-background z-[9999] flex flex-col">
73109
{/* Header */}
74110
<div className="sticky top-0 z-10 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 border-b border-border">
75111
<div className="flex items-center justify-between p-4">
@@ -83,9 +119,11 @@ function LocationViewContent() {
83119
<ArrowLeft className="h-4 w-4" />
84120
</Button>
85121
<div>
86-
<h1 className="text-lg font-semibold text-foreground">Location View</h1>
122+
<h1 className="text-lg font-semibold text-foreground">
123+
{titleLoading ? 'Page Location' : (pageTitle || 'Page Location')}
124+
</h1>
87125
<p className="text-sm text-muted-foreground">
88-
{formatCoordinates(location)}
126+
Page location
89127
</p>
90128
</div>
91129
</div>
@@ -94,24 +132,17 @@ function LocationViewContent() {
94132

95133
{/* Map Container */}
96134
<div className="flex-1 relative">
97-
<OpenStreetMapPicker
135+
<MapPicker
98136
location={location}
99137
height="100%"
100-
readOnly={true}
101-
showControls={false}
102-
initialZoom={15}
138+
readOnly={true} // Don't allow pin placement in view mode
139+
showControls={false} // Hide zoom controls to prevent overlap with header
140+
initialZoom={location?.zoom || 15} // Use saved zoom level
141+
allowPanning={true} // Allow panning for exploration
103142
/>
104143
</div>
105144

106-
{/* Footer with location info */}
107-
<div className="bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 border-t border-border p-4">
108-
<div className="flex items-center gap-2 text-sm text-muted-foreground">
109-
<MapPin className="h-4 w-4" />
110-
<span>
111-
{formatCoordinates(location)}
112-
</span>
113-
</div>
114-
</div>
145+
{/* Footer with location info - removed coordinates display */}
115146
</div>
116147
);
117148
}

app/api/debug/create-test-page/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function POST(request: NextRequest) {
8181
// Test the recent edits API to see if our page shows up
8282
let recentEditsTest = null;
8383
try {
84-
const recentEditsUrl = new URL('/api/recent-edits', request.url);
84+
const recentEditsUrl = new URL('/api/recent-edits/global', request.url);
8585
recentEditsUrl.searchParams.set('limit', '10');
8686

8787
const response = await fetch(recentEditsUrl.toString());

app/api/debug/test-recent-edits/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function GET(request: NextRequest) {
7979
// Also test the actual recent edits API
8080
let apiResponse = null;
8181
try {
82-
const apiUrl = new URL('/api/recent-edits', request.url);
82+
const apiUrl = new URL('/api/recent-edits/global', request.url);
8383
if (userId) {
8484
apiUrl.searchParams.set('userId', userId);
8585
}

app/api/pages/[id]/location/route.js

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)