Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile: Fixes #10065 filtered deleted and trash folder when counting active notebooks #10087

Merged
merged 8 commits into from Mar 11, 2024
4 changes: 3 additions & 1 deletion packages/app-mobile/components/NoteList.tsx
Expand Up @@ -6,6 +6,7 @@ import { connect } from 'react-redux';
import { FlatList, Text, StyleSheet, Button, View } from 'react-native';
import { FolderEntity, NoteEntity } from '@joplin/lib/services/database/types';
import { AppState } from '../utils/types';
import Folder from '@joplin/lib/models/Folder';

const { _ } = require('@joplin/lib/locale');
const { NoteItem } = require('./note-item.js');
Expand Down Expand Up @@ -90,7 +91,7 @@ class NoteListComponent extends Component<NoteListProps> {
keyExtractor={item => item.id}
/>;
} else {
if (!this.props.folders.length) {
if (!Folder.atLeastOneRealFolderExists(this.props.folders)) {
const noItemMessage = _('You currently have no notebooks.');
return (
<View style={this.styles().noNotebookView}>
Expand All @@ -106,6 +107,7 @@ class NoteListComponent extends Component<NoteListProps> {
}
}


const NoteList = connect((state: AppState) => {
return {
items: state.notes,
Expand Down
2 changes: 1 addition & 1 deletion packages/app-mobile/components/screens/Notes.tsx
Expand Up @@ -238,7 +238,7 @@ class NotesScreenComponent extends BaseScreenComponent<any> {
const thisComp = this;

const makeActionButtonComp = () => {
if (this.props.notesParentType === 'Folder' && itemIsInTrash(parent)) return null;
if ((this.props.notesParentType === 'Folder' && itemIsInTrash(parent)) || !Folder.atLeastOneRealFolderExists(this.props.folders)) return null;

const getTargetFolderId = async () => {
if (!buttonFolderId && isAllNotes) {
Expand Down
16 changes: 16 additions & 0 deletions packages/lib/models/Folder.test.ts
Expand Up @@ -353,4 +353,20 @@ describe('models/Folder', () => {
await expectThrow(async () => Folder.delete(folder2.id, { toTrash: true, toTrashParentId: folder2.id }));
});

it('should tell if at least one folder other than trash and deleted exists', async () => {
let folders: FolderEntity[] = [];
expect(Folder.atLeastOneRealFolderExists(folders)).toBe(false);

folders = await Folder.all({ includeTrash: true });
expect(Folder.atLeastOneRealFolderExists(folders)).toBe(false);

const f1 = await Folder.save({ title: 'folder1' });
folders = await Folder.all({ includeTrash: true });
expect(Folder.atLeastOneRealFolderExists(folders)).toBe(true);

await Folder.delete(f1.id, { toTrash: true });
folders = await Folder.all({ includeTrash: true });
expect(Folder.atLeastOneRealFolderExists(folders)).toBe(false);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the tests. Also please add a case for testing an empty array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the test case for testing an empty array, please check.


});
6 changes: 6 additions & 0 deletions packages/lib/models/Folder.ts
Expand Up @@ -934,4 +934,10 @@ export default class Folder extends BaseItem {
return !!folders.find(f => !!f.icon);
}

public static atLeastOneRealFolderExists(folders: FolderEntity[]) {
// returns true if at least one folder exists other than trash folder and deleted folders
const trashFolderId = getTrashFolderId();
return folders.filter((folder) => folder.id !== trashFolderId && folder.deleted_time === 0).length > 0;
}

}