53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
// Gestionnaire d'onglets pour les cartes NFC
|
|
export function openOrFocusCard(slug: string) {
|
|
const url = `${window.location.origin}/card/${slug}`
|
|
|
|
// Vérifier si un onglet est déjà ouvert
|
|
const isTabOpen = localStorage.getItem('nfc-active-tab') === 'true'
|
|
|
|
if (isTabOpen) {
|
|
// Si un onglet est déjà ouvert, on envoie un message pour le rafraîchir
|
|
const channel = new BroadcastChannel('nfc-channel')
|
|
channel.postMessage({
|
|
type: 'nfc-focus',
|
|
slug: slug,
|
|
timestamp: Date.now()
|
|
})
|
|
|
|
// On se concentre sur l'onglet existant
|
|
window.focus()
|
|
|
|
// On ferme le canal après un court délai
|
|
setTimeout(() => channel.close(), 1000)
|
|
|
|
// On empêche l'ouverture d'un nouvel onglet
|
|
return false
|
|
}
|
|
|
|
// Aucun onglet ouvert, on en ouvre un nouveau
|
|
window.open(url, '_blank')
|
|
return true
|
|
}
|
|
|
|
// Fonction pour formater l'URL à utiliser dans la carte NFC
|
|
export function getNfcCardUrl(slug: string): string {
|
|
return `javascript:(function(){
|
|
const url = '${window.location.origin}/card/${slug}';
|
|
const isTabOpen = localStorage.getItem('nfc-active-tab') === 'true';
|
|
|
|
if (isTabOpen) {
|
|
const channel = new BroadcastChannel('nfc-channel');
|
|
channel.postMessage({
|
|
type: 'nfc-focus',
|
|
slug: '${slug}',
|
|
timestamp: Date.now()
|
|
});
|
|
setTimeout(() => channel.close(), 1000);
|
|
window.focus();
|
|
window.location.href = url;
|
|
} else {
|
|
window.open(url, '_blank');
|
|
}
|
|
})();`
|
|
}
|