Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use react-query to implement refetch logic #92

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Header } from "./components/Header";
import { PromptList } from "./components/PromptList";
import { useEffect } from "react";
import { Dashboard } from "./components/Dashboard";
import { Routes, Route, Link } from "react-router-dom";
import { Chat } from "./components/Chat";
Expand All @@ -20,14 +19,10 @@ import {
import { useBreadcrumb } from "./hooks/useBreadcrumb";

function App() {
const { data: prompts, isLoading, refetch } = usePromptsData();
const { data: prompts, isLoading } = usePromptsData();
useSse();
const breadcrumb = useBreadcrumb();

useEffect(() => {
refetch();
}, [refetch]);

return (
<div className="flex w-screen h-screen">
<Sidebar loading={isLoading}>
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/__tests__/useSee.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { renderHook, act } from "@testing-library/react";
import { vi } from "vitest";
import { useSse } from "../useSse";
import { TestQueryClientProvider } from "@/lib/test-utils";

vi.mock("react-router-dom", () => ({
useLocation: vi.fn(() => ({ pathname: "/" })),
Expand Down Expand Up @@ -63,7 +64,7 @@ describe("useSse", () => {
});

it("should send notification if new alert is detected", () => {
renderHook(() => useSse());
renderHook(() => useSse(), { wrapper: TestQueryClientProvider });

expect(MockEventSource.instances.length).toBe(1);
const instance = MockEventSource.instances[0];
Expand All @@ -87,7 +88,7 @@ describe("useSse", () => {
});

it("should send notification if new alert is detected", () => {
renderHook(() => useSse());
renderHook(() => useSse(), { wrapper: TestQueryClientProvider });

act(() => {
MockEventSource.triggerMessage("other message");
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useSse.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { useEffect } from "react";
import { useBrowserNotification } from "./useBrowserNotification";
import { useLocation } from "react-router-dom";
import { useQueryClient } from "@tanstack/react-query";

const BASE_URL = import.meta.env.VITE_BASE_API_URL;

export function useSse() {
const location = useLocation();
const { sendNotification } = useBrowserNotification();
const queryClient = useQueryClient();

useEffect(() => {
const eventSource = new EventSource(
`${BASE_URL}/dashboard/alerts_notification`
`${BASE_URL}/dashboard/alerts_notification`,
);

eventSource.onmessage = function (event) {
queryClient.invalidateQueries({ refetchType: "all" });
if (event.data.toLowerCase().includes("new alert detected")) {
sendNotification("CodeGate Dashboard", {
body: "New Alert detected!",
Expand All @@ -28,5 +31,5 @@ export function useSse() {
return () => {
eventSource.close();
};
}, [location.pathname, sendNotification]);
}, [location.pathname, queryClient, sendNotification]);
}
Loading