![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.ca/public_html/src/hooks/ |
import { useCallback, useEffect, useState } from 'react';
interface ShareData {
url: string;
title: string;
description: string;
image?: string;
hashtags?: string[];
}
interface UseSocialShareReturn {
shareToFacebook: (data: ShareData) => Promise<void>;
shareToTwitter: (data: ShareData) => Promise<void>;
shareToLinkedIn: (data: ShareData) => Promise<void>;
shareToWhatsApp: (data: ShareData) => Promise<void>;
copyToClipboard: (data: ShareData) => Promise<void>;
isSharing: boolean;
shareError: string | null;
}
declare global {
interface Window {
FB?: any;
fbAsyncInit?: () => void;
}
}
export const useSocialShare = (): UseSocialShareReturn => {
const [isSharing, setIsSharing] = useState(false);
const [shareError, setShareError] = useState<string | null>(null);
// Initialize Facebook SDK
useEffect(() => {
if (typeof window !== 'undefined' && !window.FB) {
// Load Facebook SDK
const script = document.createElement('script');
script.src = 'https://connect.facebook.net/en_US/sdk.js';
script.async = true;
script.defer = true;
script.crossOrigin = 'anonymous';
window.fbAsyncInit = () => {
window.FB.init({
appId: process.env.NEXT_PUBLIC_FACEBOOK_APP_ID || 'your-facebook-app-id',
cookie: true,
xfbml: true,
version: 'v18.0'
});
};
document.head.appendChild(script);
return () => {
document.head.removeChild(script);
};
}
}, []);
const shareToFacebook = useCallback(async (data: ShareData) => {
setIsSharing(true);
setShareError(null);
try {
// Use direct URL redirection for Facebook sharing
const url = `https://www.facebook.com/dialog/share?` +
`app_id=${process.env.NEXT_PUBLIC_FACEBOOK_APP_ID}` +
`&display=popup` +
`&href=${encodeURIComponent(data.url)}` +
(Array.isArray(data.hashtags) && data.hashtags[0] ? `&hashtag=%23${data.hashtags[0]}` : '') +
`&redirect_uri=${encodeURIComponent(window.location.href)}`;
window.open(url, '_blank', 'width=600,height=400');
} catch (error) {
setShareError('Failed to share to Facebook');
console.error('Facebook share error:', error);
} finally {
setIsSharing(false);
}
}, []);
const shareToTwitter = useCallback(async (data: ShareData) => {
setIsSharing(true);
setShareError(null);
try {
const text = `${data.title}\n\n${data.description}`;
const hashtags = Array.isArray(data.hashtags) ? data.hashtags.join(',') : '';
window.open(
`https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}&url=${encodeURIComponent(data.url)}&hashtags=${encodeURIComponent(hashtags)}`,
'_blank',
'width=600,height=400'
);
} catch (error) {
setShareError('Failed to share to Twitter');
console.error('Twitter share error:', error);
} finally {
setIsSharing(false);
}
}, []);
const shareToLinkedIn = useCallback(async (data: ShareData) => {
setIsSharing(true);
setShareError(null);
try {
window.open(
`https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(data.url)}&title=${encodeURIComponent(data.title)}&summary=${encodeURIComponent(data.description)}`,
'_blank',
'width=600,height=400'
);
} catch (error) {
setShareError('Failed to share to LinkedIn');
console.error('LinkedIn share error:', error);
} finally {
setIsSharing(false);
}
}, []);
const shareToWhatsApp = useCallback(async (data: ShareData) => {
setIsSharing(true);
setShareError(null);
try {
const text = `${data.title}\n\n${data.description}\n\n${data.url}`;
// Check if mobile
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (isMobile) {
window.open(`whatsapp://send?text=${encodeURIComponent(text)}`);
} else {
window.open(
`https://wa.me/?text=${encodeURIComponent(text)}`,
'_blank',
'width=600,height=400'
);
}
} catch (error) {
setShareError('Failed to share to WhatsApp');
console.error('WhatsApp share error:', error);
} finally {
setIsSharing(false);
}
}, []);
const copyToClipboard = useCallback(async (data: ShareData) => {
setIsSharing(true);
setShareError(null);
try {
const shareText = `${data.title}\n\n${data.description}\n\n${data.url}`;
if (navigator.clipboard) {
await navigator.clipboard.writeText(shareText);
} else {
// Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = shareText;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
// Show success feedback
setShareError(null);
} catch (error) {
setShareError('Failed to copy to clipboard');
console.error('Clipboard error:', error);
} finally {
setIsSharing(false);
}
}, []);
return {
shareToFacebook,
shareToTwitter,
shareToLinkedIn,
shareToWhatsApp,
copyToClipboard,
isSharing,
shareError
};
};