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

Conversation

Sidd-R
Copy link
Contributor

@Sidd-R Sidd-R commented Mar 8, 2024

Summary

Filtered the array of folders for trash and deleted folders before taking its length. Earlier both were being considered which led to trash and deleted folders to be considered as active notebooks and the ui of an active notebook was being displayed. When there was an attempt to create a new note/todo list, the Folder.defaultFolder() returned null in screens/Notes.tsx and when its id was accessed, it gave cannot read property error.

Rationale

Filtered the folders using the following logic:

  • Used the folder id of trash folder to filter it out.
  • Filtered folders whose deleted time was not equal to 0 to remove deleted folders.

Now when there are no active notebooks, create new notebook option is shown and the floating button for creating new note/todo is hidden.

Before:
before_

After:
after_

Tested on Android 13

Link to my introduction in forum: - https://discourse.joplinapp.org/t/introducing-sidd-r/36498

Copy link
Contributor

github-actions bot commented Mar 8, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Sidd-R
Copy link
Contributor Author

Sidd-R commented Mar 8, 2024

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Mar 8, 2024
@Sidd-R
Copy link
Contributor Author

Sidd-R commented Mar 8, 2024

Hello, this is my first time contributing to Joplin and I have tried my best to follow the given guidelines as much as possible but incase if I missed anything do let me know! Also, any tips to improve my pr would be appreciated!

@Sidd-R Sidd-R marked this pull request as ready for review March 8, 2024 02:59
@Sidd-R
Copy link
Contributor Author

Sidd-R commented Mar 8, 2024

@PackElend label me please

if (!this.props.folders.length) {
// calculate number of folders except deleted and trash folder
// trash folder id: de1e7ede1e7ede1e7ede1e7ede1e7ede
const atleastOneNotebookExists = this.props.folders.filter((folder: FolderEntity) => folder.id !== 'de1e7ede1e7ede1e7ede1e7ede1e7ede' && folder.deleted_time === 0).length > 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider using getTrashFolderId here instead of de1e7ede1e7ede1e7ede1e7ede1e7ede. (getTrashFolderId is defined here).

Thank you for working on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I have made the suggested changes and updated the pr. Do let me know if any other changes are needed!

if (!this.props.folders.length) {
// calculate number of folders except deleted and trash folder
// trash folder id: de1e7ede1e7ede1e7ede1e7ede1e7ede
const atleastOneNotebookExists = this.props.folders.filter((folder: FolderEntity) => folder.id !== 'de1e7ede1e7ede1e7ede1e7ede1e7ede' && folder.deleted_time === 0).length > 0;
Copy link
Collaborator

@roman-r-m roman-r-m Mar 8, 2024

Choose a reason for hiding this comment

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

Will this not cause the same error as #10073 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, it is not causing that error as of now, I don't get how the changes I have made can possibly cause that error. Can you please elaborate on it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not saying your changes caused that issue, of course. I mean if you have a conflicts folder, won't your code have the same errors as the issue I linked.

Copy link
Contributor Author

@Sidd-R Sidd-R Mar 8, 2024

Choose a reason for hiding this comment

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

Oh, got it. Then I'll add another condition to check whether the folder id matches with the conflict folder id before checking for the deleted_time attribute. This should resolve it I guess, correct me if I'm wrong.

I can use the conflictFolderId() function to get its id.

OR

maybe we can add deleted_time attribute to the conflicts folder object, that would fix #10073 as well.

before:

public static conflictFolder(): FolderEntity {
		const now = Date.now();

		return {
			type_: this.TYPE_FOLDER,
			id: this.conflictFolderId(),
			parent_id: '',
			title: this.conflictFolderTitle(),
			updated_time: now,
			user_updated_time: now,
			share_id: '',
			is_shared: 0,
		};
	}

after:

public static conflictFolder(): FolderEntity {
		const now = Date.now();

		return {
			type_: this.TYPE_FOLDER,
			id: this.conflictFolderId(),
			parent_id: '',
			title: this.conflictFolderTitle(),
			updated_time: now,
			user_updated_time: now,
			share_id: '',
			is_shared: 0,
                     +  deleted_time: 0,
		};
	}

const trashFolderId = getTrashFolderId();

// calculate number of folders except deleted and trash folder
const atleastOneNotebookExists = this.props.folders.filter((folder: FolderEntity) => folder.id !== trashFolderId && folder.deleted_time === 0).length > 0;
Copy link
Owner

Choose a reason for hiding this comment

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

Please use the term "folder" (not "notebook") in code

const trashFolderId = getTrashFolderId();

// calculate number of folders except deleted and trash folder
const atleastOneNotebookExists = this.props.folders.filter((folder: FolderEntity) => folder.id !== trashFolderId && folder.deleted_time === 0).length > 0;
Copy link
Owner

Choose a reason for hiding this comment

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

Since you use the same code in two places please make it a function that takes folders as input and returns a boolean. You can put it in models/Folder.ts

Also name it something like atLeastOneRealFolderExists since the trash folder is a virtual one

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 made the changes you suggested and have added unit test for the same.

@@ -353,4 +353,17 @@ describe('models/Folder', () => {
await expectThrow(async () => Folder.delete(folder2.id, { toTrash: true, toTrashParentId: folder2.id }));
});

it('should tell if atleast one folder other than trash and deleted exists', async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

Should be "at least" (not sure why "atleast" was in our dictionary but I removed it now)

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.

@laurent22
Copy link
Owner

Looks good now, thanks @Sidd-R!

@laurent22 laurent22 merged commit da393f6 into laurent22:dev Mar 11, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mobile: New note button throws exception on first start in dev mode when there are no notebooks
5 participants