link share look

This commit is contained in:
2026-05-04 14:24:44 +01:00
parent bccc0adea6
commit 5bc1f6bb30

View File

@@ -529,7 +529,48 @@ export default function App() {
}).filter(Boolean); }).filter(Boolean);
const sharedCol = collection(db, 'artifacts', appId, 'sharedLooks'); const sharedCol = collection(db, 'artifacts', appId, 'sharedLooks');
const docRef = await addDoc(sharedCol, { const docRef = doc(sharedCol); // Gerar ID de forma síncrona para contornar restrições do Safari
// Força o uso do domínio oficial se estiver em localhost (para os links de partilha funcionarem)
const baseUrl = (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')
? 'https://mycloset.epvc.pt'
: window.location.origin;
const shareUrl = `${baseUrl}${window.location.pathname}?shared=${docRef.id}`;
let copySuccess = false;
// Tentar copiar para o clipboard imediatamente no fluxo do clique
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(shareUrl);
copySuccess = true;
} else {
throw new Error("Clipboard API indisponível");
}
} catch (err) {
// Fallback para Safari antigo ou ambientes sem HTTPS
try {
const textArea = document.createElement("textarea");
textArea.value = shareUrl;
textArea.style.position = "fixed";
textArea.style.left = "-9999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
copySuccess = document.execCommand('copy');
document.body.removeChild(textArea);
} catch (fallbackErr) {
console.error('Erro no fallback de clipboard:', fallbackErr);
}
}
// Opcional: usar Web Share API se estiver no telemóvel/Mac (comentado caso prefiram só copiar)
// if (navigator.share) {
// await navigator.share({ title: 'MyCloset Look', url: shareUrl });
// }
// Agora podemos guardar no Firebase de forma assíncrona
await setDoc(docRef, {
lookName: look.name, lookName: look.name,
ownerUid: user.uid, ownerUid: user.uid,
ownerEmail: user.email || '', ownerEmail: user.email || '',
@@ -537,14 +578,12 @@ export default function App() {
createdAt: new Date().getTime(), createdAt: new Date().getTime(),
}); });
// Força o uso do domínio oficial se estiver em localhost (para os links de partilha funcionarem) if (copySuccess) {
const baseUrl = (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')
? 'https://mycloset.epvc.pt'
: window.location.origin;
const shareUrl = `${baseUrl}${window.location.pathname}?shared=${docRef.id}`;
await navigator.clipboard.writeText(shareUrl);
setCopiedLookId(look.id); setCopiedLookId(look.id);
setTimeout(() => setCopiedLookId(null), 3000); setTimeout(() => setCopiedLookId(null), 3000);
} else {
alert('Link de partilha: ' + shareUrl);
}
} catch (err) { } catch (err) {
console.error('Erro ao partilhar look:', err); console.error('Erro ao partilhar look:', err);
alert('Erro ao gerar link de partilha.'); alert('Erro ao gerar link de partilha.');