1
1
'use client'
2
2
3
- import { useInfiniteQuery , useQueryClient } from '@tanstack/react-query'
3
+ import {
4
+ useInfiniteQuery ,
5
+ useMutation ,
6
+ useQueryClient ,
7
+ } from '@tanstack/react-query'
4
8
import { useEffect , useMemo , useState } from 'react'
5
9
import clsx from 'clsx'
6
10
import { stagger , useAnimate } from 'framer-motion'
@@ -57,26 +61,29 @@ const PostBox = () => {
57
61
58
62
const [ value , setValue ] = useState ( '' )
59
63
const queryClient = useQueryClient ( )
60
- if ( ! isLogin ) return null
61
-
62
- const handleSend = ( ) => {
63
- apiClient . shorthand . proxy . post ( { data : { content : value } } ) . then ( ( res ) => {
64
- setValue ( '' )
65
-
66
- queryClient . setQueryData <
67
- InfiniteData <
68
- RecentlyModel [ ] & {
69
- comments : number
70
- }
71
- >
72
- > ( QUERY_KEY , ( old ) => {
73
- return produce ( old , ( draft ) => {
74
- draft ?. pages [ 0 ] . unshift ( res . $serialized as any )
75
- return draft
64
+ const { mutateAsync : handleSend , isPending } = useMutation ( {
65
+ mutationFn : async ( ) => {
66
+ apiClient . shorthand . proxy
67
+ . post ( { data : { content : value } } )
68
+ . then ( ( res ) => {
69
+ setValue ( '' )
70
+
71
+ queryClient . setQueryData <
72
+ InfiniteData <
73
+ RecentlyModel [ ] & {
74
+ comments : number
75
+ }
76
+ >
77
+ > ( QUERY_KEY , ( old ) => {
78
+ return produce ( old , ( draft ) => {
79
+ draft ?. pages [ 0 ] . unshift ( res . $serialized as any )
80
+ return draft
81
+ } )
82
+ } )
76
83
} )
77
- } )
78
- } )
79
- }
84
+ } ,
85
+ } )
86
+ if ( ! isLogin ) return null
80
87
return (
81
88
< form onSubmit = { preventDefault } className = "mb-8" >
82
89
< TextArea
@@ -94,8 +101,8 @@ const PostBox = () => {
94
101
>
95
102
< div className = "absolute bottom-2 right-2 flex size-5 center" >
96
103
< MotionButtonBase
97
- onClick = { handleSend }
98
- disabled = { value . length === 0 }
104
+ onClick = { ( ) => handleSend ( ) }
105
+ disabled = { value . length === 0 || isPending }
99
106
className = "duration-200 disabled:cursor-not-allowed disabled:opacity-10"
100
107
>
101
108
< TiltedSendIcon className = "size-5 text-zinc-800 dark:text-zinc-200" />
0 commit comments