Skip to content

Commit 7ec1102

Browse files
committed
Added working share buttons
1 parent f02e327 commit 7ec1102

10 files changed

Lines changed: 135 additions & 10 deletions

File tree

app/(tabs)/profile/[user_id].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default function OtherProfileScreen() {
5656

5757
), [currentUser])
5858

59-
async function showPosts(UID) {
59+
async function showPosts(UID: string) {
6060
const c = collection(db, "posts");
6161
const q = query(c,
6262
where("uid", "==", UID),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"expo": "53.0.22",
2626
"expo-auth-session": "~6.2.1",
2727
"expo-blur": "~14.1.5",
28+
"expo-clipboard": "~7.1.5",
2829
"expo-constants": "~17.1.7",
2930
"expo-dev-client": "^5.2.4",
3031
"expo-device": "^7.1.4",

src/components/containers/post.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const Post = memo(function Post({ id, user_uid, media, used_media, message, uid,
9090
</Pressable>
9191
</View> */}
9292
<ThreeDots
93+
postId={id}
9394
data={[
9495
(uid == user_uid) ?
9596
{ text: "Delete Post", func: () => { deletePost(id) }, icon: "delete" } :
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import OnlyIconButton from "@components/inputs/onlyIconButton"
2+
import { sharePost, sharePostToFacebook, sharePostToReddit, sharePostToWhatsapp } from "@utils/otherUtils"
3+
import { View } from "react-native"
4+
5+
interface Props {
6+
postId: string
7+
}
8+
9+
export default function ShareBtns({ postId }: Props) {
10+
11+
return (
12+
<View style={{ gap: 10, width: "100%", flexDirection: "row" }}>
13+
<OnlyIconButton
14+
func={() => { sharePostToWhatsapp(postId) }}
15+
icon='whatsapp'
16+
/>
17+
18+
<OnlyIconButton
19+
func={() => { sharePostToFacebook(postId) }}
20+
icon='facebook'
21+
/>
22+
23+
<OnlyIconButton
24+
func={() => { sharePostToReddit(postId) }}
25+
icon='reddit'
26+
/>
27+
28+
<OnlyIconButton
29+
func={() => { }}
30+
icon='link-variant'
31+
/>
32+
33+
<OnlyIconButton
34+
func={() => { sharePost(postId) }}
35+
icon='dots-horizontal'
36+
/>
37+
</View>
38+
)
39+
}

src/components/display/threeDots.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import { Pressable, View } from 'react-native';
66

77

88
interface Props {
9-
data: BottomSheetItem[]
9+
data: BottomSheetItem[],
10+
postId: string
1011
}
1112

12-
export default function ThreeDots({ data }: Props) {
13+
export default function ThreeDots({ data, postId }: Props) {
1314
const [isVisible, setVisible] = useState(false);
14-
const { setSheetData, closeSheet, expandSheet } = useBottomSheetContext();
15+
const { setSheetData, setExtraData, closeSheet, expandSheet } = useBottomSheetContext();
1516

1617
function toggleSheet() {
1718
setSheetData(data);
19+
setExtraData({ postId: postId });
1820
setVisible(!isVisible);
1921
if (isVisible) {
2022
closeSheet();

src/components/inputs/onlyIconButton.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//components
2-
import { ViewStyle, Pressable, StyleProp, StyleSheet } from "react-native"
3-
import MaterialDesignIcons from "@react-native-vector-icons/material-design-icons"
2+
import MaterialDesignIcons from "@react-native-vector-icons/material-design-icons";
3+
import { Pressable, StyleProp, StyleSheet, ViewStyle } from "react-native";
44

55

66
type Props = {
77
func: () => void;
88
style?: StyleProp<ViewStyle>;
9-
icon: "account-plus-outline" | "arrow-left" | "magnify";
9+
icon: "account-plus-outline" | "arrow-left" | "magnify" | "whatsapp" | "facebook" | "reddit" | "link-variant" | "dots-horizontal";
1010
}
1111

1212
export default function OnlyIconButton({ func, style, icon }: Props) {

src/contexts/BottomSheetContext.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
import ShareBtns from '@components/containers/shareBtns';
12
import CustomText from '@components/display/customText';
23
import BottomSheet, { BottomSheetBackdrop, BottomSheetView } from '@gorhom/bottom-sheet';
34
import MaterialDesignIcons from '@react-native-vector-icons/material-design-icons';
4-
import { BottomSheetItem } from '@utils/types';
5+
import { BottomSheetData, BottomSheetItem } from '@utils/types';
56
import React, { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
67
import { StyleSheet } from 'react-native';
78
import { Pressable } from 'react-native-gesture-handler';
89

910
type sheetContextType = {
1011
setSheetData: (data: BottomSheetItem[]) => void;
12+
setExtraData: (data: BottomSheetData) => void;
1113
closeSheet: () => void;
1214
expandSheet: () => void;
1315
}
1416
const BottomSheetContext = createContext<sheetContextType | null>(null);
1517

1618
export function BottomSheetProvider({ children }: { children: React.ReactNode }) {
1719
const [data, setData] = useState<BottomSheetItem[]>([]);
20+
const [extraData, setEData] = useState<BottomSheetData>();
1821
const bottomSheetRef = useRef<BottomSheet>(null);
1922
const snapPoints = useMemo(() => ["25%", "50%"], []);
2023

@@ -32,6 +35,10 @@ export function BottomSheetProvider({ children }: { children: React.ReactNode })
3235
function setSheetData(data: BottomSheetItem[]) {
3336
setData(data);
3437
}
38+
39+
function setExtraData(data: BottomSheetData) {
40+
setEData(data);
41+
}
3542
function closeSheet() {
3643
bottomSheetRef.current?.close();
3744
}
@@ -44,6 +51,7 @@ export function BottomSheetProvider({ children }: { children: React.ReactNode })
4451
<BottomSheetContext.Provider
4552
value={{
4653
setSheetData,
54+
setExtraData,
4755
closeSheet,
4856
expandSheet
4957
}}
@@ -60,6 +68,7 @@ export function BottomSheetProvider({ children }: { children: React.ReactNode })
6068

6169
>
6270
<BottomSheetView style={styles.contentContainer}>
71+
<ShareBtns postId={extraData ? extraData.postId : ""} />
6372
{
6473
data.map((item: BottomSheetItem) => (
6574
<Pressable onPress={item.func} key={item.text} style={styles.button}>
@@ -89,7 +98,8 @@ const styles = StyleSheet.create({
8998
backgroundColor: "#17171d",
9099
borderColor: "#25252f",
91100
flex: 1,
92-
padding: 36,
101+
paddingVertical: 10,
102+
paddingHorizontal: 20,
93103
alignItems: 'flex-start',
94104
zIndex: 1,
95105
},

src/utils/otherUtils.tsx

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { auth, db } from "@auth/firebase";
2+
import * as Clipboard from 'expo-clipboard';
3+
import * as Linking from 'expo-linking';
24
import { addDoc, collection, deleteDoc, doc, getCountFromServer, getDoc, increment, serverTimestamp, setDoc, updateDoc } from "firebase/firestore";
5+
import { Alert, Share } from "react-native";
36
import { extractUrl } from "./stringTimeUtils";
47

58
export async function uploadToHc(urls: string[]) {
@@ -185,4 +188,64 @@ export async function deletePost(postId: string) {
185188
} catch (e) {
186189
console.log("Couldn't delete post", e);
187190
}
188-
}
191+
}
192+
193+
194+
export async function sharePost(id: string) {
195+
const redirectUrl = Linking.createURL(`/comments/${id}`);
196+
const cleanedUrl = redirectUrl.replace("hacknet:///", "https://hacknet-web.vercel.app/")
197+
try {
198+
await Share.share({
199+
message: `Check out this post! ${cleanedUrl}`
200+
});
201+
} catch (e: any) {
202+
if (e) {
203+
console.log(e.message)
204+
}
205+
}
206+
}
207+
export async function shareToClipboard(id: string) {
208+
const redirectUrl = Linking.createURL(`/comments/${id}`);
209+
const cleanedUrl = redirectUrl.replace("hacknet:///", "https://hacknet-web.vercel.app/")
210+
Clipboard.setStringAsync(cleanedUrl);
211+
}
212+
213+
export async function sharePostToWhatsapp(id: string) {
214+
const redirectUrl = Linking.createURL(`/comments/${id}`);
215+
const cleanedUrl = redirectUrl.replace("hacknet:///", "https://hacknet-web.vercel.app/")
216+
const msg = `Check out this post! ${cleanedUrl}`;
217+
const url = `whatsapp://send?text=${encodeURIComponent(msg)}`;
218+
219+
Linking.canOpenURL(url).then(async () => {
220+
await Linking.openURL(url);
221+
}).catch((e) => {
222+
Alert.alert("Whatsapp is probably not installed...");
223+
});
224+
}
225+
226+
227+
export async function sharePostToFacebook(id: string) {
228+
const redirectUrl = Linking.createURL(`/comments/${id}`);
229+
const cleanedUrl = redirectUrl.replace("hacknet:///", "https://hacknet-web.vercel.app/")
230+
const url = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(cleanedUrl)}`;
231+
232+
Linking.canOpenURL(url).then(async () => {
233+
await Linking.openURL(url);
234+
}).catch((e) => {
235+
Alert.alert("Facebook is probably not installed...");
236+
});
237+
}
238+
239+
export async function sharePostToReddit(id: string) {
240+
const redirectUrl = Linking.createURL(`/comments/${id}`);
241+
const cleanedUrl = redirectUrl.replace("hacknet:///", "https://hacknet-web.vercel.app/")
242+
const url = `reddit://submit?url=${encodeURIComponent(cleanedUrl)}&title=Check+Out+This+Post:&type=LINK`;
243+
const fallBackurl = `https://www.reddit.com/submit?url=${encodeURIComponent(cleanedUrl)}&title=Check+Out+This+Post:&type=LINK`;
244+
245+
Linking.canOpenURL(url).then(async () => {
246+
await Linking.openURL(url);
247+
}).catch(async (e) => {
248+
await Linking.openURL(fallBackurl);
249+
});
250+
}
251+

src/utils/types.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ export type BottomSheetItem = {
3535
text: string;
3636
func: () => void;
3737
icon: "delete" | "share-variant" | "account-plus" | "heart"
38+
};
39+
40+
export type BottomSheetData = {
41+
postId: string;
3842
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3979,6 +3979,11 @@ expo-blur@~14.1.5:
39793979
resolved "https://registry.yarnpkg.com/expo-blur/-/expo-blur-14.1.5.tgz#910712389e19286ccdc136275bf569f427aa05ef"
39803980
integrity sha512-CCLJHxN4eoAl06ESKT3CbMasJ98WsjF9ZQEJnuxtDb9ffrYbZ+g9ru84fukjNUOTtc8A8yXE5z8NgY1l0OMrmQ==
39813981

3982+
expo-clipboard@~7.1.5:
3983+
version "7.1.5"
3984+
resolved "https://registry.yarnpkg.com/expo-clipboard/-/expo-clipboard-7.1.5.tgz#6980f825d4d7f53aa557d3321dd975bf65541910"
3985+
integrity sha512-TCANUGOxouoJXxKBW5ASJl2WlmQLGpuZGemDCL2fO5ZMl57DGTypUmagb0CVUFxDl0yAtFIcESd78UsF9o64aw==
3986+
39823987
expo-constants@~17.1.7:
39833988
version "17.1.7"
39843989
resolved "https://registry.yarnpkg.com/expo-constants/-/expo-constants-17.1.7.tgz#35194c1cef51f1ea756333418f1e077be79a012b"

0 commit comments

Comments
 (0)