Files
resolutionflow/frontend/src/components/library/ShareTreeModal.tsx
Michael Chihlas 0e0e3572f4 refactor: replace barrel imports with direct module imports for tree-shaking
Replace all `from '@/api'` barrel imports with direct imports from
specific module files (e.g. `from '@/api/trees'`) across 20 files.
This enables better tree-shaking so each page only bundles the API
modules it actually uses.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 18:52:14 -05:00

280 lines
9.5 KiB
TypeScript

import { useState, useEffect } from 'react'
import { X, Copy, Check, Link2, Users, Lock, Globe } from 'lucide-react'
import type { TreeListItem, TreeShare, TreeVisibility } from '@/types'
import { treesApi } from '@/api/trees'
import { cn } from '@/lib/utils'
import { toast } from '@/lib/toast'
interface ShareTreeModalProps {
tree: TreeListItem
isOpen: boolean
onClose: () => void
}
export function ShareTreeModal({ tree, isOpen, onClose }: ShareTreeModalProps) {
const [isGenerating, setIsGenerating] = useState(false)
const [shares, setShares] = useState<TreeShare[]>([])
const [activeShare, setActiveShare] = useState<TreeShare | null>(null)
const [copied, setCopied] = useState(false)
const [allowForking, setAllowForking] = useState(true)
const [visibility, setVisibility] = useState<TreeVisibility>('private')
useEffect(() => {
if (isOpen) {
loadShares()
// Reset state
setCopied(false)
setAllowForking(true)
}
}, [isOpen, tree.id])
const loadShares = async () => {
try {
const sharesData = await treesApi.listShares(tree.id)
setShares(sharesData)
// Set active share to most recent
if (sharesData.length > 0) {
setActiveShare(sharesData[0])
}
} catch (err) {
console.error('Failed to load shares:', err)
}
}
const handleGenerateLink = async () => {
setIsGenerating(true)
try {
const newShare = await treesApi.createShare(tree.id, {
allow_forking: allowForking,
})
setShares([newShare, ...shares])
setActiveShare(newShare)
toast.success('Share link generated')
} catch (err) {
console.error('Failed to generate share link:', err)
toast.error('Failed to generate share link')
} finally {
setIsGenerating(false)
}
}
const handleCopyLink = async () => {
if (!activeShare) return
try {
await navigator.clipboard.writeText(activeShare.share_url)
setCopied(true)
toast.success('Link copied to clipboard')
setTimeout(() => setCopied(false), 2000)
} catch (err) {
console.error('Failed to copy link:', err)
toast.error('Failed to copy link')
}
}
const handleVisibilityChange = async (newVisibility: TreeVisibility) => {
try {
await treesApi.updateVisibility(tree.id, { visibility: newVisibility })
setVisibility(newVisibility)
toast.success('Visibility updated')
} catch (err) {
console.error('Failed to update visibility:', err)
toast.error('Failed to update visibility')
}
}
const getVisibilityIcon = (level: TreeVisibility) => {
switch (level) {
case 'private':
return <Lock className="h-4 w-4" />
case 'team':
return <Users className="h-4 w-4" />
case 'link':
return <Link2 className="h-4 w-4" />
case 'public':
return <Globe className="h-4 w-4" />
}
}
const getVisibilityDescription = (level: TreeVisibility) => {
switch (level) {
case 'private':
return 'Only you can access'
case 'team':
return 'Team members can access'
case 'link':
return 'Anyone with the link'
case 'public':
return 'Discoverable by everyone'
}
}
if (!isOpen) return null
return (
<div className="fixed inset-0 z-50 flex items-center justify-center">
{/* Backdrop */}
<div
className="absolute inset-0 bg-black/80 backdrop-blur-sm"
onClick={onClose}
/>
{/* Modal */}
<div className="relative w-full max-w-lg glass-card rounded-2xl shadow-lg">
{/* Header */}
<div className="flex items-center justify-between border-b border-white/[0.06] px-6 py-4">
<h2 className="text-lg font-semibold text-white">Share Tree</h2>
<button
onClick={onClose}
className="rounded-md p-1 text-white/40 hover:bg-white/[0.06] hover:text-white"
>
<X className="h-5 w-5" />
</button>
</div>
{/* Body */}
<div className="px-6 py-4 space-y-6">
{/* Tree Info */}
<div>
<h3 className="font-medium text-white">{tree.name}</h3>
{tree.description && (
<p className="mt-1 text-sm text-white/70 line-clamp-2">
{tree.description}
</p>
)}
</div>
{/* Visibility Settings */}
<div>
<label className="mb-2 block text-sm font-medium text-white">
Visibility
</label>
<div className="space-y-2">
{(['private', 'team', 'link', 'public'] as TreeVisibility[]).map((level) => (
<button
key={level}
onClick={() => handleVisibilityChange(level)}
className={cn(
'flex w-full items-center gap-3 rounded-md border px-4 py-3 text-left transition-colors',
visibility === level
? 'border-white/20 bg-white/10 text-white'
: 'border-white/[0.06] bg-transparent text-white/50 hover:border-white/20 hover:bg-white/[0.06]'
)}
>
{getVisibilityIcon(level)}
<div className="flex-1">
<div className="text-sm font-medium capitalize">{level}</div>
<div className="text-xs text-white/40">
{getVisibilityDescription(level)}
</div>
</div>
{visibility === level && (
<div className="h-2 w-2 rounded-full bg-white" />
)}
</button>
))}
</div>
</div>
{/* Share Link Generation */}
{visibility !== 'private' && (
<div>
<label className="mb-2 block text-sm font-medium text-white">
Share Link
</label>
{/* Allow Forking Checkbox */}
<div className="mb-3 flex items-center gap-2">
<input
type="checkbox"
id="allow-forking"
checked={allowForking}
onChange={(e) => setAllowForking(e.target.checked)}
className="h-4 w-4 rounded border-white/10 bg-black/50 text-white focus:ring-2 focus:ring-white/20 focus:ring-offset-2 focus:ring-offset-black"
/>
<label
htmlFor="allow-forking"
className="text-sm text-white/70 cursor-pointer"
>
Allow recipients to fork this tree
</label>
</div>
{/* Generate Button */}
{!activeShare && (
<button
onClick={handleGenerateLink}
disabled={isGenerating}
className={cn(
'w-full rounded-md bg-white px-4 py-2 text-sm font-medium text-black',
'hover:bg-white/90 disabled:opacity-50 disabled:cursor-not-allowed'
)}
>
{isGenerating ? 'Generating...' : 'Generate Share Link'}
</button>
)}
{/* Active Share Link */}
{activeShare && (
<div className="space-y-2">
<div className="flex items-center gap-2 rounded-md border border-white/10 bg-black/50 p-3">
<input
type="text"
value={activeShare.share_url}
readOnly
className="flex-1 bg-transparent text-sm text-white outline-none"
/>
<button
onClick={handleCopyLink}
className={cn(
'flex items-center gap-2 rounded-md border border-white/10 px-3 py-1.5 text-sm font-medium transition-colors',
copied
? 'border-green-500 bg-green-500/10 text-green-400'
: 'text-white/60 hover:bg-white/10 hover:text-white'
)}
>
{copied ? (
<>
<Check className="h-4 w-4" />
Copied
</>
) : (
<>
<Copy className="h-4 w-4" />
Copy
</>
)}
</button>
</div>
<p className="text-xs text-white/40">
{activeShare.allow_forking
? 'Recipients can fork this tree'
: 'Forking disabled for this share'}
</p>
{shares.length > 1 && (
<p className="text-xs text-white/40">
{shares.length} active share links
</p>
)}
</div>
)}
</div>
)}
</div>
{/* Footer */}
<div className="flex justify-end gap-3 border-t border-white/[0.06] px-6 py-4">
<button
onClick={onClose}
className={cn(
'rounded-md border border-white/10 px-4 py-2 text-sm font-medium text-white/60',
'hover:bg-white/10 hover:text-white'
)}
>
Close
</button>
</div>
</div>
</div>
)
}