import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import type { ContactData, SocialLinksData } from './types';
import LinkedInIcon from './components/icons/LinkedInIcon';
import XIcon from './components/icons/XIcon';
import GithubIcon from './components/icons/GithubIcon';
import TiktokIcon from './components/icons/TiktokIcon';
import InstagramIcon from './components/icons/InstagramIcon';
import FacebookIcon from './components/icons/FacebookIcon';
import DiscordIcon from './components/icons/DiscordIcon';
import VerifiedIcon from './components/icons/VerifiedIcon';
import VirtualIdCard from './components/VirtualIdCard';
import UploadIcon from './components/icons/UploadIcon';
import AuthenticityScore from './components/AuthenticityScore';
import BrainCircuitIcon from './components/icons/BrainCircuitIcon';
import ArrowRightIcon from './components/icons/ArrowRightIcon';
interface EditPageProps {
initialData: ContactData;
onSave: (data: ContactData) => void;
}
// Reusable Section Component for editable fields
const EditSection: React.FC<{title: string, children: React.ReactNode}> = ({ title, children }) => (
{title}
{children}
);
const socialPlatforms = [
{
key: 'linkedin', name: 'LinkedIn', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-blue-500 to-sky-400',
button: 'bg-blue-600 hover:bg-blue-700',
}
},
{
key: 'x', name: 'X (Twitter)', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-slate-900 to-slate-700',
button: 'bg-slate-800 hover:bg-slate-900',
}
},
{
key: 'github', name: 'GitHub', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-slate-900 to-slate-700',
button: 'bg-slate-800 hover:bg-slate-900',
}
},
{
key: 'tiktok', name: 'TikTok', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-black via-rose-500 to-cyan-400',
button: 'bg-black hover:bg-gray-800',
}
},
{
key: 'instagram', name: 'Instagram', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-purple-500 via-pink-500 to-yellow-500',
button: 'bg-pink-600 hover:bg-pink-700',
}
},
{
key: 'facebook', name: 'Facebook', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-blue-700 to-blue-500',
button: 'bg-blue-700 hover:bg-blue-800',
}
},
{
key: 'discord', name: 'Discord', icon: ,
brandClasses: {
iconBg: 'bg-gradient-to-br from-indigo-600 to-purple-500',
button: 'bg-indigo-600 hover:bg-indigo-700',
}
},
] as const;
const EditPage: React.FC = ({ initialData, onSave }) => {
const navigate = useNavigate();
const [formData, setFormData] = useState(initialData);
useEffect(() => {
const handleOauthMessage = (event: MessageEvent) => {
if (event.origin !== window.location.origin) {
return;
}
const { type, platform, profileUrl, error } = event.data;
if (type === 'oauth-success' && platform && profileUrl) {
setFormData(prev => ({
...prev,
socials: {
...prev.socials,
[platform]: profileUrl
}
}));
} else if (type === 'oauth-error') {
console.error(`OAuth Error for ${platform}:`, error);
alert(`Failed to connect ${platform}. Please try again.`);
}
};
window.addEventListener('message', handleOauthMessage);
return () => {
window.removeEventListener('message', handleOauthMessage);
};
}, []);
useEffect(() => {
// Recalculate authenticity score when dependencies change
const connectedSocials = Object.values(formData.socials).filter(link => !!link).length;
let socialScore = 0;
if (connectedSocials === 1) {
socialScore = 15;
} else if (connectedSocials === 2) {
socialScore = 30;
} else if (connectedSocials > 2) {
socialScore = 30 + (connectedSocials - 2) * 5;
}
const finalSocialScore = Math.min(socialScore, 45);
const videoScore = formData.videoIntroUrl ? 15 : 0;
// Only update if the scores have changed to prevent infinite loops
if (
finalSocialScore !== formData.authenticityScore.socialBinding ||
videoScore !== formData.authenticityScore.videoVerification
) {
setFormData(prev => ({
...prev,
authenticityScore: {
...prev.authenticityScore,
socialBinding: finalSocialScore,
videoVerification: videoScore,
}
}));
}
}, [formData.socials, formData.videoIntroUrl, formData.authenticityScore]);
const handleInputChange = (e: React.ChangeEvent) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value,
}));
};
const handleVideoChange = (e: React.ChangeEvent) => {
const file = e.target.files?.[0];
if (file) {
const videoUrl = URL.createObjectURL(file);
setFormData(prev => ({
...prev,
videoIntroUrl: videoUrl,
videoPosterUrl: '' // Reset poster for new video
}));
}
};
const handleSocialConnect = (platform: keyof SocialLinksData) => {
const isConnected = !!formData.socials[platform];
if (isConnected) {
// Disconnect logic
const newSocials = { ...formData.socials, [platform]: '' };
setFormData(prev => ({ ...prev, socials: newSocials }));
return;
}
// --- Real OAuth URLs ---
const redirectUri = `${window.location.origin}/oauth-callback.html`;
let oauthUrl = '';
switch (platform) {
case 'github':
oauthUrl = `https://github.com/login/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&scope=read:user&state=github`;
break;
case 'linkedin':
oauthUrl = `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&state=linkedin&scope=profile%20email%20openid`;
break;
case 'x':
oauthUrl = `https://twitter.com/i/oauth2/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&scope=users.read%20tweet.read&state=x&code_challenge=challenge&code_challenge_method=plain`;
break;
case 'tiktok':
oauthUrl = `https://www.tiktok.com/v2/auth/authorize?client_key=YOUR_CLIENT_KEY&scope=user.info.basic&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&state=tiktok`;
break;
case 'instagram':
oauthUrl = `https://api.instagram.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&scope=user_profile,user_media&response_type=code&state=instagram`;
break;
case 'facebook':
oauthUrl = `https://www.facebook.com/v19.0/dialog/oauth?client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&state=facebook&scope=public_profile,email`;
break;
case 'discord':
oauthUrl = `https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=identify%20email&state=discord`;
break;
}
// --- Popup Window Logic ---
const width = 600;
const height = 700;
const left = (window.innerWidth - width) / 2;
const top = (window.innerHeight - height) / 2;
window.open(
oauthUrl,
'socialLogin',
`width=${width},height=${height},left=${left},top=${top},toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes`
);
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSave(formData);
navigate('/contact');
};
const totalScore = formData.authenticityScore.videoVerification + formData.authenticityScore.socialBinding + formData.authenticityScore.cloneMaturity;
return (
);
};
export default EditPage;