feat: add message action hooks with TDD
Implement useMessageActions and useMessageUnreadCount hooks following strict TDD principles (RED → GREEN → REFACTOR). useMessageActions provides: - markRead(id) - mark single message as read - batchMarkRead(ids) - batch mark messages as read - deleteMessage(id) - delete message - Independent loading/error states for each operation useMessageUnreadCount provides: - Fetch total unread count and counts by message type - refetch() method for manual refresh - loading and error state management All operations use MessageController from SDK with proper error handling. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
63
hooks/use-message-actions.ts
Normal file
63
hooks/use-message-actions.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { root } from '@repo/core'
|
||||
import { MessageController } from '@repo/sdk'
|
||||
import { useState } from 'react'
|
||||
|
||||
export const useMessageActions = () => {
|
||||
const [markReadLoading, setMarkReadLoading] = useState(false)
|
||||
const [markReadError, setMarkReadError] = useState<Error | null>(null)
|
||||
const [batchMarkReadLoading, setBatchMarkReadLoading] = useState(false)
|
||||
const [batchMarkReadError, setBatchMarkReadError] = useState<Error | null>(null)
|
||||
const [deleteLoading, setDeleteLoading] = useState(false)
|
||||
const [deleteError, setDeleteError] = useState<Error | null>(null)
|
||||
|
||||
const markRead = async (id: string) => {
|
||||
setMarkReadLoading(true)
|
||||
setMarkReadError(null)
|
||||
try {
|
||||
const messageController = root.get(MessageController)
|
||||
await messageController.markRead({ id })
|
||||
} catch (error) {
|
||||
setMarkReadError(error as Error)
|
||||
} finally {
|
||||
setMarkReadLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const batchMarkRead = async (ids: string[]) => {
|
||||
setBatchMarkReadLoading(true)
|
||||
setBatchMarkReadError(null)
|
||||
try {
|
||||
const messageController = root.get(MessageController)
|
||||
await messageController.batchMarkRead({ ids })
|
||||
} catch (error) {
|
||||
setBatchMarkReadError(error as Error)
|
||||
} finally {
|
||||
setBatchMarkReadLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const deleteMessage = async (id: string) => {
|
||||
setDeleteLoading(true)
|
||||
setDeleteError(null)
|
||||
try {
|
||||
const messageController = root.get(MessageController)
|
||||
await messageController.delete({ id })
|
||||
} catch (error) {
|
||||
setDeleteError(error as Error)
|
||||
} finally {
|
||||
setDeleteLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
markRead,
|
||||
markReadLoading,
|
||||
markReadError,
|
||||
batchMarkRead,
|
||||
batchMarkReadLoading,
|
||||
batchMarkReadError,
|
||||
deleteMessage,
|
||||
deleteLoading,
|
||||
deleteError,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user