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

adding domain preview #627

Merged
merged 3 commits into from
Jul 13, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { HStack, Pressable, Text, useTheme, View, VStack } from "native-base";
import { useWindowDimensions } from "react-native";
import { useNavigation } from "@react-navigation/native";
import { NativeStackNavigationProp } from "@react-navigation/native-stack";

import useFeedItem from "../../../../../hooks/feeds/useFeedItem";
import {
ExtensionType,
getLinkInfo,
getBaseUrl,
} from "../../../../../helpers/LinkHelper";
import { setResponseTo } from "../../../../../slices/comments/newCommentSlice";
import { useAppDispatch, useAppSelector } from "../../../../../../store";
import CompactFeedItemThumbnail from "./CompactFeedItemThumbnail";
Expand Down Expand Up @@ -56,6 +62,11 @@ function CompactFeedItem({
const fontModifier = fontSizeMap[fontSize];
const FONT_SIZE = isSystemTextSize ? 15 / fontScale : 15 + fontModifier;

const linkInfo = getLinkInfo(post.post.url);
const showLink =
linkInfo.extType === ExtensionType.VIDEO ||
linkInfo.extType === ExtensionType.GENERIC;

// TODO Memoize this properly
return (
<View flex={1} my={0.5}>
Expand Down Expand Up @@ -92,7 +103,15 @@ function CompactFeedItem({
: theme.colors.app.textPrimary
}
>
{post.post.name}
{post.post.name}{" "}
{showLink && (
<Text
fontSize={FONT_SIZE - 1}
color={theme.colors.app.textSecondary}
>
({getBaseUrl(linkInfo.link, true)})
</Text>
)}
</Text>

<CompactFeedItemFooter post={post} />
Expand Down
12 changes: 11 additions & 1 deletion src/helpers/LinkHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,17 @@ export const openLink = (
}
};

export const getBaseUrl = (link?: string): string => {
export const getBaseUrl = (link?: string, noSubdomain = false): string => {
if (noSubdomain) {
const domain = link.replace(/^(https?:\/\/)?(www\.)?/i, "").split("/")[0];
const parts = domain.split(".").reverse();

if (parts.length > 2 && parts[1].length <= 3) {
return `${parts[2]}.${parts[1]}.${parts[0]}`;
}

return `${parts[1]}.${parts[0]}`;
}
const regex = /^(?:https?:\/\/)?([^/]+)/;
return link ? link.match(regex)[1] : null;
};