-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(mail): inbox selection checkbox and Empty Bin/Spam actions #1561
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useState, useCallback } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toast } from 'sonner'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Button } from '@/components/ui/button'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Trash } from '@/components/icons/icons'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { trpcClient } from '@/providers/query-provider'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useOptimisticActions } from '@/hooks/use-optimistic-actions'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface Props { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A small utility component to permanently delete all emails in the currently opened folder (spam / bin) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function EmptyFolderButton({ folder }: Props) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { optimisticDeleteThreads, optimisticMoveThreadsTo } = useOptimisticActions(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [isLoading, setIsLoading] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handleEmptyFolder = useCallback(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isLoading) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const confirmText = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder === 'spam' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? 'This will delete all emails in the Spam folder and move them to Bin. Continue?' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : 'This will permanently delete all emails in the Bin folder. Continue?'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!window.confirm(confirmText)) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsLoading(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ids: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let cursor = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const MAX_PER_PAGE = 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (true) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fetch mails in pages of 100 until there is no nextPageToken | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const page = await trpcClient.mail.listThreads.query({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max: MAX_PER_PAGE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cursor, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } as any); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove unsafe type assertion. The Define a proper type for the query parameters: +interface ListThreadsParams {
+ folder: string;
+ q: string;
+ max: number;
+ cursor: string;
+}
- const page = await trpcClient.mail.listThreads.query({
- folder,
- q: '',
- max: MAX_PER_PAGE,
- cursor,
- } as any);
+ const page = await trpcClient.mail.listThreads.query({
+ folder,
+ q: '',
+ max: MAX_PER_PAGE,
+ cursor,
+ } satisfies ListThreadsParams);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (page?.threads?.length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ids.push(...page.threads.map((t: { id: string }) => t.id)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!page?.nextPageToken) break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cursor = page.nextPageToken; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add safety mechanism for pagination loop. The while loop could theoretically run indefinitely if the backend returns inconsistent pagination data, leading to an infinite loop. Add a safety counter to prevent infinite loops: const ids: string[] = [];
let cursor = '';
const MAX_PER_PAGE = 100;
+ const MAX_PAGES = 1000; // Safety limit
+ let pageCount = 0;
while (true) {
+ if (++pageCount > MAX_PAGES) {
+ console.warn('Maximum page limit reached, stopping pagination');
+ break;
+ }
// Fetch mails in pages of 100 until there is no nextPageToken📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!ids.length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| toast.success('Folder already empty'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (folder === 'spam') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Move all spam to bin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| optimisticMoveThreadsTo(ids, folder, 'bin'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Permanently delete everything from bin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| optimisticDeleteThreads(ids, folder); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error: any) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error('Failed to empty folder', error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| toast.error(error?.message ?? 'Failed to empty folder'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setIsLoading(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [folder, isLoading, optimisticDeleteThreads, optimisticMoveThreadsTo]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size="sm" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| variant="destructive" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| disabled={isLoading} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onClick={handleEmptyFolder} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className="px-4 flex items-center justify-center" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {folder === 'spam' ? 'Empty Spam' : 'Empty Bin'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,4 +36,4 @@ services: | |
|
|
||
| volumes: | ||
| valkey-data: | ||
| postgres-data: | ||
| postgres-data: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing newline at end of file. The YAMLlint tool correctly identified that the file should end with a newline character for POSIX compliance and better Git handling. Apply this diff to fix the formatting issue: - postgres-data:
+ postgres-data:
🧰 Tools🪛 YAMLlint (1.37.1)[error] 39-39: no new line character at the end of file (new-line-at-end-of-file) 🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation and rate limiting.
The function lacks folder validation and protection against rapid successive clicks.
Add proper validation and debouncing:
const handleEmptyFolder = useCallback(async () => { - if (isLoading) return; + if (isLoading || !['spam', 'bin'].includes(folder)) return; const confirmText = folder === 'spam' ? 'This will delete all emails in the Spam folder and move them to Bin. Continue?' : 'This will permanently delete all emails in the Bin folder. Continue?';📝 Committable suggestion
🤖 Prompt for AI Agents