-
Notifications
You must be signed in to change notification settings - Fork 75
refactor(examples): add kitchen-sink example #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cabljac
wants to merge
5
commits into
main
Choose a base branch
from
@invertase/examples-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e3778c
refactor(examples): made kitchen-sink example
cabljac 64d2fed
refactor(examples): nested collection query improvements
cabljac d825178
chore: format
cabljac cade209
docs(examples): add withConverter example
cabljac 2b79f00
refactor(examples): update collection query to invalidate queries
cabljac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# TanStack Query Firebase Examples | ||
|
||
A comprehensive example application showcasing various TanStack Query Firebase hooks and patterns. | ||
|
||
## Features | ||
|
||
- **Authentication Examples**: ID token management with `useGetIdTokenQuery` | ||
- **Firestore Examples**: Collection querying with `useCollectionQuery` | ||
- **Real-time Updates**: See how the UI updates when data changes | ||
- **Mutation Integration**: Add/delete operations with proper error handling | ||
- **Loading States**: Proper loading and error state management | ||
- **Query Key Management**: Dynamic query keys based on filters | ||
|
||
## Running the Examples | ||
|
||
1. Start the Firebase emulators: | ||
```bash | ||
cd ../../../ && firebase emulators:start | ||
``` | ||
|
||
2. In another terminal, run the example app: | ||
```bash | ||
pnpm dev:emulator | ||
``` | ||
|
||
3. Navigate to different examples using the navigation bar: | ||
- **Home**: Overview of available examples | ||
- **ID Token Query**: Firebase Authentication token management | ||
- **Collection Query**: Firestore collection querying with filters | ||
|
||
## Key Concepts Demonstrated | ||
|
||
- Using `useGetIdTokenQuery` for Firebase Authentication | ||
- Using `useCollectionQuery` with different query configurations | ||
- Combining queries with mutations (`useAddDocumentMutation`, `useDeleteDocumentMutation`) | ||
- Dynamic query keys for filtered results | ||
- Proper TypeScript integration with Firestore data | ||
- React Router for navigation between examples | ||
|
||
## File Structure | ||
|
||
``` | ||
src/ | ||
├── components/ | ||
│ ├── IdTokenExample.tsx # Authentication example | ||
│ └── CollectionQueryExample.tsx # Firestore example | ||
├── App.tsx # Main app with routing | ||
├── firebase.ts # Firebase initialization | ||
├── main.tsx # Entry point | ||
└── index.css # Tailwind CSS | ||
``` | ||
|
||
## Technologies Used | ||
|
||
- **Vite**: Fast build tool and dev server | ||
- **React Router**: Client-side routing | ||
- **TanStack Query**: Data fetching and caching | ||
- **Firebase**: Authentication and Firestore | ||
- **Tailwind CSS**: Utility-first styling | ||
- **TypeScript**: Type safety |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; | ||
import { useState } from "react"; | ||
import { Link, Route, Routes, useLocation } from "react-router-dom"; | ||
import { CollectionQueryExample } from "./components/CollectionQueryExample"; | ||
import { IdTokenExample } from "./components/IdTokenExample"; | ||
import { NestedCollectionsExample } from "./components/NestedCollectionsExample"; | ||
import { WithConverterExample } from "./components/WithConverterExample"; | ||
|
||
import "./firebase"; | ||
|
||
function App() { | ||
const [queryClient] = useState( | ||
() => | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 60 * 1000, | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<div className="min-h-screen bg-gray-50"> | ||
<Navigation /> | ||
<div className="py-12"> | ||
<div className="max-w-6xl mx-auto px-4"> | ||
<Routes> | ||
<Route path="/" element={<Home />} /> | ||
<Route path="/auth/id-token" element={<IdTokenExample />} /> | ||
<Route | ||
path="/firestore/collection-query" | ||
element={<CollectionQueryExample />} | ||
/> | ||
<Route | ||
path="/nested-collections" | ||
element={<NestedCollectionsExample />} | ||
/> | ||
<Route | ||
path="/typescript-safety" | ||
element={<WithConverterExample />} | ||
/> | ||
</Routes> | ||
</div> | ||
</div> | ||
</div> | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</QueryClientProvider> | ||
); | ||
} | ||
|
||
function Navigation() { | ||
const location = useLocation(); | ||
|
||
const isActive = (path: string) => location.pathname === path; | ||
|
||
return ( | ||
<nav className="bg-white shadow-sm border-b"> | ||
<div className="max-w-6xl mx-auto px-4"> | ||
<div className="flex items-center justify-between h-16"> | ||
<div className="flex items-center space-x-8"> | ||
<Link | ||
to="/" | ||
className={`text-lg font-semibold ${ | ||
isActive("/") ? "text-blue-600" : "text-gray-900" | ||
}`} | ||
> | ||
TanStack Query Firebase Examples | ||
</Link> | ||
</div> | ||
<div className="flex items-center space-x-6"> | ||
<Link | ||
to="/auth/id-token" | ||
className={`text-sm font-medium ${ | ||
isActive("/auth/id-token") | ||
? "text-blue-600" | ||
: "text-gray-500 hover:text-gray-700" | ||
}`} | ||
> | ||
Auth: ID Token | ||
</Link> | ||
<Link | ||
to="/firestore/collection-query" | ||
className={`text-sm font-medium ${ | ||
isActive("/firestore/collection-query") | ||
? "text-blue-600" | ||
: "text-gray-500 hover:text-gray-700" | ||
}`} | ||
> | ||
Firestore: Collection Query | ||
</Link> | ||
<Link | ||
to="/nested-collections" | ||
className={`text-sm font-medium ${ | ||
isActive("/nested-collections") | ||
? "text-blue-600" | ||
: "text-gray-500 hover:text-gray-700" | ||
}`} | ||
> | ||
Nested Collections | ||
</Link> | ||
<Link | ||
to="/typescript-safety" | ||
className={`text-sm font-medium ${ | ||
isActive("/typescript-safety") | ||
? "text-blue-600" | ||
: "text-gray-500 hover:text-gray-700" | ||
}`} | ||
> | ||
TypeScript Safety | ||
</Link> | ||
</div> | ||
</div> | ||
</div> | ||
</nav> | ||
); | ||
} | ||
|
||
function Home() { | ||
return ( | ||
<div className="text-center"> | ||
<h1 className="text-4xl font-bold text-gray-900 mb-6"> | ||
TanStack Query Firebase Examples | ||
</h1> | ||
<p className="text-xl text-gray-600 mb-8"> | ||
Explore different patterns and use cases for Firebase with TanStack | ||
Query | ||
</p> | ||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6"> | ||
<Link | ||
to="/auth/id-token" | ||
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow" | ||
> | ||
<h2 className="text-lg font-semibold text-gray-900 mb-2"> | ||
Auth: ID Token | ||
</h2> | ||
<p className="text-gray-600 text-sm"> | ||
Get and refresh Firebase ID tokens with proper caching | ||
</p> | ||
</Link> | ||
<Link | ||
to="/firestore/collection-query" | ||
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow" | ||
> | ||
<h2 className="text-lg font-semibold text-gray-900 mb-2"> | ||
Firestore: Collection Query | ||
</h2> | ||
<p className="text-gray-600 text-sm"> | ||
Query Firestore collections with filtering and mutations | ||
</p> | ||
</Link> | ||
<Link | ||
to="/nested-collections" | ||
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow" | ||
> | ||
<h2 className="text-lg font-semibold text-gray-900 mb-2"> | ||
Nested Collections | ||
</h2> | ||
<p className="text-gray-600 text-sm"> | ||
Handle nested Firestore collections with real-time updates | ||
</p> | ||
</Link> | ||
<Link | ||
to="/typescript-safety" | ||
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow" | ||
> | ||
<h2 className="text-lg font-semibold text-gray-900 mb-2"> | ||
TypeScript Safety | ||
</h2> | ||
<p className="text-gray-600 text-sm"> | ||
Resolve DocumentData vs custom interface type issues | ||
</p> | ||
</Link> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script path references 'firebase-examples' but the directory is actually 'kitchen-sink'. This will cause the emulator script to fail when trying to change to the correct directory.
Copilot uses AI. Check for mistakes.