Skip to content

Commit

Permalink
feat: ability to turn of auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
homocodian committed Feb 18, 2024
1 parent 4f952e7 commit 3017cbd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 deletions.
36 changes: 21 additions & 15 deletions src/components/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import KeyboardShortcut from "@/components/general/KeyboardShortcut";
import SettingsMenu from "@/components/general/SettingsMenu";
import { useDrawer } from "@/context/DrawerContext";
import { useAuthStore } from "@/store/auth";
import Tooltip from "@mui/material/Tooltip";
import { useSearchParams } from "react-router-dom";
import { SideDrawer } from "./main";
import RefreshButton from "./refresh";
Expand Down Expand Up @@ -81,9 +82,13 @@ function AppBar() {
<div className="space-x-2">
{user?.uid ? (
<>
<IconButton onClick={() => setShouldShowToolbar(false)}>
<SearchIcon htmlColor="#fff" />
</IconButton>
<Tooltip title="Search">
<IconButton
onClick={() => setShouldShowToolbar(false)}
>
<SearchIcon htmlColor="#fff" />
</IconButton>
</Tooltip>
<RefreshButton />
</>
) : null}
Expand All @@ -110,18 +115,19 @@ function AppBar() {
gap="1rem"
>
<Searchbar />
<IconButton
onClick={() => {
if (searchParams.get("q")) {
searchParams.delete("q");
setSearchParams(searchParams);
}

setShouldShowSearchbar(false);
}}
>
<ClearIcon htmlColor="#fff" />
</IconButton>
<Tooltip title="Close">
<IconButton
onClick={() => {
if (searchParams.get("q")) {
searchParams.delete("q");
setSearchParams(searchParams);
}
setShouldShowSearchbar(false);
}}
>
<ClearIcon htmlColor="#fff" />
</IconButton>
</Tooltip>
</Box>
</Slide>
</Toolbar>
Expand Down
24 changes: 24 additions & 0 deletions src/components/general/AutoRefreshMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import DoneIcon from "@mui/icons-material/Done";
import ListItemIcon from "@mui/material/ListItemIcon";
import MenuItem from "@mui/material/MenuItem";
import { ComponentProps } from "react";
import { useLocalStorage } from "usehooks-ts";

export function AutoRefreshMenuItem(props: ComponentProps<typeof MenuItem>) {
const [isAutoRefresh, setIsAutoRefresh] = useLocalStorage(
"auto-refresh",
true,
);

return (
<MenuItem
{...props}
onClick={() => {
setIsAutoRefresh((old) => !old);
}}
>
<ListItemIcon>{isAutoRefresh ? <DoneIcon /> : null}</ListItemIcon>
Auto refresh
</MenuItem>
);
}
7 changes: 7 additions & 0 deletions src/components/general/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Capacitor } from "@capacitor/core";
import { GoogleAuth } from "@codetrix-studio/capacitor-google-auth";
import { signOut } from "firebase/auth";
import toast from "react-hot-toast";
import { AutoRefreshMenuItem } from "./AutoRefreshMenuItem";

const logout = async () => {
if (Capacitor.isNativePlatform()) {
Expand Down Expand Up @@ -146,6 +147,12 @@ export default function SettingsMenu() {
<Divider />
<ThemeMenuItem handleItemClick={handleItemClick} />
<Divider />
<AutoRefreshMenuItem
sx={{
marginTop: "0.5rem",
}}
/>
<Divider />
<MenuItem
onClick={() => handleItemClick(openModal)}
sx={{
Expand Down
21 changes: 12 additions & 9 deletions src/components/refresh.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { cn } from "@/lib/utils";
import RefreshIcon from "@mui/icons-material/Refresh";
import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import {
useIsFetching,
useIsMutating,
useQueryClient,
} from "@tanstack/react-query";
import { RefreshCw } from "lucide-react";
import toast from "react-hot-toast";

function RefreshButton() {
Expand All @@ -20,14 +21,16 @@ function RefreshButton() {
}

return (
<IconButton aria-label="refresh" onClick={refresh}>
<RefreshCw
className={cn(
"h-5 w-5 text-white",
isFetching > 0 || isMutating > 0 ? "animate-spin" : "",
)}
/>
</IconButton>
<Tooltip title="Refresh">
<IconButton aria-label="refresh" onClick={refresh}>
<RefreshIcon
className={cn(
"h-5 w-5 text-white",
isFetching > 0 || isMutating > 0 ? "animate-spin" : "",
)}
/>
</IconButton>
</Tooltip>
);
}

Expand Down
16 changes: 10 additions & 6 deletions src/utils/query-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { AxiosError } from "axios";
import { signOut } from "firebase/auth";
import toast from "react-hot-toast";

const isAutoRefresh = JSON.parse(
window.localStorage.getItem("auto-refresh") ?? "true",
);

// Create a client
export const queryClient = new QueryClient({
queryCache: new QueryCache({
Expand Down Expand Up @@ -36,10 +40,10 @@ export const queryClient = new QueryClient({
} else toast.error(`Something went wrong: ${error.message}`);
},
}),
// defaultOptions: {
// queries: {
// // gc time 5 minutes
// staleTime: 5 * 60 * 1000,
// },
// },
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchInterval: isAutoRefresh ? 1000 * 30 : undefined,
},
},
});

0 comments on commit 3017cbd

Please sign in to comment.