Skip to content

Commit

Permalink
feat: do not refresh status if not on status page or looking at the s…
Browse files Browse the repository at this point in the history
…tatus popup
  • Loading branch information
CrescentKohana committed Jan 20, 2024
1 parent 1683282 commit c36c971
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/status/status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function statusMessage(running: boolean) {
}

function StatusPage() {
const { data } = useProcessingStatus()
const { data } = useProcessingStatus(true)

const errorPopup = (key: number, triggerText: string, error: string, details: Map<string, string>) => {
return (
Expand Down
7 changes: 6 additions & 1 deletion components/Nav/NavPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ interface Props {
children: ReactNode
menuRef?: RefObject<PopupActions>
isLink?: boolean
isVisible?: boolean
onVisibilityChange?: (isOpen: boolean) => void
}

const NavPopup = ({ triggerChildren, children, menuRef, isLink }: Props) => {
const NavPopup = ({ triggerChildren, children, menuRef, isLink, isVisible, onVisibilityChange }: Props) => {
return (
<Popup
trigger={
Expand All @@ -28,6 +30,9 @@ const NavPopup = ({ triggerChildren, children, menuRef, isLink }: Props) => {
position="bottom right"
arrow={false}
ref={menuRef}
open={isVisible}
onOpen={onVisibilityChange ? () => onVisibilityChange(true) : () => {}}
onClose={onVisibilityChange ? () => onVisibilityChange(false) : () => {}}
>
<div
className="rounded bg-slate-700 w-auto h-auto min-w-64 flex flex-col py-2"
Expand Down
15 changes: 13 additions & 2 deletions components/Nav/RunTasks.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client"
import Image from "next/image"
import { useState } from "react"
import { toast } from "react-toastify"
import { statusMessage } from "../../app/status/status"
import { initiateMetadataGen, initiateScan, initiateThumbnailGen } from "../../lib/api/task"
Expand All @@ -9,7 +10,9 @@ import Button from "../Button"
import NavPopup from "./NavPopup"

const RunTasks = () => {
const { data } = useProcessingStatus()
const [isVisible, setIsVisible] = useState(false)

const { data } = useProcessingStatus(isVisible)

// TODO: More options to the UI such as quick scan, covers only thumnbnail generation etc.
// const quickScanHandler = async () => {
Expand All @@ -32,6 +35,10 @@ const RunTasks = () => {
await initiateThumbnailGen(true, true)
}

const onVisibilityChange = (isOpen: boolean) => {
setIsVisible(isOpen)
}

if (!data) {
return (
<NavPopup triggerChildren={<Image width={24} height={24} alt="scan menu" src={ScanIcon} />}>
Expand All @@ -52,7 +59,11 @@ const RunTasks = () => {
}

return (
<NavPopup triggerChildren={<Image width={24} height={24} alt="scan menu" src={ScanIcon} />}>
<NavPopup
triggerChildren={<Image width={24} height={24} alt="scan menu" src={ScanIcon} />}
isVisible={isVisible}
onVisibilityChange={onVisibilityChange}
>
<Button href="/status#thumbnails" className="mx-4 my-2 w-60">
View stats
</Button>
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/data/useProcessingStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { APIPathsV1, swrFetcher } from "../../api/other"

const fetcher: Fetcher<ProcessingStatus, string> = (id) => swrFetcher(id)

export default function useProcessingStatus() {
const { data } = useSWR(APIPathsV1.Status, (key) => fetcher(key), { refreshInterval: 1000 })
export default function useProcessingStatus(refresh?: boolean) {
const { data } = useSWR(APIPathsV1.Status, (key) => fetcher(key), { refreshInterval: refresh ? 2000 : 0 })

return {
data,
Expand Down

0 comments on commit c36c971

Please sign in to comment.