Skip to content

Commit

Permalink
Merge pull request #821 from c-bata/remove-debounce
Browse files Browse the repository at this point in the history
Use `useDeferredValue` instead of `DebounceInputTextField`
  • Loading branch information
c-bata authored Mar 5, 2024
2 parents 3e50328 + 7708a1f commit f0f7f09
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 81 deletions.
1 change: 1 addition & 0 deletions optuna_dashboard/ts/components/Debounce.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FC, useEffect } from "react"
import { TextField, TextFieldProps } from "@mui/material"

// TODO(c-bata): Remove this and use `useDeferredValue` instead.
export const DebouncedInputTextField: FC<{
onChange: (s: string, valid: boolean) => void
delay: number
Expand Down
59 changes: 32 additions & 27 deletions optuna_dashboard/ts/components/StudyList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { FC, useEffect, useState } from "react"
import React, {
FC,
useEffect,
useState,
useDeferredValue,
useMemo,
} from "react"
import { useNavigate } from "react-router-dom"
import { useRecoilValue } from "recoil"
import { Link } from "react-router-dom"
Expand Down Expand Up @@ -26,7 +32,6 @@ import CompareIcon from "@mui/icons-material/Compare"
import DriveFileRenameOutlineIcon from "@mui/icons-material/DriveFileRenameOutline"

import { actionCreator } from "../action"
import { DebouncedInputTextField } from "./Debounce"
import { studySummariesLoadingState, studySummariesState } from "../state"
import { styled } from "@mui/system"
import { AppDrawer } from "./AppDrawer"
Expand All @@ -41,7 +46,8 @@ export const StudyList: FC<{
const theme = useTheme()
const action = actionCreator()

const [studyFilterText, setStudyFilterText] = React.useState<string>("")
const [_studyFilterText, setStudyFilterText] = React.useState<string>("")
const studyFilterText = useDeferredValue(_studyFilterText)
const studyFilter = (row: StudySummary) => {
const keywords = studyFilterText.split(" ")
return !keywords.every((k) => {
Expand All @@ -64,12 +70,14 @@ export const StudyList: FC<{
const query = useQuery()
const initialSortBy = query.get("studies_order_by") === "asc" ? "asc" : "desc"
const [sortBy, setSortBy] = useState<"asc" | "desc">(initialSortBy)
const filteredStudies = useMemo(() => {
let filteredStudies: StudySummary[] = studies.filter((s) => !studyFilter(s))
if (sortBy === "desc") {
filteredStudies = filteredStudies.reverse()
}
return filteredStudies
}, [studyFilterText, studies, sortBy])

let filteredStudies = studies.filter((s) => !studyFilter(s))

if (sortBy === "desc") {
filteredStudies = filteredStudies.reverse()
}
useEffect(() => {
action.updateStudySummaries()
}, [])
Expand Down Expand Up @@ -201,26 +209,23 @@ export const StudyList: FC<{
<Card sx={{ margin: theme.spacing(2) }}>
<CardContent>
<Box sx={{ display: "flex" }}>
<DebouncedInputTextField
onChange={(s) => {
setStudyFilterText(s)
<TextField
onChange={(e) => {
setStudyFilterText(e.target.value)
}}
delay={500}
textFieldProps={{
fullWidth: true,
id: "search-study",
variant: "outlined",
placeholder: "Search study",
sx: { maxWidth: 500 },
InputProps: {
startAdornment: (
<InputAdornment position="start">
<SvgIcon fontSize="small" color="action">
<Search />
</SvgIcon>
</InputAdornment>
),
},
id="search-study"
variant="outlined"
placeholder="Search study"
fullWidth
sx={{ maxWidth: 500 }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<SvgIcon fontSize="small" color="action">
<Search />
</SvgIcon>
</InputAdornment>
),
}}
/>
{sortBySelect}
Expand Down
28 changes: 0 additions & 28 deletions standalone_app/src/components/Debounce.tsx

This file was deleted.

52 changes: 26 additions & 26 deletions standalone_app/src/components/StudyList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from "react"
import React, { FC, useState, useMemo, useDeferredValue } from "react"
import {
AppBar,
Typography,
Expand All @@ -22,7 +22,6 @@ import Brightness7Icon from "@mui/icons-material/Brightness7"
import { useRecoilValue } from "recoil"
import { studiesState } from "../state"
import { Link } from "react-router-dom"
import { DebouncedInputTextField } from "./Debounce"
import { Search } from "@mui/icons-material"
import { StorageLoader } from "./StorageLoader"

Expand All @@ -32,8 +31,9 @@ export const StudyList: FC<{
const theme = useTheme()
const studies = useRecoilValue<Study[]>(studiesState)

const [studyFilterText, setStudyFilterText] = useState<string>("")
const [_studyFilterText, setStudyFilterText] = useState<string>("")
const [sortBy, setSortBy] = useState<"id-asc" | "id-desc">("id-asc")
const studyFilterText = useDeferredValue(_studyFilterText)
const studyFilter = (row: Study): boolean => {
const keywords = studyFilterText.split(" ")
return !keywords.every((k) => {
Expand All @@ -43,10 +43,13 @@ export const StudyList: FC<{
return row.study_name.indexOf(k) >= 0
})
}
let filteredStudies: Study[] = studies.filter((s) => !studyFilter(s))
if (sortBy === "id-desc") {
filteredStudies = filteredStudies.reverse()
}
const filteredStudies = useMemo(() => {
let filteredStudies: Study[] = studies.filter((s) => !studyFilter(s))
if (sortBy === "id-desc") {
filteredStudies = filteredStudies.reverse()
}
return filteredStudies
}, [studyFilterText, studies, sortBy])

const Select = styled(TextField)(({ theme }) => ({
"& .MuiInputBase-input": {
Expand Down Expand Up @@ -131,26 +134,23 @@ export const StudyList: FC<{
<Card sx={{ margin: theme.spacing(2) }}>
<CardContent>
<Box sx={{ display: "flex" }}>
<DebouncedInputTextField
onChange={(s) => {
setStudyFilterText(s)
<TextField
onChange={(e) => {
setStudyFilterText(e.target.value)
}}
delay={500}
textFieldProps={{
fullWidth: true,
id: "search-study",
variant: "outlined",
placeholder: "Search study",
sx: { maxWidth: 500 },
InputProps: {
startAdornment: (
<InputAdornment position="start">
<SvgIcon fontSize="small" color="action">
<Search />
</SvgIcon>
</InputAdornment>
),
},
id="search-study"
variant="outlined"
placeholder="Search study"
fullWidth
sx={{ maxWidth: 500 }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<SvgIcon fontSize="small" color="action">
<Search />
</SvgIcon>
</InputAdornment>
),
}}
/>
{sortBySelect}
Expand Down

0 comments on commit f0f7f09

Please sign in to comment.