Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Dec 12, 2024
1 parent 798b8cf commit d2a6a14
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/features/post/crosspost/CrosspostContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import { repeat } from "ionicons/icons";
import { arrowUpSharp } from "ionicons/icons";
import { chatbubbleOutline } from "ionicons/icons";
import { PostView } from "lemmy-js-client";
import { useEffect } from "react";

import { imageLoaded } from "#/features/media/imageSlice";
import useAspectRatio from "#/features/media/useAspectRatio";
import LargePostContents from "#/features/post/inFeed/large/LargePostContents";
import usePostSrc from "#/features/post/inFeed/usePostSrc";
import { cx } from "#/helpers/css";
import { formatNumber } from "#/helpers/number";
import { useAppDispatch } from "#/store";

import { CrosspostProps } from "./Crosspost";
import { useCopyPostAspectRatioIfNeeded } from "./useCopyPostAspectRatioIfNeeded";

import styles from "./CrosspostContents.module.css";

Expand All @@ -28,21 +24,7 @@ export default function CrosspostContents({
hasBeenRead,
post,
}: CrosspostContentsProps) {
const dispatch = useAppDispatch();
const postAspectRatio = useAspectRatio(usePostSrc(post));
const crosspostSrc = usePostSrc(crosspost);
const crosspostAspectRatio = useAspectRatio(crosspostSrc);

// Workaround to immediately copy over the aspect ratio of the original image
// to avoid flickering when the new crosspost image src loads
useEffect(() => {
if (!crosspostSrc) return;
if (postAspectRatio && !crosspostAspectRatio) {
dispatch(
imageLoaded({ src: crosspostSrc, aspectRatio: postAspectRatio }),
);
}
}, [crosspostSrc, postAspectRatio, crosspostAspectRatio, dispatch]);
useCopyPostAspectRatioIfNeeded(post, crosspost);

return (
<>
Expand Down
30 changes: 30 additions & 0 deletions src/features/post/crosspost/useCopyPostAspectRatioIfNeeded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PostView } from "lemmy-js-client";
import { useEffect } from "react";

import { imageLoaded } from "#/features/media/imageSlice";
import useAspectRatio from "#/features/media/useAspectRatio";
import usePostSrc from "#/features/post/inFeed/usePostSrc";
import { useAppDispatch } from "#/store";

/**
* Workaround to immediately copy over the aspect ratio of the original image
* to avoid flickering when the new crosspost image src loads
*/
export function useCopyPostAspectRatioIfNeeded(
post: PostView | undefined,
crosspost: PostView | undefined,
) {
const dispatch = useAppDispatch();
const postAspectRatio = useAspectRatio(usePostSrc(post));
const crosspostSrc = usePostSrc(crosspost);
const crosspostAspectRatio = useAspectRatio(crosspostSrc);

useEffect(() => {
if (!crosspostSrc) return;
if (postAspectRatio && !crosspostAspectRatio) {
dispatch(
imageLoaded({ src: crosspostSrc, aspectRatio: postAspectRatio }),
);
}
}, [crosspostSrc, postAspectRatio, crosspostAspectRatio, dispatch]);
}

0 comments on commit d2a6a14

Please sign in to comment.