-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/features/post/crosspost/useCopyPostAspectRatioIfNeeded.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |