Skip to content

Fixed bug with userdisplay and latestrelease #2053

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
39 changes: 13 additions & 26 deletions frontend/src/entry/model/releases/AssociatedReleasesDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,33 @@ import {
Typography,
} from '@mui/material'
import { useGetModel } from 'actions/model'
import { useGetReleasesForModelId } from 'actions/release'
import { useEffect, useMemo, useState } from 'react'
import { useMemo } from 'react'
import EmptyBlob from 'src/common/EmptyBlob'
import Loading from 'src/common/Loading'
import { Transition } from 'src/common/Transition'
import Link from 'src/Link'
import MessageAlert from 'src/MessageAlert'
import { FileInterface, isFileInterface } from 'types/types'
import { sortByCreatedAtDescending } from 'utils/arrayUtils'
import { FileInterface, isFileInterface, ReleaseInterface } from 'types/types'
import { formatDateString } from 'utils/dateUtils'

type AssociatedReleasesDialogProps = {
modelId: string
file: FileInterface | File
sortedAssociatedReleases: Array<ReleaseInterface>
latestRelease: string
open: boolean
onClose: () => void
}

export default function AssociatedReleasesDialog({ modelId, file, open, onClose }: AssociatedReleasesDialogProps) {
const { releases, isReleasesLoading, isReleasesError } = useGetReleasesForModelId(modelId)
export default function AssociatedReleasesDialog({
modelId,
file,
sortedAssociatedReleases,
latestRelease,
open,
onClose,
}: AssociatedReleasesDialogProps) {
const { model, isModelLoading, isModelError } = useGetModel(modelId, 'model')
const [latestRelease, setLatestRelease] = useState('')

const sortedAssociatedReleases = useMemo(
() =>
releases
.filter((release) => isFileInterface(file) && release.fileIds.includes(file._id))
.sort(sortByCreatedAtDescending),
[file, releases],
)

useEffect(() => {
if (model && releases.length > 0 && sortedAssociatedReleases.length > 0) {
setLatestRelease(sortedAssociatedReleases[0].semver)
}
}, [model, releases, sortedAssociatedReleases])

const associatedReleasesDisplay = useMemo(
() =>
Expand Down Expand Up @@ -85,18 +76,14 @@ export default function AssociatedReleasesDialog({ modelId, file, open, onClose
[file, latestRelease, model, sortedAssociatedReleases],
)

if (isReleasesError) {
return <MessageAlert message={isReleasesError.info.message} severity='error' />
}

if (isModelError) {
return <MessageAlert message={isModelError.info.message} severity='error' />
}

return (
<Dialog fullWidth open={open} onClose={onClose} maxWidth='sm' slots={{ transition: Transition }}>
<DialogTitle>Associated Releases</DialogTitle>
<DialogContent>{isModelLoading || isReleasesLoading ? <Loading /> : associatedReleasesDisplay}</DialogContent>
<DialogContent>{isModelLoading ? <Loading /> : associatedReleasesDisplay}</DialogContent>
<DialogActions>
<Button variant='contained' onClick={onClose}>
Close
Expand Down
45 changes: 33 additions & 12 deletions frontend/src/entry/model/releases/FileDownload.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Done, Error, InfoOutlined, Refresh, Warning } from '@mui/icons-material'
import { Chip, Divider, IconButton, Link, Popover, Stack, Tooltip, Typography } from '@mui/material'
import { rerunFileScan, useGetFileScannerInfo } from 'actions/fileScanning'
import { useGetReleasesForModelId } from 'actions/release'
import prettyBytes from 'pretty-bytes'
import { Fragment, ReactElement, useCallback, useMemo, useState } from 'react'
import { Fragment, ReactElement, useCallback, useEffect, useMemo, useState } from 'react'
import Loading from 'src/common/Loading'
import UserDisplay from 'src/common/UserDisplay'
import AssociatedReleasesDialog from 'src/entry/model/releases/AssociatedReleasesDialog'
import useNotification from 'src/hooks/useNotification'
import MessageAlert from 'src/MessageAlert'
import { FileInterface, isFileInterface, ScanState } from 'types/types'
import { sortByCreatedAtDescending } from 'utils/arrayUtils'
import { formatDateString, formatDateTimeString } from 'utils/dateUtils'
import { getErrorMessage } from 'utils/fetcher'
import { plural } from 'utils/stringUtils'
Expand All @@ -28,6 +29,22 @@ interface ChipDetails {
export default function FileDownload({ modelId, file, showAssociatedReleases = false }: FileDownloadProps) {
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null)
const [associatedReleasesOpen, setAssociatedReleasesOpen] = useState(false)
const { releases, isReleasesLoading, isReleasesError } = useGetReleasesForModelId(modelId)
const [latestRelease, setLatestRelease] = useState('')

const sortedAssociatedReleases = useMemo(
() =>
releases
.filter((release) => isFileInterface(file) && release.fileIds.includes(file._id))
.sort(sortByCreatedAtDescending),
[file, releases],
)

useEffect(() => {
if (releases.length > 0 && sortedAssociatedReleases.length > 0) {
setLatestRelease(releases[0].semver)
}
}, [releases, sortedAssociatedReleases])

const sendNotification = useNotification()
const { scanners, isScannersLoading, isScannersError } = useGetFileScannerInfo()
Expand Down Expand Up @@ -164,7 +181,11 @@ export default function FileDownload({ modelId, file, showAssociatedReleases = f
return <MessageAlert message={isScannersError.info.message} severity='error' />
}

if (isScannersLoading) {
if (isReleasesError) {
return <MessageAlert message={isReleasesError.info.message} severity='error' />
}

if (isScannersLoading || isReleasesLoading) {
return <Loading />
}

Expand Down Expand Up @@ -201,15 +222,13 @@ export default function FileDownload({ modelId, file, showAssociatedReleases = f
)}
</Stack>
</Stack>
<Stack direction={{ sm: 'column', md: 'row' }} spacing={2} alignItems='center' justifyContent='space-between'>
<Stack direction='row'>
<Typography textOverflow='ellipsis' overflow='hidden' variant='caption' sx={{ mb: 2 }}>
Added by {<UserDisplay dn={file.createdAt.toString()} />} on
<Typography textOverflow='ellipsis' overflow='hidden' variant='caption' fontWeight='bold'>
{` ${formatDateString(file.createdAt.toString())}`}
</Typography>
</Typography>
</Stack>
<Stack direction={{ sm: 'column', md: 'row' }} spacing={0.5} alignItems='center' justifyContent='flex-start'>
Copy link
Member

@ARADDCC002 ARADDCC002 Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to break these two bits up as they're the same sentence. Instead I would remove this <Stack /> and replace it with:

Uploaded on <span style={{fontWeight: 'bold'}}>{${formatDateString(file.createdAt.toString())}}

<Typography variant='caption' sx={{ mb: 2 }}>
Uploaded on
</Typography>
<Typography variant='caption' fontWeight='bold'>
{`${formatDateString(file.createdAt.toString())}`}
</Typography>
</Stack>
</Stack>
)}
Expand All @@ -218,6 +237,8 @@ export default function FileDownload({ modelId, file, showAssociatedReleases = f
open={associatedReleasesOpen}
onClose={() => setAssociatedReleasesOpen(false)}
file={file}
latestRelease={latestRelease}
sortedAssociatedReleases={sortedAssociatedReleases}
/>
</>
)
Expand Down
Loading