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;
const picker = useSessionPicker({
sessionService: sessionServiceRef.current!,
sessionService: sessionServiceRef.current,
currentBranch,
onSelect,
onCancel,

View File

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