'use client' import { useState, useEffect } from 'react' import { Calendar, Copy, Check, RefreshCw, ExternalLink, HelpCircle, Smartphone } from 'lucide-react' import { Card, Button, showToast } from '@/components/ui' interface CalendarIntegrationProps { workspaceId: string } export function CalendarIntegration({ workspaceId }: CalendarIntegrationProps) { const [feed, setFeed] = useState(null) const [loading, setLoading] = useState(true) const [copied, setCopied] = useState(false) const [showInstructions, setShowInstructions] = useState(false) useEffect(() => { fetchFeed() }, [workspaceId]) const fetchFeed = async () => { try { const response = await fetch(`/api/workspaces/${workspaceId}/calendar-feed/settings`) if (response.ok) { const data = await response.json() setFeed(data.feed) } } catch (err) { console.error('Failed to fetch calendar feed:', err) } finally { setLoading(false) } } const createFeed = async () => { try { const response = await fetch(`/api/workspaces/${workspaceId}/calendar-feed`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ includeAppointments: true, includeMeds: false, daysAhead: 90 }) }) if (response.ok) { const data = await response.json() setFeed(data.feed) showToast('Calendar feed created!', 'success') } } catch { showToast('Failed to create feed', 'error') } } const regenerateToken = async () => { try { const response = await fetch(`/api/workspaces/${workspaceId}/calendar-feed/regenerate`, { method: 'POST' }) if (response.ok) { const data = await response.json() setFeed(data.feed) showToast('Feed URL regenerated', 'success') } } catch { showToast('Failed to regenerate', 'error') } } const copyFeedUrl = () => { if (!feed) return const url = `${window.location.origin}/api/workspaces/${workspaceId}/calendar-feed?token=${feed.token}` navigator.clipboard.writeText(url) setCopied(true) setTimeout(() => setCopied(false), 2000) showToast('Feed URL copied!', 'success') } if (loading) { return (
) } return (

Calendar Integration

Sync appointments to your calendar

{!feed ? (

Create a calendar feed to see your appointments in Apple Calendar, Google Calendar, or Outlook.

) : (
{/* Feed URL */}

Copy this URL and add it to your calendar app

{/* Settings */}

Appointments

{feed.includeAppointments ? 'Included' : 'Not included'}

Look Ahead

{feed.daysAhead} days

{/* Last Accessed */} {feed.lastAccessedAt && (

Last accessed: {new Date(feed.lastAccessedAt).toLocaleDateString()}

)} {/* Actions */}
{/* Instructions */} {showInstructions && (

How to Add to Your Calendar

Apple Calendar (iPhone/Mac):

  1. Copy the feed URL above
  2. Open Calendar app → File → New Calendar Subscription
  3. Paste the URL and click Subscribe

Google Calendar:

  1. Copy the feed URL above
  2. Open Google Calendar → Settings → Add Calendar → From URL
  3. Paste the URL and click Add Calendar

Outlook:

  1. Copy the feed URL above
  2. Open Outlook → Add Calendar → Subscribe from web
  3. Paste the URL and click Import
)}
)}
) }