Skip to content

SANC-108: Made agency name dynamic - #78

Open
jason-duong4509 wants to merge 2 commits into
mainfrom
SANC-108-make-agency-name-dynamic
Open

SANC-108: Made agency name dynamic#78
jason-duong4509 wants to merge 2 commits into
mainfrom
SANC-108-make-agency-name-dynamic

Conversation

@jason-duong4509

@jason-duong4509 jason-duong4509 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Added a new endpoint, getUserOrgName, to return a user's organization name from the DB. Used the new endpoint to return the user's organization name to the client, and had that info displayed in the nav bar

video.mp4

Summary by CodeRabbit

  • Improvements
    • Enhanced logout experience by ensuring cached user data is properly cleared, providing a clean state for subsequent login sessions.

@vercel

vercel Bot commented Apr 14, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
salvationarmy Ready Ready Preview, Comment Apr 16, 2026 3:18am

@coderabbitai

coderabbitai Bot commented Apr 14, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR refactors how the Navbar component obtains the agency name by moving data fetching from prop-based injection to internal TRPC query execution. It introduces a new protected server procedure getUserOrgName that retrieves the active organization's name from the database, while the Profile component clears the React Query cache on logout.

Changes

Cohort / File(s) Summary
Navbar Component Refactoring
src/app/_components/common/navbar.tsx
Removes agencyName from props; component now self-fetches organization name via protected getUserOrgName query, showing loading state while data is unavailable.
Organization API Enhancement
src/server/api/routers/organizations.ts
Adds new getUserOrgName protected query procedure that retrieves the active organization's name with error handling for missing organization ID or non-existent organization records.
Logout Cache Management
src/app/_components/common/profile/profile.tsx
Integrates useQueryClient hook to clear React Query's cached data upon logout completion.

Sequence Diagram(s)

sequenceDiagram
    participant Navbar as Navbar Component
    participant TRPC as TRPC Client
    participant Server as API Server
    participant DB as Database

    Note over Navbar: view === "agency"
    Navbar->>TRPC: useQuery("getUserOrgName", enabled: true)
    TRPC->>Server: protectedProcedure.query()
    Server->>Server: Verify activeOrganizationId exists
    Server->>DB: findFirst(id: activeOrganizationId)
    DB-->>Server: organization record
    Server-->>TRPC: org.name
    TRPC-->>Navbar: agencyName data
    Navbar->>Navbar: Render "[agencyName] Home"
Loading
sequenceDiagram
    participant User as User
    participant Profile as Profile Component
    participant Router as Next Router
    participant QC as React Query Client

    User->>Profile: Click logout
    Profile->>Router: router.push("/logout")
    Router-->>Profile: Navigation complete
    Profile->>QC: queryClient.clear()
    QC->>QC: Purge all cached queries
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • JustinTan-1
  • themaxboucher
  • burtonjong
  • Yemyam
  • tanvimahal
  • wesleylui
  • Lujarios

Poem

🐰 Hops with glee:
The navbar now fetches its own delight,
No props need to pass through the night,
A query from server, the org name so bright,
The cache clears on logout—pure might!
Data flows freely, the pattern's just right! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: making the agency name dynamic by fetching it from a backend endpoint instead of passing it as a static prop.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch SANC-108-make-agency-name-dynamic

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/app/_components/common/navbar.tsx (1)

41-58: ⚠️ Potential issue | 🟠 Major

Gate org-name query to agency view and separate loading vs error UI states.

At line 41, the query runs unconditionally for all navbar variants (admin, agency, driver), but agencyName is only used when view === "agency". This triggers unnecessary API requests in admin and driver pages. Additionally, line 58 shows "Loading..." for both loading and error states, making network failures indistinguishable from genuine loading.

The suggested fix uses the enabled option (supported by @tanstack/react-query 5.69.0 in this project) to gate the query and extracts isError to differentiate failure states:

