-
Notifications
You must be signed in to change notification settings - Fork 19
ECHO-276 Split query ts file v1 #236
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
227c8c2
Refactor component imports to use local hooks
ussaama a7ca6ff
Remove unused hooks and refactor query functions in query.ts
ussaama c9fee0f
Refactor query hooks and component imports for improved modularity
ussaama 1e6beb2
Refactor query.ts to remove unused imports and streamline code
ussaama 4d9023c
Merge remote-tracking branch 'origin/main' into dissect-query-ts-file
ussaama 55f7ba1
Remove unused hooks for fetching conversation insights and quotes fro…
ussaama 63387b2
Refactor hooks imports to improve module organization and clarity
ussaama 1a79516
Merge branch 'main' into dissect-query-ts-file
ussaama 500ae75
Refactor hook imports across components to enhance organization and c…
ussaama f77918b
Refactor conversation hooks for improved organization and functionality
ussaama 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,126 @@ | ||
| # Modular Component Pattern | ||
|
|
||
| ## Overview | ||
|
|
||
| The monolithic `frontend/src/lib/query.ts` file has been **dissected and removed**. We now follow a modular component pattern where each domain has its own directory with components, hooks, and utilities. | ||
|
|
||
| ## 🚨 Important Changes | ||
|
|
||
| - **`query.ts` no longer exists** - all functionality moved to modular directories | ||
| - **New import patterns** - hooks imported from component directories | ||
| - **Domain-based organization** - related functionality grouped together | ||
|
|
||
| ## Directory Structure | ||
|
|
||
| Each component domain follows this pattern: | ||
|
|
||
| ``` | ||
| frontend/src/components/{domain}/ | ||
| | | ||
| ├── ComponentName.tsx # React components | ||
| ├── hooks/ | ||
| │ └── index.ts # Domain-specific React Query hooks | ||
| └── utils/ | ||
| └── index.ts # Domain-specific utilities | ||
| ``` | ||
|
|
||
| ## Where to Find Functionality | ||
|
|
||
| ### Component Domains | ||
| - **Participant**: `@/components/participant/hooks` | ||
| - **Project**: `@/components/project/hooks` | ||
| - **Chat**: `@/components/chat/hooks` | ||
| - **Auth**: `@/components/auth/hooks` | ||
| and so on... | ||
|
|
||
| ## Import Patterns | ||
|
|
||
| ### ❌ Old Way (No longer works) | ||
| ```typescript | ||
| import { useProjectById } from '@/lib/query'; | ||
| ``` | ||
|
|
||
| ### ✅ New Way | ||
| ```typescript | ||
| import { useProjectById } from '@/components/project/hooks'; | ||
| ``` | ||
|
|
||
| ## Creating New Components | ||
|
|
||
| ### 1. Create Directory Structure | ||
| ```bash | ||
| frontend/src/components/{domain}/ | ||
| ├── hooks/ | ||
| │ └── index.ts | ||
| └── utils/ | ||
| └── index.ts | ||
| ``` | ||
|
|
||
| ### 2. Export Hooks | ||
| ```typescript | ||
| // frontend/src/components/{domain}/hooks/index.ts | ||
| export const useDomainQuery = () => { | ||
| // Implementation | ||
| }; | ||
|
|
||
| export const useDomainMutation = () => { | ||
| // Implementation | ||
| }; | ||
| ``` | ||
|
|
||
| ### 3. Import in Components | ||
| ```typescript | ||
| import { useDomainQuery, useDomainMutation } from '@/components/{domain}/hooks'; | ||
| ``` | ||
|
|
||
| ## Guidelines | ||
|
|
||
| ### 1. **Organization** | ||
| - Keep related functionality together | ||
| - Use descriptive names that indicate the domain | ||
| - Export all hooks from `hooks/index.ts` | ||
|
|
||
| ### 2. **File Naming** | ||
| - Components: `PascalCase.tsx` | ||
| - Hooks/Utils/Types: `index.ts` (within respective directories) | ||
|
|
||
| ### 3. **Code Structure** | ||
| - Group mutations and queries logically | ||
| - Add JSDoc comments for complex functions | ||
| - Follow existing patterns in the codebase | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### "Cannot find module '@/lib/query'" | ||
| - The file no longer exists | ||
| - Find the hook in the appropriate component directory | ||
| - Use the new import pattern | ||
|
|
||
| ### "Hook not found" | ||
| - Search the codebase for the hook name | ||
| - Check component directories for the relevant domain | ||
| - Look in shared library files | ||
|
|
||
| ### "Import path error" | ||
| - Ensure you're using the correct path format | ||
| - Check that the hook is exported from the index.ts file | ||
|
|
||
| ## Benefits | ||
|
|
||
| - **Better Organization**: Related functionality grouped together | ||
| - **Easier Navigation**: Find specific features quickly | ||
| - **Reduced Conflicts**: Less merge conflicts between developers | ||
| - **Clear Ownership**: Each domain has its own space | ||
| - **Improved Testing**: Test individual domains in isolation | ||
| - **Better Scalability**: Easy to add new domains | ||
|
|
||
| ## Migration Checklist | ||
|
|
||
| When working with existing code or creating new features: | ||
|
|
||
| - [ ] Check if functionality belongs in a specific component domain | ||
| - [ ] Use the appropriate import path for hooks | ||
| - [ ] Follow the established directory structure | ||
| - [ ] Keep related functionality grouped together | ||
| - [ ] Test that imports work correctly | ||
| - [ ] Verify that the component follows the modular pattern |
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
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,182 @@ | ||
| import { toast } from "@/components/common/Toaster"; | ||
| import { useI18nNavigate } from "@/hooks/useI18nNavigate"; | ||
| import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
| import { directus } from "@/lib/directus"; | ||
| import { | ||
| passwordRequest, | ||
| passwordReset, | ||
| readUser, | ||
| registerUser, | ||
| registerUserVerify, | ||
| } from "@directus/sdk"; | ||
| import { ADMIN_BASE_URL } from "@/config"; | ||
| import { throwWithMessage } from "../utils/errorUtils"; | ||
|
|
||
| export const useCurrentUser = () => | ||
| useQuery({ | ||
| queryKey: ["users", "me"], | ||
| queryFn: () => { | ||
| try { | ||
| return directus.request(readUser("me")); | ||
| } catch (error) { | ||
| return null; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| export const useResetPasswordMutation = () => { | ||
| const navigate = useI18nNavigate(); | ||
| return useMutation({ | ||
| mutationFn: async ({ | ||
| token, | ||
| password, | ||
| }: { | ||
| token: string; | ||
| password: string; | ||
| }) => { | ||
| try { | ||
| const response = await directus.request(passwordReset(token, password)); | ||
| return response; | ||
| } catch (e) { | ||
| throwWithMessage(e); | ||
| } | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success( | ||
| "Password reset successfully. Please login with new password.", | ||
| ); | ||
| navigate("/login"); | ||
| }, | ||
| onError: (e) => { | ||
| try { | ||
| toast.error(e.message); | ||
| } catch (e) { | ||
| toast.error("Error resetting password. Please contact support."); | ||
| } | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export const useRequestPasswordResetMutation = () => { | ||
| const navigate = useI18nNavigate(); | ||
| return useMutation({ | ||
| mutationFn: async (email: string) => { | ||
| try { | ||
| const response = await directus.request( | ||
| passwordRequest(email, `${ADMIN_BASE_URL}/password-reset`), | ||
| ); | ||
| return response; | ||
| } catch (e) { | ||
| throwWithMessage(e); | ||
| } | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success("Password reset email sent successfully"); | ||
| navigate("/check-your-email"); | ||
| }, | ||
| onError: (e) => { | ||
| toast.error(e.message); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export const useVerifyMutation = (doRedirect: boolean = true) => { | ||
ussaama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const navigate = useI18nNavigate(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: async (data: { token: string }) => { | ||
| try { | ||
| const response = await directus.request(registerUserVerify(data.token)); | ||
| return response; | ||
| } catch (e) { | ||
| throwWithMessage(e); | ||
| } | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success("Email verified successfully."); | ||
| if (doRedirect) { | ||
| setTimeout(() => { | ||
| // window.location.href = `/login?new=true`; | ||
| navigate(`/login?new=true`); | ||
| }, 4500); | ||
| } | ||
| }, | ||
| onError: (e) => { | ||
| toast.error(e.message); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export const useRegisterMutation = () => { | ||
| const navigate = useI18nNavigate(); | ||
| return useMutation({ | ||
| mutationFn: async (payload: Parameters<typeof registerUser>) => { | ||
| try { | ||
| const response = await directus.request(registerUser(...payload)); | ||
| return response; | ||
| } catch (e) { | ||
| try { | ||
| throwWithMessage(e); | ||
| } catch (inner) { | ||
| if (inner instanceof Error) { | ||
| if (inner.message === "You don't have permission to access this.") { | ||
| throw new Error( | ||
| "Oops! It seems your email is not eligible for registration at this time. Please consider joining our waitlist for future updates!", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success("Please check your email to verify your account."); | ||
| navigate("/check-your-email"); | ||
| }, | ||
| onError: (e) => { | ||
| toast.error(e.message); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| // todo: add redirection logic here | ||
| export const useLoginMutation = () => { | ||
| return useMutation({ | ||
| mutationFn: (payload: Parameters<typeof directus.login>) => { | ||
| return directus.login(...payload); | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success("Login successful"); | ||
| }, | ||
| }); | ||
| }; | ||
ussaama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const useLogoutMutation = () => { | ||
| const queryClient = useQueryClient(); | ||
| const navigate = useI18nNavigate(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: async ({ | ||
| next: _, | ||
| }: { | ||
| next?: string; | ||
| reason?: string; | ||
| doRedirect: boolean; | ||
| }) => { | ||
| try { | ||
| await directus.logout(); | ||
| } catch (e) { | ||
| throwWithMessage(e); | ||
| } | ||
| }, | ||
| onMutate: async ({ next, reason, doRedirect }) => { | ||
| queryClient.resetQueries(); | ||
| if (doRedirect) { | ||
| navigate( | ||
| "/login" + | ||
| (next ? `?next=${encodeURIComponent(next)}` : "") + | ||
| (reason ? `&reason=${reason}` : ""), | ||
| ); | ||
| } | ||
| }, | ||
| }); | ||
| }; | ||
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,22 @@ | ||
| // always throws a error with a message | ||
| export function throwWithMessage(e: unknown): never { | ||
| if ( | ||
| e && | ||
| typeof e === "object" && | ||
| "errors" in e && | ||
| Array.isArray((e as any).errors) | ||
| ) { | ||
| // Handle Directus error format | ||
| const message = (e as any).errors[0].message; | ||
| console.log(message); | ||
| throw new Error(message); | ||
| } else if (e instanceof Error) { | ||
| // Handle generic errors | ||
| console.log(e.message); | ||
| throw new Error(e.message); | ||
| } else { | ||
| // Handle unknown errors | ||
| console.log("An unknown error occurred"); | ||
| throw new Error("Something went wrong"); | ||
| } | ||
| } | ||
ussaama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.