WatcherControls.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use client";
  2. import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
  3. import { useEffect } from "react";
  4. import toast from "react-hot-toast";
  5. import { get, post } from "../../lib/api";
  6. import { useNotifications } from "./NotificationContext";
  7. export default function WatcherControls() {
  8. const queryClient = useQueryClient();
  9. const { addNotification } = useNotifications();
  10. const { data } = useQuery({
  11. queryKey: ["watcher", "status"],
  12. queryFn: () => get("/watcher/status"),
  13. });
  14. const startMutation = useMutation({
  15. mutationFn: () => post("/watcher/start"),
  16. onSuccess: () => {
  17. queryClient.invalidateQueries({ queryKey: ["watcher", "status"] });
  18. toast.success("File watcher started successfully");
  19. addNotification({
  20. type: "success",
  21. title: "File Watcher Started",
  22. message: "The file watcher has been started successfully.",
  23. });
  24. },
  25. onError: () => {
  26. toast.error("Failed to start file watcher");
  27. },
  28. });
  29. const stopMutation = useMutation({
  30. mutationFn: () => post("/watcher/stop"),
  31. onSuccess: () => {
  32. queryClient.invalidateQueries({ queryKey: ["watcher", "status"] });
  33. toast.success("File watcher stopped successfully");
  34. addNotification({
  35. type: "success",
  36. title: "File Watcher Stopped",
  37. message: "The file watcher has been stopped successfully.",
  38. });
  39. },
  40. onError: () => {
  41. toast.error("Failed to stop file watcher");
  42. },
  43. });
  44. // Listen for WebSocket events
  45. useEffect(() => {
  46. const handleWatcherUpdate = (event: CustomEvent) => {
  47. const updateData = event.detail;
  48. if (updateData.type === "started" || updateData.type === "stopped") {
  49. // Invalidate and refetch the watcher status
  50. queryClient.invalidateQueries({ queryKey: ["watcher", "status"] });
  51. }
  52. };
  53. window.addEventListener(
  54. "watcherUpdate",
  55. handleWatcherUpdate as EventListener
  56. );
  57. return () => {
  58. window.removeEventListener(
  59. "watcherUpdate",
  60. handleWatcherUpdate as EventListener
  61. );
  62. };
  63. }, [queryClient]);
  64. return (
  65. <div className="flex items-center gap-2">
  66. <div className="relative group">
  67. <button
  68. className="p-2 rounded-md text-gray-600 dark:text-gray-400 hover:bg-green-100 dark:hover:bg-green-900/30 hover:text-green-600 dark:hover:text-green-400 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
  69. onClick={() => startMutation.mutate()}
  70. disabled={data?.isWatching || startMutation.isPending}
  71. title="Start file watcher"
  72. >
  73. <svg
  74. className="h-5 w-5"
  75. fill="none"
  76. viewBox="0 0 24 24"
  77. stroke="currentColor"
  78. >
  79. <path
  80. strokeLinecap="round"
  81. strokeLinejoin="round"
  82. strokeWidth={2}
  83. d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
  84. />
  85. <path
  86. strokeLinecap="round"
  87. strokeLinejoin="round"
  88. strokeWidth={2}
  89. d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.543-7z"
  90. />
  91. </svg>
  92. </button>
  93. <div className="absolute bottom-full left-1/2 transform -translate-x-1/2 mb-2 px-2 py-1 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900 text-xs rounded whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none">
  94. {startMutation.isPending ? "Starting..." : "Start Watcher"}
  95. </div>
  96. </div>
  97. <div className="relative group">
  98. <button
  99. className="p-2 rounded-md text-gray-600 dark:text-gray-400 hover:bg-red-100 dark:hover:bg-red-900/30 hover:text-red-600 dark:hover:text-red-400 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
  100. onClick={() => stopMutation.mutate()}
  101. disabled={!data?.isWatching || stopMutation.isPending}
  102. title="Stop file watcher"
  103. >
  104. <svg
  105. className="h-5 w-5"
  106. fill="none"
  107. viewBox="0 0 24 24"
  108. stroke="currentColor"
  109. >
  110. <path
  111. strokeLinecap="round"
  112. strokeLinejoin="round"
  113. strokeWidth={2}
  114. d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
  115. />
  116. <path
  117. strokeLinecap="round"
  118. strokeLinejoin="round"
  119. strokeWidth={2}
  120. d="M9 10a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z"
  121. />
  122. </svg>
  123. </button>
  124. <div className="absolute bottom-full left-1/2 transform -translate-x-1/2 mb-2 px-2 py-1 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900 text-xs rounded whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none">
  125. {stopMutation.isPending ? "Stopping..." : "Stop Watcher"}
  126. </div>
  127. </div>
  128. </div>
  129. );
  130. }