Skip to content

Commit

Permalink
Implement very basic polling ever 3s
Browse files Browse the repository at this point in the history
  • Loading branch information
callumforrester committed Oct 29, 2024
1 parent 7b7c585 commit 46b0c6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ThemeProvider } from "@emotion/react";
import FilterMenu from "./components/FilterMenu.tsx";
import { useEffect, useState } from "react";
import { useState } from "react";
import { theme } from "./theme";
import { LogData } from "./schema/interfaces.ts";

import { buildSearchQuery, graylogSearch } from "./utils/api/search.ts";
import LogTable from "./components/LogTable.tsx";
import Disclaimer from "./components/Disclaimer.tsx";
import { useInterval } from "./utils/interval.ts";

function App() {
// Init states
Expand All @@ -18,7 +19,7 @@ function App() {
const [applicationFilter, setApplicationFilter] = useState("*");
const [beamlineFilter, setBeamlineFilter] = useState("*");

useEffect(() => {
useInterval(() => {
// Calls file for auth and calls POST API call
(async () => {
try {
Expand All @@ -38,7 +39,7 @@ function App() {
}
}
})();
}, [logFilter, applicationFilter, beamlineFilter]);
}, 3000);

return (
<ThemeProvider theme={theme}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/LogTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function LogTable(props: Props) {
} else if (props.error !== undefined) {
return <p>{props.error}</p>;
} else {
return <p>An unknown error has occurred</p>;
return <p>No data from server</p>;
}
})()}
</BoxBasic>
Expand Down
27 changes: 27 additions & 0 deletions src/utils/interval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect, useRef } from "react";

type UseInterval = <T, U>(
callback: (vars?: U) => T,
delay: number | null,
) => void;

export const useInterval: UseInterval = (callback, delay) => {
const callbackRef = useRef<typeof callback | null>(null);

useEffect(() => {
callbackRef.current = callback;
}, [callback]);

useEffect(() => {
const tick = () => {
if (callbackRef.current) {
callbackRef.current();
}
};
tick();
if (delay !== null) {
const id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
};

0 comments on commit 46b0c6b

Please sign in to comment.