diff --git a/src/App.jsx b/src/App.jsx index 439fd48..236924a 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -529,7 +529,48 @@ export default function App() { }).filter(Boolean); 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, ownerUid: user.uid, ownerEmail: user.email || '', @@ -537,14 +578,12 @@ export default function App() { createdAt: new Date().getTime(), }); - // 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}`; - await navigator.clipboard.writeText(shareUrl); - setCopiedLookId(look.id); - setTimeout(() => setCopiedLookId(null), 3000); + if (copySuccess) { + setCopiedLookId(look.id); + setTimeout(() => setCopiedLookId(null), 3000); + } else { + alert('Link de partilha: ' + shareUrl); + } } catch (err) { console.error('Erro ao partilhar look:', err); alert('Erro ao gerar link de partilha.');