Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/classes/list/class-list-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export function ClassListView({ classId, setClassId }: ClassListViewProps) {
if (queryTerm === "current") {
if (!selectedTermId && currentTerm) {
setSelectedTermId(currentTerm.id);
} else if (!selectedTermId && !isLoadingCurrentTerm && terms?.[0]) {
// No current term returned from backend; fall back to first term
setSelectedTermId(terms[0].id);
}
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/classes/list/components/term-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ export function TermSelect({
isRefetching,
} = clientApi.term.all.useQuery();

// If the current term is not in the list, select the first term
// If the selected term no longer exists (e.g. deleted), fall back to the first term.
// Only runs when selectedTermId is already set — initial selection is handled by class-list-view.
useEffect(() => {
const selectedTerm = terms?.find((t) => t.id === selectedTermId);
if (!selectedTerm && terms?.[0] && !isRefetching)
if (selectedTermId && !selectedTerm && terms?.[0] && !isRefetching)
setSelectedTermId(terms[0].id);
}, [terms, selectedTermId, setSelectedTermId]);
Comment on lines 38 to 42
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.

P2 Potential regression when no current term exists

The guard is correct for the described race condition, but it removes a safety net for a related edge case. When queryTerm === "current" but no term is marked as current (i.e., currentTerm is undefined), the effect in class-list-view.tsx (lines 112–116) never sets selectedTermId, leaving it as null indefinitely. Previously this useEffect would have rescued the UI by falling back to terms[0]; with selectedTermId && added, the guard prevents that fallback and the component renders a loading skeleton forever.

If it's always guaranteed that a current term exists when there are any terms, this is fine. Otherwise, consider adding a fallback in class-list-view.tsx for when queryTerm === "current" but currentTerm is absent:

// In class-list-view.tsx, inside the sync effect (around line 112)
if (queryTerm === "current") {
  if (!selectedTermId && currentTerm) {
    setSelectedTermId(currentTerm.id);
  } else if (!selectedTermId && !isLoadingCurrentTerm && terms?.[0]) {
    // No current term configured; fall back to the first term
    setSelectedTermId(terms[0].id);
  }
  return;
}


Expand Down