Add guards

This commit is contained in:
Alexander Farber
2025-12-13 09:53:05 +01:00
parent f5c868702b
commit e76f47512c
3 changed files with 22 additions and 7 deletions

View File

@@ -41,7 +41,7 @@ export function ResumeSessionDialog({
: 5; : 5;
const picker = useSessionPicker({ const picker = useSessionPicker({
sessionService: sessionServiceRef.current!, sessionService: sessionServiceRef.current,
currentBranch, currentBranch,
onSelect, onSelect,
onCancel, onCancel,

View File

@@ -23,7 +23,7 @@ export interface SessionState {
} }
export interface UseSessionPickerOptions { export interface UseSessionPickerOptions {
sessionService: SessionService; sessionService: SessionService | null;
currentBranch?: string; currentBranch?: string;
onSelect: (sessionId: string) => void; onSelect: (sessionId: string) => void;
onCancel: () => void; onCancel: () => void;
@@ -92,7 +92,9 @@ export function useSessionPicker({
// Calculate scroll offset based on mode // Calculate scroll offset based on mode
const scrollOffset = centerSelection const scrollOffset = centerSelection
? (() => { ? (() => {
if (filteredSessions.length <= maxVisibleItems) return 0; if (filteredSessions.length <= maxVisibleItems) {
return 0;
}
const halfVisible = Math.floor(maxVisibleItems / 2); const halfVisible = Math.floor(maxVisibleItems / 2);
let offset = selectedIndex - halfVisible; let offset = selectedIndex - halfVisible;
offset = Math.max(0, offset); offset = Math.max(0, offset);
@@ -111,6 +113,11 @@ export function useSessionPicker({
// Load initial sessions // Load initial sessions
useEffect(() => { useEffect(() => {
// Guard: don't load if sessionService is not ready
if (!sessionService) {
return;
}
const loadInitialSessions = async () => { const loadInitialSessions = async () => {
try { try {
const result: ListSessionsResult = await sessionService.listSessions({ const result: ListSessionsResult = await sessionService.listSessions({
@@ -130,7 +137,9 @@ export function useSessionPicker({
// Load more sessions // Load more sessions
const loadMoreSessions = useCallback(async () => { const loadMoreSessions = useCallback(async () => {
if (!sessionState.hasMore || isLoadingMoreRef.current) return; if (!sessionService || !sessionState.hasMore || isLoadingMoreRef.current) {
return;
}
isLoadingMoreRef.current = true; isLoadingMoreRef.current = true;
try { try {
@@ -229,7 +238,9 @@ export function useSessionPicker({
// Navigation down // Navigation down
if (key.downArrow || input === 'j') { if (key.downArrow || input === 'j') {
if (filteredSessions.length === 0) return; if (filteredSessions.length === 0) {
return;
}
setSelectedIndex((prev) => { setSelectedIndex((prev) => {
const newIndex = Math.min(filteredSessions.length - 1, prev + 1); const newIndex = Math.min(filteredSessions.length - 1, prev + 1);

View File

@@ -15,8 +15,12 @@ export const SESSION_PAGE_SIZE = 20;
* Truncates text to fit within a given width, adding ellipsis if needed. * Truncates text to fit within a given width, adding ellipsis if needed.
*/ */
export function truncateText(text: string, maxWidth: number): string { export function truncateText(text: string, maxWidth: number): string {
if (text.length <= maxWidth) return text; if (text.length <= maxWidth) {
if (maxWidth <= 3) return text.slice(0, maxWidth); return text;
}
if (maxWidth <= 3) {
return text.slice(0, maxWidth);
}
return text.slice(0, maxWidth - 3) + '...'; return text.slice(0, maxWidth - 3) + '...';
} }