import * as ATPAPI from "@atproto/api"
import {
AppBskyEmbedDefs,
AppBskyEmbedExternal,
AppBskyEmbedImages,
AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia,
AppBskyEmbedVideo,
AppBskyFeedDefs,
AppBskyFeedPost,
AppBskyGraphDefs,
AtUri,
ModerationDecision,
} from "@atproto/api";
import * as React from "react";
import { useEffect, useRef, useState } from "react";
import ReactPlayer from "react-player";
import { FeedItemRenderAturiLoader } from "~/routes/profile.$did";
import type { LightboxProps } from "~/routes/profile.$did/post.$rkey.image.$i";
import { PollEmbed } from "./PollComponents";
import { UniversalPostRenderer, UniversalPostRendererATURILoader } from "./UniversalPostRenderer";
type Embed =
| AppBskyEmbedRecord.View
| AppBskyEmbedImages.View
| AppBskyEmbedVideo.View
| AppBskyEmbedExternal.View
| AppBskyEmbedRecordWithMedia.View
| { $type: string;[k: string]: unknown };
enum PostEmbedViewContext {
ThreadHighlighted = "ThreadHighlighted",
Feed = "Feed",
FeedEmbedRecordWithMedia = "FeedEmbedRecordWithMedia",
}
const stopgap = {
display: "flex",
justifyContent: "center",
padding: "32px 12px",
borderRadius: 12,
border: "1px solid rgba(161, 170, 174, 0.38)",
};
export function PostEmbeds({
embed,
moderation,
onOpen,
allowNestedQuotes,
viewContext,
salt,
navigate,
postid,
nopics,
lightboxCallback,
constellationLinks,
redactedLoading,
referral
}: {
embed?: Embed;
moderation?: ModerationDecision;
onOpen?: () => void;
allowNestedQuotes?: boolean;
viewContext?: PostEmbedViewContext;
salt: string;
navigate: (_: any) => void;
postid?: { did: string; rkey: string };
nopics?: boolean;
lightboxCallback?: (d: LightboxProps) => void;
constellationLinks?: any;
redactedLoading?: boolean;
referral?: string[];
}) {
function setLightboxIndex(number: number) {
navigate({
to: "/profile/$did/post/$rkey/image/$i",
params: {
did: postid?.did,
rkey: postid?.rkey,
i: number.toString(),
},
});
}
if (
AppBskyEmbedRecordWithMedia.isView(embed) &&
AppBskyEmbedRecord.isViewRecord(embed.record.record) &&
AppBskyFeedPost.isRecord(embed.record.record.value)
) {
const post: AppBskyFeedDefs.PostView = {
$type: "app.bsky.feed.defs#postView",
uri: embed.record.record.uri,
cid: embed.record.record.cid,
author: embed.record.record.author,
record: embed.record.record.value as { [key: string]: unknown },
embed: embed.record.record.embeds
? embed.record.record.embeds?.[0]
: undefined,
replyCount: embed.record.record.replyCount,
repostCount: embed.record.record.repostCount,
likeCount: embed.record.record.likeCount,
quoteCount: embed.record.record.quoteCount,
indexedAt: embed.record.record.indexedAt,
labels: embed.record.record.labels,
};
return (
{
e.stopPropagation();
const parsed = new AtUri(post.uri);
if (parsed) {
navigate({
to: "/profile/$did/post/$rkey",
params: { did: parsed.host, rkey: parsed.rkey },
});
}
}}
depth={1}
/>
);
}
if (AppBskyEmbedRecord.isView(embed)) {
const reallybaduri = (embed?.record as any)?.uri as string | undefined;
const reallybadaturi = reallybaduri ? new AtUri(reallybaduri) : undefined;
if (AppBskyFeedDefs.isGeneratorView(embed.record)) {
return feedgen placeholder
;
} else if (
!!reallybaduri &&
!!reallybadaturi &&
reallybadaturi.collection === "app.bsky.feed.generator"
) {
return (
);
}
if (AppBskyGraphDefs.isListView(embed.record)) {
return list placeholder
;
} else if (
!!reallybaduri &&
!!reallybadaturi &&
reallybadaturi.collection === "app.bsky.graph.list"
) {
return (
);
}
if (AppBskyGraphDefs.isStarterPackViewBasic(embed.record)) {
return starter pack card placeholder
;
} else if (
!!reallybaduri &&
!!reallybadaturi &&
reallybadaturi.collection === "app.bsky.graph.starterpack"
) {
return (
);
}
if (
AppBskyEmbedRecord.isViewRecord(embed.record) &&
AppBskyFeedPost.isRecord(embed.record.value)
) {
const post: AppBskyFeedDefs.PostView = {
$type: "app.bsky.feed.defs#postView",
uri: embed.record.uri,
cid: embed.record.cid,
author: embed.record.author,
record: embed.record.value as { [key: string]: unknown },
embed: embed.record.embeds ? embed.record.embeds?.[0] : undefined,
replyCount: embed.record.replyCount,
repostCount: embed.record.repostCount,
likeCount: embed.record.likeCount,
quoteCount: embed.record.quoteCount,
indexedAt: embed.record.indexedAt,
labels: embed.record.labels,
};
return (
{
e.stopPropagation();
const parsed = new AtUri(post.uri);
if (parsed) {
navigate({
to: "/profile/$did/post/$rkey",
params: { did: parsed.host, rkey: parsed.rkey },
});
}
}}
depth={1}
/>
);
} if (AppBskyEmbedRecord.isViewNotFound(embed.record)) {
return (
)
} else {
console.log("what the hell is a ", embed);
return <>sorry>;
}
}
if (AppBskyEmbedImages.isView(embed)) {
const { images } = embed;
const lightboxImages = images.map((img) => ({
src: img.fullsize,
alt: img.alt,
}));
if (lightboxCallback) {
lightboxCallback({ images: lightboxImages });
}
if (nopics) return;
if (images.length > 0) {
if (images.length === 1) {
const image = images[0];
return (
{
const { width, height } = image.aspectRatio;
const ratio = width / height;
return ratio < 0.5 ? "1 / 2" : `${width} / ${height}`;
})()
: "1 / 1",
borderRadius: 12,
overflow: "hidden",
}}
className="border border-gray-200 dark:border-gray-800 was7 bg-gray-200 dark:bg-gray-900"
>
{redactedLoading ? (
) : (

{
e.stopPropagation();
setLightboxIndex(0);
}}
/>
)}
);
}
if (images.length === 2) {
return (
{images.map((img, i) => (
{redactedLoading ? (
) : (

{
e.stopPropagation();
setLightboxIndex(i);
}}
/>
)}
))}
);
}
if (images.length === 3) {
return (
{redactedLoading ? (
) : (
![{images[0].alt}]({images[0].fullsize})
{
e.stopPropagation();
setLightboxIndex(0);
}}
/>
)}
{[1, 2].map((i) => (
{redactedLoading ? (
) : (
![{images[i].alt}]({images[i].fullsize})
{
e.stopPropagation();
setLightboxIndex(i + 1);
}}
/>
)}
))}
);
}
if (images.length === 4) {
return (
{images.map((img, i) => (
{redactedLoading ? (
) : (

{
e.stopPropagation();
setLightboxIndex(i);
}}
/>
)}
))}
);
}
return image count more than one placeholder
;
}
}
if (AppBskyEmbedExternal.isView(embed)) {
const pollLinks = constellationLinks?.links?.["app.reddwarf.embed.poll"];
const hasPollLink = pollLinks && Object.keys(pollLinks).length > 0;
const isfromappview = referral?.includes("appview")
if ((hasPollLink || isfromappview) && postid) {
// warning: i gave up and warpped it in a div lmao
return (
);
}
const link = embed.external;
return (
);
}
if (AppBskyEmbedVideo.isView(embed)) {
if (nopics) return;
const playlist = embed.playlist;
return (
);
}
return ;
}
export type embedtryfall = {
embed: ATPAPI.AppBskyEmbedExternal.View,
onOpen?: () => void;
}
export function ExternalLinkEmbed({
link,
onOpen,
style,
redactedLoading,
referral
}: {
link: AppBskyEmbedExternal.ViewExternal;
onOpen?: () => void;
style?: React.CSSProperties;
redactedLoading?: boolean;
referral?: string[];
}) {
//const fromappview = referral?.includes("appview")
//const []
const { uri, title, description, thumb } = link;
const thumbAspectRatio = 1.91;
const titleStyle = {
fontSize: 16,
fontWeight: 700,
marginBottom: 4,
wordBreak: "break-word",
textAlign: "left",
maxHeight: "4em",
display: "-webkit-box",
WebkitBoxOrient: "vertical",
overflow: "hidden",
WebkitLineClamp: 2,
};
const descriptionStyle = {
fontSize: 14,
marginBottom: 8,
wordBreak: "break-word",
textAlign: "left",
maxHeight: "5em",
display: "-webkit-box",
WebkitBoxOrient: "vertical",
overflow: "hidden",
WebkitLineClamp: 3,
};
const linkStyle = {
textDecoration: "none",
wordBreak: "break-all",
textAlign: "left",
};
const containerStyle = {
display: "flex",
flexDirection: "column",
borderRadius: 12,
maxWidth: "100%",
overflow: "hidden",
...style,
};
return (
{
e.stopPropagation();
if (onOpen) onOpen();
}}
style={linkStyle as React.CSSProperties}
className="text-gray-500 dark:text-gray-400"
>
{thumb && (
{redactedLoading ? (
) : (

)}
)}
);
}
export const SmartHLSPlayer = ({
url,
thumbnail,
aspect,
redactedLoading,
}: {
url: string;
thumbnail?: string;
aspect?: AppBskyEmbedDefs.AspectRatio;
redactedLoading?: boolean;
}) => {
const [playing, setPlaying] = useState(false);
const containerRef = useRef(null);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting && playing) {
setPlaying(false);
}
},
{
root: null,
threshold: 0.25,
},
);
if (containerRef.current) {
observer.observe(containerRef.current);
}
return () => {
if (containerRef.current) {
observer.unobserve(containerRef.current);
}
};
}, [playing]);
return (
{!playing && (
<>
{redactedLoading ? (
) : (

{
e.stopPropagation();
if (redactedLoading) return;
setPlaying(true);
}}
/>
)}
{
e.stopPropagation();
if (redactedLoading) return;
setPlaying(true);
}}
style={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
color: "white",
pointerEvents: "none",
userSelect: "none",
}}
//className="text-shadow-md"
>
>
)}
{playing && (
)}
);
};
function getDomain(url: string) {
try {
const { hostname } = new URL(url);
return hostname;
} catch (e) {
if (!url.startsWith("http")) {
try {
const { hostname } = new URL("http://" + url);
return hostname;
} catch {
return null;
}
}
return null;
}
}