Skip to content

Commit

Permalink
fix: Weird behavior with expo-image
Browse files Browse the repository at this point in the history
- Sometimes, when we put an invalid uri as the source, the placeholder doesn't get applied, instead, the last valid image displayed by any expo image components is used
  • Loading branch information
cyanChill committed Dec 13, 2024
1 parent 00f1411 commit 10ed55e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions mobile/src/modules/media/components/MediaImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Folder } from "@/icons";

import { Colors } from "@/constants/Styles";
import { cn } from "@/lib/style";
import { ReservedPlaylists } from "../constants";
import { ReservedNames, ReservedPlaylists } from "../constants";
import type { MediaType } from "../types";

// https://www.nativewind.dev/v4/api/css-interop
Expand Down Expand Up @@ -54,7 +54,7 @@ export function MediaImage({

return (
<Image
source={Array.isArray(source) ? null : source}
source={getUsedImage({ source, type })}
placeholder={
type === "artist"
? require("@/resources/images/face-glyph.png")
Expand All @@ -69,6 +69,23 @@ export function MediaImage({
);
}

/** Helper to return the correct image displayed in `<MediaImage />`. */
function getUsedImage(args: {
source: MediaImage.ImageSource | Array<string | null>;
type: Omit<MediaType, "playlist">;
}) {
if (
Array.isArray(args.source) ||
args.source === null ||
ReservedNames.has(args.source)
) {
if (args.type === "artist")
return require("@/resources/images/face-glyph.png");
return require("@/resources/images/music-glyph.png");
}
return args.source;
}

/** Only used to represent a playlist. */
function CollageImage({
size,
Expand Down

0 comments on commit 10ed55e

Please sign in to comment.