1
- import { Button } from "@/components/ui/button " ;
1
+ import { Switch } from "@/components/ui/switch " ;
2
2
import { Loader2 } from "lucide-react" ;
3
3
import React from "react" ;
4
4
import { useQueryAutomation } from "@/hooks/user-queries" ;
5
5
import { useMutationData } from "@/hooks/use-mutation-data" ;
6
6
import { activateAutomation } from "@/actions/automations" ;
7
- import { ActiveAutomation } from "@/icons/active-automation" ;
8
7
9
8
type Props = {
10
9
id : string ;
@@ -18,18 +17,37 @@ const ActivateAutomationButton = ({ id }: Props) => {
18
17
"automation-info"
19
18
) ;
20
19
21
- return (
22
- < Button
23
- disabled = { isPending }
24
- onClick = { ( ) => mutate ( { state : ! data ?. data ?. active } ) }
25
- className = "lg:px-10 bg-gradient-to-br hover:opacity-80 text-white rounded-full from-[#3352CC] font-medium to-[#1C2D70] ml-4"
26
- >
27
- { isPending ? < Loader2 className = "animate-spin" /> : < ActiveAutomation /> }
20
+ const [ optimisticState , setOptimisticState ] = React . useState ( data ?. data ?. active || false ) ;
21
+
22
+ React . useEffect ( ( ) => {
23
+ if ( ! isPending ) {
24
+ setOptimisticState ( data ?. data ?. active || false ) ;
25
+ }
26
+ } , [ data ?. data ?. active , isPending ] ) ;
28
27
29
- < p className = "lg:inline hidden" >
30
- { data ?. data ?. active ? "Disable" : "Activate" }
31
- </ p >
32
- </ Button >
28
+ const handleStateChange = ( checked : boolean ) => {
29
+ setOptimisticState ( checked ) ;
30
+ mutate ( { state : checked } ) ;
31
+ } ;
32
+
33
+ return (
34
+ < div className = "flex items-center space-x-2" >
35
+ < Switch
36
+ disabled = { isPending }
37
+ checked = { optimisticState }
38
+ onCheckedChange = { handleStateChange }
39
+ className = "data-[state=checked]:hover:bg-green-600 data-[state=checked]:bg-green-500 data-[state=unchecked]:hover:bg-slate-500 data-[state=unchecked]:bg-slate-400 h-6 w-11 mx-4 [&>span]:data-[state=checked]:translate-x-6 [&>span]:bg-white transition-colors duration-200"
40
+ />
41
+ < span className = "text-sm font-medium" >
42
+ { isPending ? (
43
+ < Loader2 className = "h-4 w-4 animate-spin" />
44
+ ) : (
45
+ < strong className = { optimisticState ? "text-green-500" : "" } >
46
+ { optimisticState ? "Active" : "Inactive" }
47
+ </ strong >
48
+ ) }
49
+ </ span >
50
+ </ div >
33
51
) ;
34
52
} ;
35
53
0 commit comments