Skip to content

Commit

Permalink
Display domain on compact posts
Browse files Browse the repository at this point in the history
  • Loading branch information
bishtawi committed Mar 20, 2024
1 parent bdf1f06 commit 84e7cfa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/features/post/inFeed/compact/CompactPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useCrosspostUrl from "../../shared/useCrosspostUrl";
import { useInModqueue } from "../../../../routes/pages/shared/ModqueuePage";
import { PageTypeContext } from "../../../feed/PageTypeContext";
import { styled } from "@linaria/react";
import { parseUrlForDisplay } from "../../../../helpers/url";

const Container = styled.div`
width: 100%;
Expand Down Expand Up @@ -126,6 +127,12 @@ const EndDetails = styled.div`
margin-left: auto;
`;

const Domain = styled.div`
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;

export default function CompactPost({ post }: PostProps) {
const compactThumbnailPositionType = useAppSelector(
(state) => state.settings.appearance.compact.thumbnailsPosition,
Expand All @@ -146,6 +153,11 @@ export default function CompactPost({ post }: PostProps) {
post.read;
const nsfw = useMemo(() => isNsfw(post), [post]);

const [domain] = useMemo(
() => (post.post.url ? parseUrlForDisplay(post.post.url) : []),
[post],
);

return (
<ModeratableItem itemView={post}>
<Container>
Expand Down Expand Up @@ -186,6 +198,7 @@ export default function CompactPost({ post }: PostProps) {
/>
)}
</From>
{domain && <Domain>{domain}</Domain>}
<ActionsContainer>
<PreviewStats post={post} />
{inModqueue ? (
Expand Down
28 changes: 5 additions & 23 deletions src/features/shared/Url.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { styled } from "@linaria/react";
import { useMemo } from "react";
import { parseUrlForDisplay } from "../../helpers/url";

const Rest = styled.span`
opacity: 0.6;
Expand All @@ -10,29 +11,10 @@ interface UrlProps {
}

export default function Url({ children }: UrlProps) {
const [domain, rest] = useMemo(() => {
let url;

try {
url = new URL(children);
} catch (error) {
console.error(error);
return [];
}

const protocolPrefix = url.protocol === "https:" ? "" : `${url.protocol}//`;
const normalizedHost = (() => {
if (protocolPrefix) return url.host;
if (url.host.startsWith("www.")) return url.host.slice(4);

return url.host;
})();

return [
`${protocolPrefix}${normalizedHost}`,
`${url.pathname}${url.search}${url.hash}`,
];
}, [children]);
const [domain, rest] = useMemo(
() => parseUrlForDisplay(children),
[children],
);

if (!domain || !rest) return;

Expand Down
25 changes: 25 additions & 0 deletions src/helpers/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,28 @@ export function getVideoSrcForUrl(url: string) {
export function stripProtocol(url: string): string {
return url.replace(/^https?:\/\//, "");
}

export function parseUrlForDisplay(url: string): string[] {
let parsedUrl;

try {
parsedUrl = new URL(url);
} catch (error) {
console.error(error);
return [];
}

const protocolPrefix =
parsedUrl.protocol === "https:" ? "" : `${parsedUrl.protocol}//`;
const normalizedHost = (() => {
if (protocolPrefix) return parsedUrl.host;
if (parsedUrl.host.startsWith("www.")) return parsedUrl.host.slice(4);

return parsedUrl.host;
})();

return [
`${protocolPrefix}${normalizedHost}`,
`${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`,
];
}

0 comments on commit 84e7cfa

Please sign in to comment.