11import { auth , db } from "@auth/firebase" ;
2+ import * as Clipboard from 'expo-clipboard' ;
3+ import * as Linking from 'expo-linking' ;
24import { addDoc , collection , deleteDoc , doc , getCountFromServer , getDoc , increment , serverTimestamp , setDoc , updateDoc } from "firebase/firestore" ;
5+ import { Alert , Share } from "react-native" ;
36import { extractUrl } from "./stringTimeUtils" ;
47
58export 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+
0 commit comments