refactor: cleanup some components
This commit is contained in:
12 files changed
+71
-40
No files matched your search
@@ -1,27 +1,30 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
function useInterval(callback: () => void, delay: number) {
|
||||
const savedCallback = useRef<null | (() => void)>(null);
|
||||
export default function useInterval(
|
||||
callback: () => void,
|
||||
delay: number | null,
|
||||
) {
|
||||
const savedCallback = useRef(callback);
|
||||
|
||||
// Remember the latest callback.
|
||||
// Remember the latest callback if it changes.
|
||||
useEffect(() => {
|
||||
if (savedCallback.current) {
|
||||
savedCallback.current = callback;
|
||||
}
|
||||
savedCallback.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
// Set up the interval.
|
||||
useEffect(() => {
|
||||
function tick() {
|
||||
if (savedCallback.current) {
|
||||
savedCallback.current();
|
||||
}
|
||||
// Don't schedule if no delay is specified.
|
||||
// Note: 0 is a valid value for delay.
|
||||
if (delay === null) {
|
||||
return;
|
||||
}
|
||||
if (delay !== null) {
|
||||
const id = setInterval(tick, delay);
|
||||
return () => clearInterval(id);
|
||||
}
|
||||
}, [delay]);
|
||||
}
|
||||
|
||||
export default useInterval;
|
||||
const id = setInterval(() => {
|
||||
savedCallback.current();
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
clearInterval(id);
|
||||
};
|
||||
}, [delay]);
|
||||
}
|
||||
@@ -26,6 +26,7 @@ type CentralStore = {
|
||||
clientVersion: string;
|
||||
currentUser: null | UserDto;
|
||||
fetchCurrentUser: (api: Api | undefined) => Promise<void>;
|
||||
resetCurrentUser: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -58,11 +59,14 @@ export const CentralProvider = ({ children }: { children: ReactNode }) => {
|
||||
clientVersion: version,
|
||||
currentUser: null,
|
||||
fetchCurrentUser: async (api) => {
|
||||
if (api) {
|
||||
if (api?.accessToken) {
|
||||
const user = (await getUserApi(api).getCurrentUser()).data;
|
||||
set((s) => ({ ...s, currentUser: user }));
|
||||
}
|
||||
},
|
||||
resetCurrentUser: () => {
|
||||
set((s) => ({ ...s, currentUser: null }));
|
||||
},
|
||||
})),
|
||||
);
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user