Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StudyArtifactCards and TrialArtifactCards into ArtifactCards to Remove Redundancy #1028

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,44 @@ import React, {
useRef,
useState,
} from "react"

import { Trial } from "ts/types/optuna"
import { actionCreator } from "../../action"
import { StudyDetail, Trial } from "../../types/optuna"
import { ArtifactCardMedia } from "./ArtifactCardMedia"
import { useDeleteTrialArtifactDialog } from "./DeleteArtifactDialog"
import { useDeleteArtifactDialog } from "./DeleteArtifactDialog"
import { isTableArtifact, useTableArtifactModal } from "./TableArtifactViewer"
import {
isThreejsArtifact,
useThreejsArtifactModal,
} from "./ThreejsArtifactViewer"

export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {
type StudyOrTrial =
| {
type: "study"
study: StudyDetail
}
| {
type: "trial"
trial: Trial
}
export const ArtifactCards: FC<{
studyOrTrial: StudyOrTrial
isArtifactModifiable?: boolean
}> = ({ studyOrTrial, isArtifactModifiable = true }) => {
const theme = useTheme()
const [openDeleteArtifactDialog, renderDeleteArtifactDialog] =
useDeleteTrialArtifactDialog()
useDeleteArtifactDialog()
const [openThreejsArtifactModal, renderThreejsArtifactModal] =
useThreejsArtifactModal()
const [openTableArtifactModal, renderTableArtifactModal] =
useTableArtifactModal()
const isArtifactModifiable = (trial: Trial) => {
return trial.state === "Running" || trial.state === "Waiting"
}

const width = "200px"
const height = "150px"
const artifacts = [...trial.artifacts].sort((a, b) => {
const sortedArtifacts = [
...(studyOrTrial.type === "study"
? studyOrTrial.study.artifacts
: studyOrTrial.trial.artifacts),
].sort((a, b) => {
if (a.filename < b.filename) {
return -1
} else if (a.filename > b.filename) {
Expand All @@ -56,28 +68,31 @@ export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {

return (
<>
<Typography
variant="h5"
sx={{ fontWeight: theme.typography.fontWeightBold }}
>
Artifacts
</Typography>
<Box
component="div"
sx={{ display: "flex", flexWrap: "wrap", p: theme.spacing(1, 0) }}
>
{artifacts.map((artifact) => {
const urlPath = `/artifacts/${trial.study_id}/${trial.trial_id}/${artifact.artifact_id}`
{sortedArtifacts.map((artifact) => {
const urlPath =
studyOrTrial.type === "study"
? `/artifacts/${studyOrTrial.study.id}/${artifact.artifact_id}`
: `/artifacts/${studyOrTrial.trial.study_id}/${studyOrTrial.trial.trial_id}/${artifact.artifact_id}`
return (
<Card
key={artifact.artifact_id}
sx={{
marginBottom: theme.spacing(2),
width: width,
margin: theme.spacing(0, 1, 1, 0),
display: "flex",
flexDirection: "column",
alignItems: "center",
display: studyOrTrial.type === "trial" ? "flex" : undefined,
flexDirection:
studyOrTrial.type === "trial" ? "column" : undefined,
alignItems:
studyOrTrial.type === "trial" ? "center" : undefined,
border:
studyOrTrial.type === "study"
? `1px solid ${theme.palette.divider}`
: undefined,
}}
>
<ArtifactCardMedia
Expand All @@ -96,12 +111,22 @@ export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {
sx={{
p: theme.spacing(0.5, 0),
flexGrow: 1,
wordBreak: "break-all",
maxWidth: `calc(100% - ${theme.spacing(
4 +
(isThreejsArtifact(artifact) ? 4 : 0) +
(isArtifactModifiable(trial) ? 4 : 0)
)})`,
wordBreak:
studyOrTrial.type === "trial" ? "break-all" : undefined,
maxWidth:
studyOrTrial.type === "trial"
? `calc(100% - ${theme.spacing(
4 +
(isThreejsArtifact(artifact) ? 4 : 0) +
(isArtifactModifiable ? 4 : 0)
)})`
: `calc(100% - ${
isThreejsArtifact(artifact)
? theme.spacing(12)
: theme.spacing(8)
})`,
wordWrap:
studyOrTrial.type === "study" ? "break-word" : undefined,
}}
>
{artifact.filename}
Expand Down Expand Up @@ -132,23 +157,32 @@ export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {
<FullscreenIcon />
</IconButton>
) : null}
{isArtifactModifiable(trial) ? (
{isArtifactModifiable && (
<IconButton
aria-label="delete artifact"
size="small"
color="inherit"
sx={{ margin: "auto 0" }}
onClick={() => {
openDeleteArtifactDialog(
trial.study_id,
trial.trial_id,
artifact
studyOrTrial.type === "study"
? {
type: "study",
studyId: studyOrTrial.study.id,
artifact,
}
: {
type: "trial",
studyId: studyOrTrial.trial.study_id,
trialId: studyOrTrial.trial.trial_id,
artifact,
}
)
}}
>
<DeleteIcon />
</IconButton>
) : null}
)}
<IconButton
aria-label="download artifact"
size="small"
Expand All @@ -163,9 +197,13 @@ export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {
</Card>
)
})}
{isArtifactModifiable(trial) ? (
<TrialArtifactUploader trial={trial} width={width} height={height} />
) : null}
{isArtifactModifiable && (
<ArtifactUploader
studyOrTrial={studyOrTrial}
width={width}
height={height}
/>
)}
</Box>
{renderDeleteArtifactDialog()}
{renderThreejsArtifactModal()}
Expand All @@ -174,14 +212,14 @@ export const TrialArtifactCards: FC<{ trial: Trial }> = ({ trial }) => {
)
}

const TrialArtifactUploader: FC<{
trial: Trial
const ArtifactUploader: FC<{
studyOrTrial: StudyOrTrial
width: string
height: string
}> = ({ trial, width, height }) => {
}> = ({ studyOrTrial, width, height }) => {
const theme = useTheme()
const action = actionCreator()
const [dragOver, setDragOver] = useState<boolean>(false)
const [dragOver, setDragOver] = useState(false)

const inputRef = useRef<HTMLInputElement>(null)
const handleClick: MouseEventHandler = () => {
Expand All @@ -190,34 +228,55 @@ const TrialArtifactUploader: FC<{
}
inputRef.current.click()
}

const handleOnChange: ChangeEventHandler<HTMLInputElement> = (e) => {
const files = e.target.files
if (files === null) {
return
}
action.uploadTrialArtifact(trial.study_id, trial.trial_id, files[0])
}
const handleDrop: DragEventHandler = (e) => {
e.stopPropagation()
e.preventDefault()
const files = e.dataTransfer.files
setDragOver(false)
for (let i = 0; i < files.length; i++) {
action.uploadTrialArtifact(trial.study_id, trial.trial_id, files[i])
if (studyOrTrial.type === "study") {
action.uploadStudyArtifact(studyOrTrial.study.id, files[0])
} else if (studyOrTrial.type === "trial") {
action.uploadTrialArtifact(
studyOrTrial.trial.study_id,
studyOrTrial.trial.trial_id,
files[0]
)
}
}

const handleDragOver: DragEventHandler = (e) => {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = "copy"
setDragOver(true)
}

const handleDragLeave: DragEventHandler = (e) => {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = "copy"
setDragOver(false)
}

const handleDrop: DragEventHandler = (e) => {
e.stopPropagation()
e.preventDefault()
const files = e.dataTransfer.files
setDragOver(false)
for (let i = 0; i < files.length; i++) {
if (studyOrTrial.type === "study") {
action.uploadStudyArtifact(studyOrTrial.study.id, files[i])
} else if (studyOrTrial.type === "trial") {
action.uploadTrialArtifact(
studyOrTrial.trial.study_id,
studyOrTrial.trial.trial_id,
files[i]
)
}
}
}

return (
<Card
sx={{
Expand Down
87 changes: 28 additions & 59 deletions optuna_dashboard/ts/components/Artifact/DeleteArtifactDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,49 @@ import React, { ReactNode, useState, FC } from "react"
import { Artifact } from "ts/types/optuna"
import { actionCreator } from "../../action"

export const useDeleteTrialArtifactDialog = (): [
(studyId: number, trialId: number, artifact: Artifact) => void,
() => ReactNode,
] => {
const action = actionCreator()

const [openDeleteArtifactDialog, setOpenDeleteArtifactDialog] =
useState(false)
const [target, setTarget] = useState<[number, number, Artifact | null]>([
-1,
-1,
null,
])

const handleCloseDeleteArtifactDialog = () => {
setOpenDeleteArtifactDialog(false)
setTarget([-1, -1, null])
}

const handleDeleteArtifact = () => {
const [studyId, trialId, artifact] = target
if (artifact === null) {
return
type Target =
| {
type: "study"
studyId: number
artifact: Artifact
}
action.deleteTrialArtifact(studyId, trialId, artifact.artifact_id)
setOpenDeleteArtifactDialog(false)
setTarget([-1, -1, null])
}

const openDialog = (studyId: number, trialId: number, artifact: Artifact) => {
setTarget([studyId, trialId, artifact])
setOpenDeleteArtifactDialog(true)
}

const renderDeleteArtifactDialog = () => {
return (
<DeleteDialog
openDeleteArtifactDialog={openDeleteArtifactDialog}
handleCloseDeleteArtifactDialog={handleCloseDeleteArtifactDialog}
filename={target[2]?.filename}
handleDeleteArtifact={handleDeleteArtifact}
/>
)
}
return [openDialog, renderDeleteArtifactDialog]
}

export const useDeleteStudyArtifactDialog = (): [
(studyId: number, artifact: Artifact) => void,
| {
type: "trial"
studyId: number
trialId: number
artifact: Artifact
}
export const useDeleteArtifactDialog = (): [
(target: Target) => void,
() => ReactNode,
] => {
const action = actionCreator()

const [openDeleteArtifactDialog, setOpenDeleteArtifactDialog] =
useState(false)
const [target, setTarget] = useState<[number, Artifact | null]>([-1, null])
const [target, setTarget] = useState<Target | null>(null)

const handleCloseDeleteArtifactDialog = () => {
setOpenDeleteArtifactDialog(false)
setTarget([-1, null])
setTarget(null)
}

const handleDeleteArtifact = () => {
const [studyId, artifact] = target
if (artifact === null) {
return
if (target === null) return
if (target.type === "study") {
action.deleteStudyArtifact(target.studyId, target.artifact.artifact_id)
} else if (target.type === "trial") {
action.deleteTrialArtifact(
target.studyId,
target.trialId,
target.artifact.artifact_id
)
}
action.deleteStudyArtifact(studyId, artifact.artifact_id)
setOpenDeleteArtifactDialog(false)
setTarget([-1, null])
setTarget(null)
}

const openDialog = (studyId: number, artifact: Artifact) => {
setTarget([studyId, artifact])
const openDialog = (target: Target) => {
setTarget(target)
setOpenDeleteArtifactDialog(true)
}

Expand All @@ -94,7 +63,7 @@ export const useDeleteStudyArtifactDialog = (): [
<DeleteDialog
openDeleteArtifactDialog={openDeleteArtifactDialog}
handleCloseDeleteArtifactDialog={handleCloseDeleteArtifactDialog}
filename={target[1]?.filename}
filename={target?.artifact.filename}
handleDeleteArtifact={handleDeleteArtifact}
/>
)
Expand Down
Loading
Loading