Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nsfw blur sometimes not applied for crossposts in remote communities #1771

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/features/media/InlineMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CSSProperties } from "react";

import Media, { MediaProps } from "#/features/media/Media";
import { cx } from "#/helpers/css";
import useLatch from "#/helpers/useLatch";
import { useAppDispatch } from "#/store";

import { IMAGE_FAILED, imageFailed, imageLoaded } from "./imageSlice";
Expand All @@ -28,15 +27,7 @@ export default function InlineMedia({
...props
}: InlineMediaProps) {
const dispatch = useAppDispatch();
const [mediaRef, currentAspectRatio] = useMediaLoadObserver(src);

/**
* Cross posts have different image thumbnail url when loaded, so prevent resizing by latching
*
* If the new image is different size (or errors), it will be properly updated then
* (IMAGE_FAILED is truthy)
*/
const aspectRatio = useLatch(currentAspectRatio);
const [mediaRef, aspectRatio] = useMediaLoadObserver(src);

function buildPlaceholderState() {
if (aspectRatio === IMAGE_FAILED) return "error";
Expand Down
58 changes: 8 additions & 50 deletions src/features/post/crosspost/Crosspost.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { IonIcon, IonSkeletonText } from "@ionic/react";
import { arrowUpSharp, chatbubbleOutline, repeat } from "ionicons/icons";
import { PostView } from "lemmy-js-client";

import LargePostContents from "#/features/post/inFeed/large/LargePostContents";
import { cx } from "#/helpers/css";
import { formatNumber } from "#/helpers/number";

import CrosspostContainer from "./CrosspostContainer";
import CrosspostContents from "./CrosspostContents";

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

interface CrosspostProps {
export interface CrosspostProps {
post: PostView;
url: string;
className?: string;
Expand All @@ -24,50 +21,11 @@ export default function Crosspost(props: CrosspostProps) {
className={cx(styles.container, props.className)}
>
{({ crosspost, hasBeenRead }) => (
<>
{crosspost ? (
<div
className={cx(
styles.title,
hasBeenRead ? styles.titleRead : undefined,
)}
>
{crosspost.post.name}
</div>
) : (
<IonSkeletonText />
)}
<LargePostContents post={crosspost ?? props.post} />
<div
className={cx(
styles.bottom,
hasBeenRead ? styles.bottomRead : undefined,
)}
>
<IonIcon className={styles.crosspostIcon} icon={repeat} />
{crosspost ? (
crosspost.community.title
) : (
<IonSkeletonText className={styles.communityIonSkeletonText} />
)}
<div className={styles.stat}>
<IonIcon icon={arrowUpSharp} />{" "}
{crosspost ? (
formatNumber(crosspost.counts.score)
) : (
<IonSkeletonText className={styles.statIonSkeletonText} />
)}
</div>
<div className={styles.stat}>
<IonIcon icon={chatbubbleOutline} />{" "}
{crosspost ? (
formatNumber(crosspost.counts.comments)
) : (
<IonSkeletonText className={styles.statIonSkeletonText} />
)}
</div>
</div>
</>
<CrosspostContents
{...props}
crosspost={crosspost}
hasBeenRead={hasBeenRead}
/>
)}
</CrosspostContainer>
);
Expand Down
75 changes: 75 additions & 0 deletions src/features/post/crosspost/CrosspostContents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { IonIcon } from "@ionic/react";
import { IonSkeletonText } from "@ionic/react";
import { repeat } from "ionicons/icons";
import { arrowUpSharp } from "ionicons/icons";
import { chatbubbleOutline } from "ionicons/icons";
import { PostView } from "lemmy-js-client";

import LargePostContents from "#/features/post/inFeed/large/LargePostContents";
import { cx } from "#/helpers/css";
import { formatNumber } from "#/helpers/number";

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

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

interface CrosspostContentsProps extends CrosspostProps {
crosspost: PostView | undefined;
hasBeenRead: boolean;
}

export default function CrosspostContents({
crosspost,
hasBeenRead,
post,
}: CrosspostContentsProps) {
useCopyPostAspectRatioIfNeeded(post, crosspost);

return (
<>
{crosspost ? (
<div
className={cx(
styles.title,
hasBeenRead ? styles.titleRead : undefined,
)}
>
{crosspost.post.name}
</div>
) : (
<IonSkeletonText />
)}
<LargePostContents post={crosspost ?? post} />
<div
className={cx(
styles.bottom,
hasBeenRead ? styles.bottomRead : undefined,
)}
>
<IonIcon className={styles.crosspostIcon} icon={repeat} />
{crosspost ? (
crosspost.community.title
) : (
<IonSkeletonText className={styles.communityIonSkeletonText} />
)}
<div className={styles.stat}>
<IonIcon icon={arrowUpSharp} />{" "}
{crosspost ? (
formatNumber(crosspost.counts.score)
) : (
<IonSkeletonText className={styles.statIonSkeletonText} />
)}
</div>
<div className={styles.stat}>
<IonIcon icon={chatbubbleOutline} />{" "}
{crosspost ? (
formatNumber(crosspost.counts.comments)
) : (
<IonSkeletonText className={styles.statIonSkeletonText} />
)}
</div>
</div>
</>
);
}
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]);
}
8 changes: 6 additions & 2 deletions src/features/post/inFeed/usePostSrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { findUrlMediaType } from "#/helpers/url";
import useSupported from "#/helpers/useSupported";
import { useAppSelector } from "#/store";

export default function usePostSrc(post: PostView): string | undefined {
export default function usePostSrc(
post: PostView | undefined,
): string | undefined {
const thumbnailIsFullsize = useSupported("Fullsize thumbnails");

const src = getPostMedia(post, thumbnailIsFullsize);
Expand All @@ -22,9 +24,11 @@ export default function usePostSrc(post: PostView): string | undefined {
}

function getPostMedia(
post: PostView,
post: PostView | undefined,
thumbnailIsFullsize: boolean,
): [string] | [string, string] | undefined {
if (!post) return;

if (post.post.url) {
const isUrlMedia = findUrlMediaType(
post.post.url,
Expand Down
13 changes: 0 additions & 13 deletions src/helpers/useLatch.ts

This file was deleted.

Loading