|
| 1 | +import { Stream } from "@/types/stream"; |
| 2 | + |
| 3 | +// Dados mínimos para compartilhamento |
| 4 | +interface ShareableStream { |
| 5 | + p: string; // platform |
| 6 | + c: string; // channelId |
| 7 | + t: string; // title |
| 8 | + m?: boolean; // isMain |
| 9 | + ch?: boolean; // showChat |
| 10 | + w?: number; // width |
| 11 | + h?: number; // height |
| 12 | + v?: number; // volume |
| 13 | + ic?: boolean; // isChannel |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Codifica as streams em uma string compacta para compartilhamento via URL |
| 18 | + */ |
| 19 | +export function encodeStreamsToUrl(streams: Stream[]): string { |
| 20 | + if (streams.length === 0) return ''; |
| 21 | + |
| 22 | + const shareableStreams: ShareableStream[] = streams.map(stream => { |
| 23 | + const shareable: ShareableStream = { |
| 24 | + p: stream.platform, |
| 25 | + c: stream.channelId, |
| 26 | + t: stream.title, |
| 27 | + }; |
| 28 | + |
| 29 | + // Só adiciona propriedades não-padrão para economizar espaço |
| 30 | + if (stream.isMain) shareable.m = true; |
| 31 | + if (stream.showChat) shareable.ch = true; |
| 32 | + if (stream.width !== 50) shareable.w = Math.round(stream.width * 100) / 100; // 2 decimais |
| 33 | + if (stream.height !== 400) shareable.h = stream.height; |
| 34 | + if (stream.volume !== 100) shareable.v = stream.volume; |
| 35 | + if (stream.isChannel) shareable.ic = true; |
| 36 | + |
| 37 | + return shareable; |
| 38 | + }); |
| 39 | + |
| 40 | + // Converte para JSON e comprime com base64 |
| 41 | + const json = JSON.stringify(shareableStreams); |
| 42 | + const compressed = btoa(encodeURIComponent(json)); |
| 43 | + |
| 44 | + return compressed; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Decodifica streams de uma URL compartilhada |
| 49 | + */ |
| 50 | +export function decodeStreamsFromUrl(encoded: string): Stream[] { |
| 51 | + if (!encoded) return []; |
| 52 | + |
| 53 | + try { |
| 54 | + // Decodifica de base64 |
| 55 | + const json = decodeURIComponent(atob(encoded)); |
| 56 | + const shareableStreams: ShareableStream[] = JSON.parse(json); |
| 57 | + |
| 58 | + // Converte de volta para formato completo |
| 59 | + const streams: Stream[] = shareableStreams.map((shareable, index) => ({ |
| 60 | + id: `stream-${Date.now()}-${index}`, |
| 61 | + platform: shareable.p as any, |
| 62 | + channelId: shareable.c, |
| 63 | + title: shareable.t, |
| 64 | + isMain: shareable.m || false, |
| 65 | + showChat: shareable.ch || false, |
| 66 | + width: shareable.w ?? 50, |
| 67 | + height: shareable.h ?? 400, |
| 68 | + volume: shareable.v ?? 100, |
| 69 | + isChannel: shareable.ic || false, |
| 70 | + })); |
| 71 | + |
| 72 | + return streams; |
| 73 | + } catch (error) { |
| 74 | + console.error('Erro ao decodificar streams da URL:', error); |
| 75 | + return []; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Gera URL completa para compartilhamento |
| 81 | + */ |
| 82 | +export function generateShareUrl(streams: Stream[]): string { |
| 83 | + const encoded = encodeStreamsToUrl(streams); |
| 84 | + if (!encoded) return window.location.origin; |
| 85 | + |
| 86 | + const url = new URL(window.location.origin); |
| 87 | + url.hash = `streams=${encoded}`; |
| 88 | + |
| 89 | + return url.toString(); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Extrai streams da URL atual |
| 94 | + */ |
| 95 | +export function getStreamsFromCurrentUrl(): Stream[] | null { |
| 96 | + const hash = window.location.hash; |
| 97 | + if (!hash) return null; |
| 98 | + |
| 99 | + // Remove o # e extrai o parâmetro streams |
| 100 | + const params = hash.substring(1); |
| 101 | + const match = params.match(/streams=([^&]+)/); |
| 102 | + |
| 103 | + if (!match) return null; |
| 104 | + |
| 105 | + return decodeStreamsFromUrl(match[1]); |
| 106 | +} |
0 commit comments