Skip to content

Commit 0a26610

Browse files
committed
fix(mobile): optimize home screen loading with counts endpoint
1 parent 7398682 commit 0a26610

4 files changed

Lines changed: 35 additions & 45 deletions

File tree

apps/mobile/v1/src/screens/FoldersScreen.tsx

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -131,60 +131,30 @@ export default function FoldersScreen() {
131131
if (!isRefresh) {
132132
setLoading(true);
133133
}
134-
const [foldersData, allNotes] = await Promise.all([
134+
135+
// Fetch folders and counts in parallel (optimized - no note data fetched)
136+
const [foldersData, noteCounts] = await Promise.all([
135137
api.getFolders(),
136-
api.getNotes() // Get all notes to calculate accurate counts
138+
api.getNoteCounts()
137139
]);
138140

139141
if (__DEV__) {
140-
console.log(`${isRefresh ? 'Refresh' : 'Initial'} load - Total notes: ${allNotes.length}`);
142+
console.log(`${isRefresh ? 'Refresh' : 'Initial'} load - Note counts:`, noteCounts);
141143
}
142144

143145
// Show only ROOT folders (no parentId) on main screen
144-
const rootFolders = foldersData.filter(folder => !folder.parentId);
145-
146-
// Calculate note counts for each root folder including subfolders
147-
const getFolderAndSubfolderIds = (folderId: string): string[] => {
148-
const ids = [folderId];
149-
const subfolders = foldersData.filter(f => f.parentId === folderId);
150-
subfolders.forEach(subfolder => {
151-
ids.push(...getFolderAndSubfolderIds(subfolder.id));
152-
});
153-
return ids;
154-
};
155-
156-
// Add note counts to root folders (including subfolder notes)
157-
const rootFoldersWithCounts = rootFolders.map(folder => {
158-
const allFolderIds = getFolderAndSubfolderIds(folder.id);
159-
const folderNotes = allNotes.filter(note =>
160-
allFolderIds.includes(note.folderId || '') &&
161-
!note.deleted
162-
);
163-
return {
164-
...folder,
165-
noteCount: folderNotes.length
166-
};
167-
});
168-
169-
setAllFolders(rootFoldersWithCounts);
170-
171-
// Calculate accurate counts from all notes - using web app field names
172-
const trashedNotes = allNotes.filter(note => note.deleted);
173-
const archivedNotes = allNotes.filter(note => note.archived && !note.deleted);
174-
const starredNotes = allNotes.filter(note => note.starred && !note.deleted && !note.archived);
175-
const activeNotes = allNotes.filter(note => !note.deleted && !note.archived);
176-
177-
const newCounts = {
178-
all: activeNotes.length,
179-
starred: starredNotes.length,
180-
archived: archivedNotes.length,
181-
trash: trashedNotes.length,
182-
};
146+
// Note: For now, we don't show per-folder counts on home screen
147+
// Could be added later with a separate API endpoint
148+
const rootFolders = foldersData
149+
.filter(folder => !folder.parentId)
150+
.map(folder => ({ ...folder, noteCount: 0 }));
151+
152+
setAllFolders(rootFolders);
153+
setCounts(noteCounts);
183154

184155
if (__DEV__) {
185-
console.log('Updated note counts:', newCounts, 'Total notes loaded:', allNotes.length);
156+
console.log('Updated note counts:', noteCounts);
186157
}
187-
setCounts(newCounts);
188158
} catch (error) {
189159
if (__DEV__) console.error('Failed to load folders data:', error);
190160
setAllFolders([]);

apps/mobile/v1/src/services/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type {
1414
Note,
1515
NoteQueryParams,
1616
EmptyTrashResponse,
17+
NoteCounts,
1718
ApiUser,
1819
ApiUserUsage,
1920
FileAttachment
@@ -46,6 +47,7 @@ export const useApiService = () => {
4647
hideNote: notesApi.hideNote,
4748
unhideNote: notesApi.unhideNote,
4849
emptyTrash: notesApi.emptyTrash,
50+
getNoteCounts: notesApi.getNoteCounts,
4951

5052
// File attachment methods
5153
pickFiles: notesApi.pickFiles,

apps/mobile/v1/src/services/api/notes.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { createHttpClient, AuthTokenGetter } from './client';
7-
import { Note, NoteQueryParams, NotesResponse, EmptyTrashResponse, FileAttachment } from './types';
7+
import { Note, NoteQueryParams, NotesResponse, EmptyTrashResponse, FileAttachment, NoteCounts } from './types';
88
import { decryptNote, decryptNotes, encryptNoteForApi, clearEncryptionCache } from './encryption';
99
import { fetchAllPages, createPaginationParams } from './utils/pagination';
1010
import { handleApiError } from './utils/errors';
@@ -237,6 +237,17 @@ export function createNotesApi(getToken: AuthTokenGetter, getUserId: () => strin
237237
}
238238
},
239239

240+
/**
241+
* Get note counts for home screen (optimized - no note data fetched)
242+
*/
243+
async getNoteCounts(): Promise<NoteCounts> {
244+
try {
245+
return await makeRequest<NoteCounts>('/notes/counts');
246+
} catch (error) {
247+
return handleApiError(error, 'getNoteCounts');
248+
}
249+
},
250+
240251
/**
241252
* Pick files from device
242253
*/

apps/mobile/v1/src/services/api/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ export interface EmptyTrashResponse {
9696
deletedCount: number;
9797
}
9898

99+
export interface NoteCounts {
100+
all: number;
101+
starred: number;
102+
archived: number;
103+
trash: number;
104+
}
105+
99106
export interface ApiUserUsage {
100107
storage: {
101108
totalBytes: number;

0 commit comments

Comments
 (0)