Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ interface BoardContextType {
error: string | null;
isInitialized: boolean;
logbook: LogbookEntry[];
getLogbook: (climbUuids: ClimbUuid[]) => Promise<void>;
getLogbook: (climbUuids: ClimbUuid[]) => Promise<boolean>;
saveTick: (options: SaveTickOptions) => Promise<void>;
saveClimb: (options: Omit<SaveClimbOptions, 'setter_id'>) => Promise<SaveClimbResponse>;
}
Expand Down Expand Up @@ -167,16 +167,17 @@ export function BoardProvider({ boardName, children }: { boardName: BoardName; c
}, [boardName, sessionStatus]);

// Internal fetch function (not memoized, called by getLogbook and effect)
const fetchLogbook = async (climbUuids: ClimbUuid[]) => {
// Returns true if the fetch was successful, false if it was skipped or failed
const fetchLogbook = async (climbUuids: ClimbUuid[]): Promise<boolean> => {
if (sessionStatus !== 'authenticated') {
setLogbook([]);
return;
return false;
}

// CRITICAL: Wait for wsAuthToken to be available
if (!wsAuthToken) {
console.log('[fetchLogbook] Waiting for auth token...');
return; // Will be called again when wsAuthToken becomes available
return false; // Caller should retry when wsAuthToken becomes available
}

try {
Expand Down Expand Up @@ -215,17 +216,20 @@ export function BoardProvider({ boardName, children }: { boardName: BoardName; c
}));

setLogbook(entries);
return true;
} catch (err) {
console.error('Failed to fetch logbook:', err);
setLogbook([]);
return false;
}
};

// Fetch logbook from local ticks API (works without Aurora credentials)
const getLogbook = useCallback(async (climbUuids: ClimbUuid[]) => {
// Returns true if the fetch was successful, false if it was skipped or failed
const getLogbook = useCallback(async (climbUuids: ClimbUuid[]): Promise<boolean> => {
// Store the UUIDs in ref to avoid re-render loops
currentClimbUuidsRef.current = climbUuids;
await fetchLogbook(climbUuids);
return await fetchLogbook(climbUuids);
}, [boardName, sessionStatus, wsAuthToken]);

// Refetch logbook only when session status changes from non-authenticated to authenticated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ vi.mock('@/app/lib/url-utils', () => ({

vi.mock('../../../board-provider/board-provider-context', () => ({
useBoardProvider: vi.fn(() => ({
getLogbook: vi.fn()
getLogbook: vi.fn().mockResolvedValue(true)
}))
}));

Expand Down Expand Up @@ -129,7 +129,7 @@ const createWrapper = () => {

describe('useQueueDataFetching', () => {
const mockSetHasDoneFirstFetch = vi.fn();
const mockGetLogbook = vi.fn();
const mockGetLogbook = vi.fn().mockResolvedValue(true);

beforeEach(() => {
vi.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,13 @@ export const useQueueDataFetching = ({

const climbUuids = JSON.parse(climbUuidsString);
if (climbUuids.length > 0) {
getLogbook(climbUuids);
fetchedUuidsRef.current = climbUuidsString;
// Only mark as fetched if the fetch actually succeeded
// This ensures we retry when wsAuthToken becomes available
getLogbook(climbUuids).then((success) => {
if (success) {
fetchedUuidsRef.current = climbUuidsString;
}
});
}
}, [climbUuidsString, getLogbook]);

Expand Down
Loading