Skip to content

Commit

Permalink
✨ Blur video on nsfw labeled content (#191)
Browse files Browse the repository at this point in the history
* ✨ Blur video on nsfw labeled content

* 💄 Fix video resizing
  • Loading branch information
foysalit committed Sep 19, 2024
1 parent 1637ab8 commit a9debb6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 3 additions & 2 deletions components/common/posts/PostsFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ export function PostEmbeds({ item }: { item: AppBskyFeedDefs.FeedViewPost }) {
? item.post.embed.media
: item.post.embed

const imageRequiresBlur = doesLabelNeedBlur(
const mediaRequiresBlur = doesLabelNeedBlur(
item.post.labels?.map(({ val }) => val),
)
const imageClassName = classNames(
`border border-gray-200 rounded`,
imageRequiresBlur ? 'blur-sm hover:blur-none' : '',
mediaRequiresBlur ? 'blur-sm hover:blur-none' : '',
)

if (AppBskyEmbedVideo.isView(embed)) {
Expand All @@ -240,6 +240,7 @@ export function PostEmbeds({ item }: { item: AppBskyFeedDefs.FeedViewPost }) {
source={embed.playlist}
thumbnail={embed.thumbnail}
alt={embed.alt}
shouldBlur={mediaRequiresBlur}
captions={captions ? (captions as AppBskyEmbedVideo.Caption[]) : []}
/>
</div>
Expand Down
4 changes: 2 additions & 2 deletions components/common/posts/PostsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ function PostEmbeds({ item }: { item: AppBskyFeedDefs.FeedViewPost }) {
const embed = AppBskyEmbedRecordWithMedia.isView(item.post.embed)
? item.post.embed.media
: item.post.embed
const imageRequiresBlur = doesLabelNeedBlur(
const mediaRequiresBlur = doesLabelNeedBlur(
item.post.labels?.map(({ val }) => val),
)
const imageClassName = classNames(
`border border-gray-200 rounded`,
imageRequiresBlur ? 'blur-sm hover:blur-none' : '',
mediaRequiresBlur ? 'blur-sm hover:blur-none' : '',
)

if (AppBskyEmbedImages.isView(embed)) {
Expand Down
16 changes: 14 additions & 2 deletions components/common/video/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ export default function VideoPlayer({
alt,
thumbnail,
captions,
shouldBlur = false,
}: {
source: string
alt?: string
thumbnail?: string
captions: AppBskyEmbedVideo.Caption[]
shouldBlur?: boolean
}) {
const [hls] = useState(() => new Hls())
const [isUnsupported, setIsUnsupported] = useState(false)
const [isHovered, setIsHovered] = useState(false)
const figId = useId()
const ref = useRef<HTMLVideoElement>(null)

Expand All @@ -41,19 +44,28 @@ export default function VideoPlayer({
}, [source, hls])

return (
<figure>
<figure
className="flex-1"
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<video
poster={thumbnail}
ref={ref}
style={{ flex: 1 }}
playsInline
preload="none"
controls
loop
muted
crossOrigin="anonymous"
className={`w-full flex-1 transition-all duration-300 ease-in-out ${
isHovered || !shouldBlur ? 'blur-none' : 'blur-md'
}`}
aria-labelledby={alt ? figId : undefined}
>
{!isHovered && shouldBlur && (
<div className="absolute inset-0 flex items-center justify-center bg-black bg-opacity-50 text-white text-xl font-bold"></div>
)}
{isUnsupported && (
<p>
Your browser does not seem to support HLS videos. Please switch to a
Expand Down

0 comments on commit a9debb6

Please sign in to comment.