Suggested fix
-  const agencyName = api.organization.getUserOrgName.useQuery().data;
+  const {
+    data: agencyName,
+    isLoading: isAgencyNameLoading,
+    isError: isAgencyNameError,
+  } = api.organization.getUserOrgName.useQuery(undefined, {
+    enabled: view === "agency",
+  });
@@
-        <Text>{view === "agency" ? (agencyName ? `${agencyName} Home` : "Loading...") : ""}</Text>
+        <Text>
+          {view === "agency"
+            ? isAgencyNameLoading
+              ? "Loading..."
+              : isAgencyNameError
+                ? "Agency Home"
+                : `${agencyName} Home`
+            : ""}
+        </Text>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/_components/common/navbar.tsx` around lines 41 - 58, The navbar
always runs api.organization.getUserOrgName.useQuery() even when view !==
"agency" and conflates loading/error UI; update the hook call to const { data:
agencyName, isLoading, isError } =
api.organization.getUserOrgName.useQuery(undefined, { enabled: view === "agency"
}) so the query only runs for the agency view, then change the Text rendering
logic (where view === "agency") to show a distinct error state when isError
(e.g., "Error loading name") vs a loading state when isLoading ("Loading...")
and otherwise display `${agencyName} Home`.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/app/_components/common/navbar.tsx`:
- Around line 41-58: The navbar always runs
api.organization.getUserOrgName.useQuery() even when view !== "agency" and
conflates loading/error UI; update the hook call to const { data: agencyName,
isLoading, isError } = api.organization.getUserOrgName.useQuery(undefined, {
enabled: view === "agency" }) so the query only runs for the agency view, then
change the Text rendering logic (where view === "agency") to show a distinct
error state when isError (e.g., "Error loading name") vs a loading state when
isLoading ("Loading...") and otherwise display `${agencyName} Home`.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 52fe48bd-d27b-4128-b98a-7ea95eba0e52

📥 Commits

Reviewing files that changed from the base of the PR and between 596e2cd and b6c61e6.

📒 Files selected for processing (2)
  • src/app/_components/common/navbar.tsx
  • src/server/api/routers/organizations.ts

- Added a new endpoint, getUserOrgName, to return a user's organization
  name from the DB
- Used the new endpoint to return the user's organization name to the
  client, and had that info displayed in the nav bar

@promatty promatty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i found this bug:

  1. log in as agency one
  2. log out. log in as agency three
    shows agency one as the name still
Image

- Added client cache clearing when the user logs out to ensure that data
  between accounts aren't saved and reused upon visiting the same page

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/app/_components/common/profile/profile.tsx (1)

22-23: Use router.replace("/logout") in the logout flow.

push adds a history entry, allowing users to back-navigate to authenticated pages, which is a security and UX concern. replace removes the current entry from history, preventing back-button access to protected routes after logout.

Suggested change
-        router.push("/logout");
+        router.replace("/logout");
         queryClient.clear();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/_components/common/profile/profile.tsx` around lines 22 - 23, Replace
the history-pushing navigation call with a history-replacing one in the logout
flow: change router.push("/logout") to router.replace("/logout") (leave
queryClient.clear() as-is) so the current entry is replaced and users cannot
back-navigate to authenticated pages; update any logout handler in this
component (the code invoking router.push and queryClient.clear, e.g., the logout
function in profile.tsx) accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/app/_components/common/profile/profile.tsx`:
- Around line 22-23: Replace the history-pushing navigation call with a
history-replacing one in the logout flow: change router.push("/logout") to
router.replace("/logout") (leave queryClient.clear() as-is) so the current entry
is replaced and users cannot back-navigate to authenticated pages; update any
logout handler in this component (the code invoking router.push and
queryClient.clear, e.g., the logout function in profile.tsx) accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c53062ec-9568-4594-8016-417f62dcc238

📥 Commits

Reviewing files that changed from the base of the PR and between b6c61e6 and 7122c73.

📒 Files selected for processing (3)
  • src/app/_components/common/navbar.tsx
  • src/app/_components/common/profile/profile.tsx
  • src/server/api/routers/organizations.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/server/api/routers/organizations.ts
  • src/app/_components/common/navbar.tsx

@jason-duong4509

Copy link
Copy Markdown
Contributor Author

i found this bug:

Thanks for catching it! It turns out that the client's browser was caching data and reusing it briefly upon reentering the same page. Both the schedule data and organization name were stale and reused from a previous session

Going forward, upon logging out, the client's browser clears its cache so that reentering the same page as a different user will trigger a refetch of data. Before, this occurred only when the window was focused

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants