Skip to content

Commit

Permalink
Merge pull request #372 from c-bata/save-reload-interval-on-global-st…
Browse files Browse the repository at this point in the history
…orage

Save reload interval on localStorage
  • Loading branch information
c-bata authored Jan 27, 2023
2 parents 5bf6c38 + 8c7ce19 commit 7370ee0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
28 changes: 28 additions & 0 deletions optuna_dashboard/ts/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ import {
paramImportanceState,
isFileUploading,
artifactIsAvailable,
reloadIntervalState,
} from "./state"

const localStorageGraphVisibility = "graphVisibility"
const localStorageReloadInterval = "reloadInterval"

type LocalStorageReloadInterval = {
reloadInterval?: number
}

export const actionCreator = () => {
const { enqueueSnackbar } = useSnackbar()
Expand All @@ -33,6 +39,7 @@ export const actionCreator = () => {
useRecoilState<StudyDetails>(studyDetailsState)
const [graphVisibility, setGraphVisibility] =
useRecoilState<GraphVisibility>(graphVisibilityState)
const setReloadInterval = useSetRecoilState<number>(reloadIntervalState)
const [paramImportance, setParamImportance] =
useRecoilState<StudyParamImportance>(paramImportanceState)
const setUploading = useSetRecoilState<boolean>(isFileUploading)
Expand Down Expand Up @@ -264,6 +271,25 @@ export const actionCreator = () => {
localStorage.setItem(localStorageGraphVisibility, JSON.stringify(value))
}

const loadReloadInterval = () => {
const reloadIntervalJSON = localStorage.getItem(localStorageReloadInterval)
if (reloadIntervalJSON === null) {
return
}
const gp = JSON.parse(reloadIntervalJSON) as LocalStorageReloadInterval
if (gp.reloadInterval !== undefined) {
setReloadInterval(gp.reloadInterval)
}
}

const saveReloadInterval = (interval: number) => {
setReloadInterval(interval)
const value: LocalStorageReloadInterval = {
reloadInterval: interval,
}
localStorage.setItem(localStorageReloadInterval, JSON.stringify(value))
}

const saveStudyNote = (studyId: number, note: Note): Promise<void> => {
return saveStudyNoteAPI(studyId, note)
.then(() => {
Expand Down Expand Up @@ -437,6 +463,8 @@ export const actionCreator = () => {
renameStudy,
getGraphVisibility,
saveGraphVisibility,
loadReloadInterval,
saveReloadInterval,
saveStudyNote,
saveTrialNote,
uploadArtifact,
Expand Down
9 changes: 5 additions & 4 deletions optuna_dashboard/ts/components/AppDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from "react"
import { useRecoilState } from "recoil"
import { useRecoilState, useRecoilValue } from "recoil"
import { styled, useTheme, Theme, CSSObject } from "@mui/material/styles"
import Box from "@mui/material/Box"
import MuiDrawer from "@mui/material/Drawer"
Expand Down Expand Up @@ -30,6 +30,7 @@ import GitHubIcon from "@mui/icons-material/GitHub"
import OpenInNewIcon from "@mui/icons-material/OpenInNew"
import QueryStatsIcon from "@mui/icons-material/QueryStats"
import { Switch } from "@mui/material"
import { actionCreator } from "../action"

const drawerWidth = 240

Expand Down Expand Up @@ -117,9 +118,9 @@ export const AppDrawer: FC<{
children?: React.ReactNode
}> = ({ studyId, toggleColorMode, page, toolbar, children }) => {
const theme = useTheme()
const action = actionCreator()
const [open, setOpen] = useRecoilState<boolean>(drawerOpenState)
const [reloadInterval, updateReloadInterval] =
useRecoilState<number>(reloadIntervalState)
const reloadInterval = useRecoilValue<number>(reloadIntervalState)

const styleListItem = {
display: "block",
Expand Down Expand Up @@ -256,7 +257,7 @@ export const AppDrawer: FC<{
<ListItemButton
sx={styleListItemButton}
onClick={() => {
updateReloadInterval(reloadInterval === -1 ? 10 : -1)
action.saveReloadInterval(reloadInterval === -1 ? 10 : -1)
}}
>
<ListItemIcon sx={styleListItemIcon}>
Expand Down
9 changes: 5 additions & 4 deletions optuna_dashboard/ts/components/ReloadIntervalSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { FC } from "react"
import { styled } from "@mui/system"
import { useRecoilState } from "recoil"
import { useRecoilValue } from "recoil"
import { reloadIntervalState } from "../state"
import { MenuItem, TextField, alpha } from "@mui/material"
import { Cached } from "@mui/icons-material"
import { actionCreator } from "../action"

export const ReloadIntervalSelect: FC = () => {
const [reloadInterval, updateReloadInterval] =
useRecoilState<number>(reloadIntervalState)
const action = actionCreator()
const reloadInterval = useRecoilValue<number>(reloadIntervalState)

const Wrapper = styled("div")(({ theme }) => ({
position: "relative",
Expand Down Expand Up @@ -71,7 +72,7 @@ export const ReloadIntervalSelect: FC = () => {
select
value={reloadInterval}
onChange={(e) => {
updateReloadInterval(e.target.value as unknown as number)
action.saveReloadInterval(e.target.value as unknown as number)
}}
>
<MenuItem value={-1}>stop</MenuItem>
Expand Down
1 change: 1 addition & 0 deletions optuna_dashboard/ts/components/StudyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const StudyDetail: FC<{
usePreferenceDialog(studyDetail)

useEffect(() => {
action.loadReloadInterval()
action.updateStudyDetail(studyIdNumber)
}, [])

Expand Down
1 change: 1 addition & 0 deletions optuna_dashboard/ts/components/StudyDetailBeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const StudyDetailBeta: FC<{
studyName !== null ? `${studyName} (id=${studyId})` : `Study #${studyId}`

useEffect(() => {
action.loadReloadInterval()
action.updateStudyDetail(studyId)
action.updateAPIMeta()
}, [])
Expand Down

0 comments on commit 7370ee0

Please sign in to comment.