Skip to content

Commit 4372754

Browse files
authored
Merge pull request #114 from Rindrics/fix
2 parents 8f515c8 + 03afade commit 4372754

3 files changed

Lines changed: 51 additions & 21 deletions

File tree

firestore.rules

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ service cloud.firestore {
1515
}
1616

1717
// Favorites: users can only read/write their own favorites
18-
match /favorites/{userId}/{document=**} {
19-
allow read, write: if request.auth != null && request.auth.uid == userId;
20-
}
21-
22-
// Favorites subcollection: users can only read/write their own favorites
23-
match /favorites/{userId}/items/{contentId} {
18+
match /favorites/{userId} {
2419
allow read, write: if request.auth != null && request.auth.uid == userId;
20+
21+
// Favorites subcollection: users can only read/write their own favorites
22+
match /items/{contentId} {
23+
allow read, write: if request.auth != null && request.auth.uid == userId;
24+
}
2525
}
2626

2727
// Legacy admin-only rules for other collections

src/components/AuthButton.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import Link from 'next/link';
44
import { useEffect, useState } from 'react';
55
import { useAuth } from '@/contexts/AuthContext';
66

7-
// Show auth UI in development or when using emulators
8-
const isDevelopment = process.env.NODE_ENV === 'development';
9-
const useEmulators = process.env.NEXT_PUBLIC_USE_FIREBASE_EMULATORS === 'true';
10-
const showAuthUI = isDevelopment || useEmulators;
11-
127
export function AuthButton() {
138
const {
149
user,
@@ -45,11 +40,6 @@ export function AuthButton() {
4540
return () => document.removeEventListener('keydown', handleEscape);
4641
}, []);
4742

48-
// Hide auth UI in production (unless using emulators)
49-
if (!showAuthUI) {
50-
return null;
51-
}
52-
5343
if (loading) {
5444
return (
5545
<div className="text-sm text-zinc-600 dark:text-zinc-400">

src/components/ListWithFavoriteSidebar.tsx

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22

3-
import { Suspense, useEffect, useState } from 'react';
3+
import { Suspense, useEffect, useLayoutEffect, useState } from 'react';
44
import { useAuth } from '@/contexts/AuthContext';
5+
import { getFavoriteContentIds } from '@/lib/favorites';
56
import { FavoriteContentList } from './FavoriteContentList';
67

78
interface Props {
@@ -16,9 +17,11 @@ export function ListWithFavoriteSidebar({
1617
maxItemsMobile = 2,
1718
}: Props) {
1819
const { user, loading } = useAuth();
19-
const [isLargeScreen, setIsLargeScreen] = useState(true);
20+
const [isLargeScreen, setIsLargeScreen] = useState(false);
21+
const [hasFavorites, setHasFavorites] = useState(false);
22+
const [isFavoritesLoading, setIsFavoritesLoading] = useState(true);
2023

21-
useEffect(() => {
24+
useLayoutEffect(() => {
2225
const mediaQuery = window.matchMedia('(min-width: 1024px)');
2326
setIsLargeScreen(mediaQuery.matches);
2427

@@ -27,8 +30,45 @@ export function ListWithFavoriteSidebar({
2730
return () => mediaQuery.removeEventListener('change', handler);
2831
}, []);
2932

30-
// Show full width when not authenticated
31-
if (!user && !loading) {
33+
// Check if user has favorites
34+
useLayoutEffect(() => {
35+
if (!user || loading) {
36+
setIsFavoritesLoading(true);
37+
setHasFavorites(false);
38+
return () => {};
39+
}
40+
41+
let cancelled = false;
42+
43+
setIsFavoritesLoading(true);
44+
getFavoriteContentIds(user.uid)
45+
.then((ids) => {
46+
if (!cancelled) {
47+
setHasFavorites(ids.length > 0);
48+
}
49+
})
50+
.catch(() => {
51+
if (!cancelled) {
52+
setHasFavorites(false);
53+
}
54+
})
55+
.finally(() => {
56+
if (!cancelled) {
57+
setIsFavoritesLoading(false);
58+
}
59+
});
60+
61+
return () => {
62+
cancelled = true;
63+
};
64+
}, [user, loading]);
65+
66+
// Show full width while loading or when authenticated but no favorites exist
67+
if (loading || isFavoritesLoading) {
68+
return <>{children}</>;
69+
}
70+
71+
if (!user || !hasFavorites) {
3272
return <>{children}</>;
3373
}
3474

0 commit comments

Comments
 (0)