feat: enhance useMoveTo hook with focused index and query state management

This commit is contained in:
Aj Wazzan
2025-05-04 12:34:14 -07:00
parent f9a752f98d
commit e207c3bf9b

View File

@@ -1,10 +1,13 @@
import { moveThreadsTo, type MoveThreadOptions } from '@/lib/thread-actions';
import useBackgroundQueue from '@/hooks/ui/use-background-queue';
import { focusedIndexAtom } from '../use-mail-navigation';
import { useMail } from '@/components/mail/use-mail';
import { useThreads } from '@/hooks/use-threads';
import { useStats } from '@/hooks/use-stats';
import { useTranslations } from 'next-intl';
import { useQueryState } from 'nuqs';
import { useState } from 'react';
import { useAtom } from 'jotai';
import { toast } from 'sonner';
const useMoveTo = () => {
@@ -14,6 +17,9 @@ const useMoveTo = () => {
const { refetch: refetchStats } = useStats();
const [mail, setMail] = useMail();
const { addToQueue, deleteFromQueue } = useBackgroundQueue();
const [, setFocusedIndex] = useAtom(focusedIndexAtom);
const [, setThreadId] = useQueryState('threadId');
const [, setActiveReplyId] = useQueryState('activeReplyId');
const getCopyByDestination = (to?: MoveThreadOptions['destination']) => {
switch (to) {
@@ -59,6 +65,9 @@ const useMoveTo = () => {
for (const threadId of threadIds) {
addToQueue(threadId);
}
setThreadId(null);
setActiveReplyId(null);
setFocusedIndex(null);
return toast.promise(promise, {
...getCopyByDestination(destination),
error: (error) => {
@@ -68,10 +77,10 @@ const useMoveTo = () => {
},
finally: async () => {
setIsLoading(false);
await Promise.all([refetchThreads(), refetchStats()]);
for (const threadId of threadIds) {
deleteFromQueue(threadId);
}
await Promise.all([refetchThreads(), refetchStats()]);
setMail({
...mail,
bulkSelected: [],