Skip to content

Commit 5471e35

Browse files
authored
Fix Invalid time range issue (#686)
1 parent 7aab98d commit 5471e35

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

web-server/pages/api/internal/version.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Endpoint, nullSchema } from '@/api-helpers/global';
55
const dockerRepoName = 'middlewareeng/middleware';
66
const githubOrgName = 'middlewarehq';
77
const githubRepoName = 'middleware';
8+
const latestTagName = 'latest';
89

910
const endpoint = new Endpoint(nullSchema);
1011

@@ -26,13 +27,6 @@ type CheckNewVersionResponse = {
2627
current_docker_image_build_date: Date;
2728
};
2829

29-
type DockerHubAPIResponse = {
30-
count: number;
31-
next: string | null;
32-
previous: string | null;
33-
results: TagResult[];
34-
};
35-
3630
type TagResult = {
3731
creator: number;
3832
id: number;
@@ -69,7 +63,7 @@ type DockerImage = {
6963
type TagCompressed = {
7064
name: string;
7165
last_updated: string;
72-
digest: string;
66+
digest?: string;
7367
};
7468

7569
function getProjectVersionInfo(): ProjectVersionInfo {
@@ -82,15 +76,17 @@ function getProjectVersionInfo(): ProjectVersionInfo {
8276
};
8377
}
8478

85-
async function fetchDockerHubTags(): Promise<TagCompressed[]> {
86-
const dockerHubUrl = `https://hub.docker.com/v2/repositories/${dockerRepoName}/tags/`;
87-
const response = await axios.get<DockerHubAPIResponse>(dockerHubUrl);
79+
async function fetchDockerHubLatestTag(): Promise<TagCompressed> {
80+
const latestTagUrl = `https://hub.docker.com/v2/repositories/${dockerRepoName}/tags/${latestTagName}`;
81+
const latestTag = (await axios.get<TagResult>(latestTagUrl)).data;
82+
83+
const amdArchImage = latestTag.images.find((i) => i.architecture === 'amd64');
8884

89-
return response.data.results.map((tag) => ({
90-
name: tag.name,
91-
digest: tag.images[0].digest,
92-
last_updated: tag.last_updated
93-
}));
85+
return {
86+
name: latestTag.name,
87+
digest: amdArchImage?.digest,
88+
last_updated: latestTag.last_updated
89+
};
9490
}
9591

9692
function isUpdateAvailable({
@@ -122,16 +118,14 @@ function isUpdateAvailable({
122118
async function checkNewImageRelease(): Promise<CheckNewVersionResponse> {
123119
const versionInfo = getProjectVersionInfo();
124120

125-
const [dockerRemoteTags] = await Promise.all([fetchDockerHubTags()]);
121+
const latestTag = await fetchDockerHubLatestTag();
126122

127-
dockerRemoteTags.sort(
128-
(a, b) =>
129-
new Date(b.last_updated).getTime() - new Date(a.last_updated).getTime()
130-
);
131-
const latestTag = dockerRemoteTags[0];
132123
const latestRemoteDate = new Date(latestTag.last_updated);
133124

134-
const latestDockerImageLink = `https://hub.docker.com/layers/${dockerRepoName}/${latestTag.name}/images/${latestTag.digest}`;
125+
let latestDockerImageLink = `https://hub.docker.com/r/${dockerRepoName}/tags`;
126+
if (latestTag.digest) {
127+
latestDockerImageLink = `https://hub.docker.com/layers/${dockerRepoName}/${latestTag.name}/images/${latestTag.digest}`;
128+
}
135129

136130
const githubRepLink = `https://github.com/${githubOrgName}/${githubRepoName}`;
137131

web-server/src/layouts/ExtendedSidebarLayout/Sidebar/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
useTheme,
88
lighten
99
} from '@mui/material';
10-
import { format } from 'date-fns';
10+
import { format, isValid } from 'date-fns';
1111
import { useContext, useMemo } from 'react';
1212

1313
import { FlexBox } from '@/components/FlexBox';
@@ -67,10 +67,10 @@ const SidebarContent = () => {
6767

6868
const imageStatus = useSelector((s) => s.app.latestImageStatus);
6969

70-
const formattedDate = format(
71-
new Date(imageStatus.current_docker_image_build_date),
72-
'dd MMM yyyy HH:mm:ss'
73-
);
70+
const imageBuildDate = new Date(imageStatus?.current_docker_image_build_date);
71+
const formattedDate = isValid(imageBuildDate)
72+
? format(imageBuildDate, 'dd MMM yyyy HH:mm:ss')
73+
: 'Not Available';
7474

7575
return (
7676
<>

0 commit comments

Comments
 (0)