Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

5.1 KB
Raw Blame
import { useState } from 'react'
import { Copy, Check, CalendarPlus, Calendar } from 'lucide-react'
import { useTranslation } from '../../i18n'

interface SubscribeLinksProps {
  httpsUrl: string
  webcalUrl: string
}

/**
 * Shared presentation for calendar subscription URLs. Renders one-click
 * subscribe actions (Google deep link + webcal handoff) plus a copy fallback.
 * Used by both the per-trip and all-trips subscribe modals.
 */
export function SubscribeLinks({ httpsUrl, webcalUrl }: SubscribeLinksProps) {
  const { t } = useTranslation()
  const [copied, setCopied] = useState<'https' | 'webcal' | null>(null)

  // Google Calendar's add-by-URL deep link. The cid must carry the webcal://
  // scheme (not https), URL-encoded, and the feed must be served over HTTPS.
  const googleUrl = `https://www.google.com/calendar/render?cid=${encodeURIComponent(webcalUrl)}`

  const copy = async (url: string, which: 'https' | 'webcal') => {
    try {
      if (navigator.clipboard && window.isSecureContext) {
        await navigator.clipboard.writeText(url)
      } else {
        // Fallback for non-secure contexts (plain HTTP) where navigator.clipboard is unavailable
        const ta = document.createElement('textarea')
        ta.value = url
        ta.style.position = 'fixed'
        ta.style.left = '-9999px'
        document.body.appendChild(ta)
        ta.focus()
        ta.select()
        document.execCommand('copy')
        document.body.removeChild(ta)
      }
      setCopied(which)
      setTimeout(() => setCopied(null), 2000)
    } catch { /* ignore */ }
  }

  return (
    <div>
      {/* One-click subscribe actions */}
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8, marginBottom: 16 }}>
        <a
          href={googleUrl}
          target="_blank"
          rel="noopener noreferrer"
          style={{
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
            padding: '9px 14px', borderRadius: 9, textDecoration: 'none',
            background: 'var(--accent, #6366f1)', color: 'var(--accent-text, #fff)',
            fontSize: 12, fontWeight: 600, fontFamily: 'inherit',
          }}
        >
          <CalendarPlus size={14} strokeWidth={2} />
          {t('dashboard.ics.addGoogle')}
        </a>
        <a
          href={webcalUrl}
          style={{
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
            padding: '9px 14px', borderRadius: 9, textDecoration: 'none',
            background: 'none', border: '1px solid var(--border-primary)',
            color: 'var(--text-primary)', fontSize: 12, fontWeight: 600, fontFamily: 'inherit',
          }}
        >
          <Calendar size={14} strokeWidth={2} />
          {t('dashboard.ics.addApple')}
        </a>
      </div>

      {/* Manual fallback — raw URLs for any other client / "From URL" boxes */}
      <details style={{ fontSize: 11, color: 'var(--text-muted)' }}>
        <summary style={{ cursor: 'pointer', userSelect: 'none', marginBottom: 8 }}>
          {t('dashboard.ics.copyManually')}
        </summary>
        <UrlRow
          label="Google Calendar"
          copyLabel={t('common.copy')}
          hint={t('dashboard.ics.pasteFromUrl')}
          url={httpsUrl}
          copied={copied === 'https'}
          onCopy={() => copy(httpsUrl, 'https')}
        />
        <UrlRow
          label="Apple Calendar / Outlook"
          copyLabel={t('common.copy')}
          hint="webcal://"
          url={webcalUrl}
          copied={copied === 'webcal'}
          onCopy={() => copy(webcalUrl, 'webcal')}
        />
      </details>
    </div>
  )
}

function UrlRow({ label, hint, url, copied, onCopy, copyLabel }: {
  label: string; hint: string; copyLabel: string; url: string; copied: boolean; onCopy: () => void
}) {
  return (
    <div style={{ marginBottom: 12 }}>
      <div style={{ fontSize: 11, fontWeight: 500, marginBottom: 5, color: 'var(--text-primary)' }}>
        {label} <span style={{ color: 'var(--text-muted)', fontWeight: 400 }}>— {hint}</span>
      </div>
      <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
        <div style={{
          flex: 1, fontSize: 10, fontFamily: 'monospace',
          padding: '5px 8px', borderRadius: 6,
          border: '1px solid var(--border-faint)',
          background: 'var(--bg-subtle, #f9fafb)',
          color: 'var(--text-muted)',
          overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
        }}>
          {url}
        </div>
        <button type="button"
          onClick={onCopy}
          title={copyLabel}
          aria-label={copyLabel}
          style={{
            flexShrink: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
            width: 28, height: 28, borderRadius: 6,
            border: '1px solid var(--border-primary)', background: 'none',
            cursor: 'pointer', color: copied ? 'var(--accent, #6366f1)' : 'var(--text-muted)',
            transition: 'color 0.15s',
          }}
        >
          {copied ? <Check size={12} strokeWidth={2.5} /> : <Copy size={12} strokeWidth={2} />}
        </button>
      </div>
    </div>
  )
}