Skip to content

Commit

Permalink
fix(image) avoid null pointer exception on custom images, that have n…
Browse files Browse the repository at this point in the history
…o properties set. fixes #716

Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Mar 28, 2024
1 parent 47c1f06 commit 03a4ed7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/pages/images/ImageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ const ImageList: FC = () => {
const filteredImages = images.filter(
(item) =>
!query ||
item.properties.description.toLowerCase().includes(query.toLowerCase()) ||
(item.properties?.description ?? "")
.toLowerCase()
.includes(query.toLowerCase()) ||
item.aliases
.map((alias) => alias.name)
.join(", ")
Expand All @@ -114,12 +116,13 @@ const ImageList: FC = () => {
);

const imageAlias = image.aliases.map((alias) => alias.name).join(", ");
const description = image.properties?.description ?? image.fingerprint;

return {
name: image.fingerprint,
columns: [
{
content: image.properties.description,
content: description,
role: "cell",
"aria-label": "Name",
},
Expand Down Expand Up @@ -167,7 +170,7 @@ const ImageList: FC = () => {
},
],
sortData: {
name: image.properties.description.toLowerCase(),
name: description.toLowerCase(),
alias: imageAlias.toLowerCase(),
architecture: image.architecture,
public: image.public,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/images/ImageName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ImageName: FC<Props> = ({ id, project }) => {
queryFn: () => fetchImage(id, project),
});

const label = image?.properties.description
const label = image?.properties?.description
? image.properties.description
: id;

Expand Down
16 changes: 6 additions & 10 deletions src/pages/images/actions/DeleteImageBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const DeleteImageBtn: FC<Props> = ({ image, project }) => {
const [isLoading, setLoading] = useState(false);
const queryClient = useQueryClient();

const description = image.properties?.description ?? image.fingerprint;

const handleDelete = () => {
setLoading(true);
void deleteImage(image, project)
Expand All @@ -31,23 +33,18 @@ const DeleteImageBtn: FC<Props> = ({ image, project }) => {
void queryClient.invalidateQueries({
queryKey: [queryKeys.projects, project],
});
toastNotify.success(
`Image ${image.properties.description} deleted.`,
);
toastNotify.success(`Image ${description} deleted.`);
},
(msg) =>
toastNotify.failure(
`Image ${image.properties.description} deletion failed`,
`Image ${description} deletion failed`,
new Error(msg),
),
() => setLoading(false),
),
)
.catch((e) => {
toastNotify.failure(
`Image ${image.properties.description} deletion failed`,
e,
);
toastNotify.failure(`Image ${description} deletion failed`, e);
setLoading(false);
});
};
Expand All @@ -59,8 +56,7 @@ const DeleteImageBtn: FC<Props> = ({ image, project }) => {
title: "Confirm delete",
children: (
<p>
This will permanently delete image{" "}
<b>{image.properties.description}</b>.<br />
This will permanently delete image <b>{description}</b>.<br />
This action cannot be undone, and can result in data loss.
</p>
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface LxdImageAlias {
export interface LxdImage {
fingerprint: string;
public: boolean;
properties: {
properties?: {
description: string;
os: string;
release: string;
Expand Down
4 changes: 2 additions & 2 deletions src/util/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export const localLxdToRemoteImage = (image: LxdImage): RemoteImage => {
return {
aliases: image.update_source?.alias ?? image.fingerprint,
arch: image.architecture === "x86_64" ? "amd64" : image.architecture,
os: image.properties.os,
os: image.properties?.os ?? "",
created_at: new Date(image.uploaded_at).getTime(),
release: image.properties.release,
release: image.properties?.release ?? "",
server: image.update_source?.server,
type: image.type,
};
Expand Down

0 comments on commit 03a4ed7

Please sign in to comment.