Skip to content

Commit

Permalink
feat(card-image): remove domain from URL and use settings.currentInst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
Stéphane committed Oct 13, 2023
1 parent bf2c87b commit e3aaa29
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "../providers/Player";
import { useTranslation } from "react-i18next";
import { CardImage } from "./CardImage";
import { useSettings } from "../providers/Settings";

const useStyles = createStyles((theme) => ({
card: {
Expand Down Expand Up @@ -82,6 +83,7 @@ export const Card: React.FC<CardProps> = memo(
const { handlePlay, loading } = usePlayVideo();
const { classes } = useStyles();
const { t } = useTranslation();
const { currentInstance } = useSettings();

const image = video.videoThumbnails.find(
(thumbnail) => thumbnail.quality === "maxresdefault"
Expand All @@ -102,7 +104,11 @@ export const Card: React.FC<CardProps> = memo(
style={{ width: "100%" }}
onClick={() => handlePlay(video.videoId)}
>
<CardImage image={image} title={video.title}>
<CardImage
image={image}
domain={currentInstance?.uri}
title={video.title}
>
<Flex
align="center"
gap="xs"
Expand Down
6 changes: 5 additions & 1 deletion src/components/CardImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ const useStyles = createStyles((theme) => ({
interface CardImageProps {
image: VideoThumbnail;
title: string;
domain?: string;
children?: React.ReactNode;
}

export const CardImage: React.FC<CardImageProps> = ({
image,
title,
domain = "",
children,
}) => {
const { classes } = useStyles();

const domainUrl = image.url.startsWith("https") ? "" : domain;

return (
<Flex
className={classes.imageContainer}
align="flex-end"
justify="flex-end"
>
<Image
src={image?.url as string}
src={`${domainUrl}${image.url}`}
alt={title}
className={classes.image}
loading="lazy"
Expand Down
6 changes: 2 additions & 4 deletions src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Skeleton } from "@mantine/core";
import { memo, useState } from "react";
import { FC, ImgHTMLAttributes, memo, useState } from "react";

interface ImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {}

export const Image: React.FC<ImageProps> = memo(
export const Image: FC<ImgHTMLAttributes<HTMLImageElement>> = memo(
({ className, alt, ...props }) => {
const [loading, setLoading] = useState(true);

Expand Down
3 changes: 0 additions & 3 deletions src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ export const SearchBar = memo(() => {
handleSubmit({ q: value });
};

console.log(searchValues);
console.log(form.getInputProps("q"));

return (
<Flex align="center" gap={16} className={classes.container}>
<Form
Expand Down
35 changes: 35 additions & 0 deletions src/database/13102023_migrate_remove_card_image_url_domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { db } from ".";
import { Playlist } from "../types/interfaces/Playlist";

const DOMAIN_REGEX =
/^(?:https?:\/\/)?(?:[^@/\n]+@)?(?:www\.)?([^:/?\n]+\.+[^:/?\n]+)/gm;

const migration = () => {
const favoritePlaylist = db.queryAll("playlists", {
query: { title: "Favorites" },
})[0] as Playlist;

try {
const updatedVideos = favoritePlaylist.videos.map((video) => {
if (video.videoThumbnails) {
return {
...video,
videoThumbnails: video.videoThumbnails?.map((thumbnail) => ({
...thumbnail,
url: thumbnail.url.replace(DOMAIN_REGEX, ""),
})),
};
}
return video;
});

favoritePlaylist.videos = updatedVideos;

db.update("playlists", { title: "Favorites" }, () => favoritePlaylist);
db.commit();
} catch (error) {
console.log(error);
}
};

export default migration;
9 changes: 9 additions & 0 deletions src/database/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ export default (() => {
saveMigration("05092023_migrate_custom_instance");
} catch {}
}

if (
!migrationsName.includes("13102023_migrate_remove_card_image_url_domain")
) {
try {
require("./13102023_migrate_remove_card_image_url_domain").default();
saveMigration("13102023_migrate_remove_card_image_url_domain");
} catch {}
}
})();

0 comments on commit e3aaa29

Please sign in to comment.