|
| 1 | +import { useState, useEffect, FormEvent } from 'react' |
| 2 | +import { Spinner } from './Spinner' |
| 3 | +import type { PlatformInfo } from '../types' |
| 4 | +import { api } from '../services/api' |
| 5 | + |
| 6 | +const PLATFORM_ICONS: Record<string, string> = { |
| 7 | + instagram: '📸', |
| 8 | + tiktok: '🎵', |
| 9 | +} |
| 10 | + |
| 11 | +interface Props { |
| 12 | + onSubmit: (url: string) => void |
| 13 | + loading: boolean |
| 14 | + error: string | null |
| 15 | +} |
| 16 | + |
| 17 | +export function UrlInput({ onSubmit, loading, error }: Props) { |
| 18 | + const [url, setUrl] = useState('') |
| 19 | + const [platforms, setPlatforms] = useState<PlatformInfo[]>([]) |
| 20 | + const [detected, setDetected] = useState<PlatformInfo | null>(null) |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + api.platforms().then(setPlatforms).catch(() => {}) |
| 24 | + }, []) |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (!url.trim() || platforms.length === 0) { |
| 28 | + setDetected(null) |
| 29 | + return |
| 30 | + } |
| 31 | + const lower = url.toLowerCase() |
| 32 | + const match = platforms.find((p) => lower.includes(p.platform)) |
| 33 | + setDetected(match ?? null) |
| 34 | + }, [url, platforms]) |
| 35 | + |
| 36 | + function handleSubmit(e: FormEvent) { |
| 37 | + e.preventDefault() |
| 38 | + if (!url.trim() || loading) return |
| 39 | + onSubmit(url.trim()) |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: '0.75rem' }}> |
| 44 | + <div style={{ position: 'relative' }}> |
| 45 | + <input |
| 46 | + type="url" |
| 47 | + placeholder="https://instagram.com/username" |
| 48 | + value={url} |
| 49 | + onChange={(e) => setUrl(e.target.value)} |
| 50 | + disabled={loading} |
| 51 | + style={{ |
| 52 | + width: '100%', |
| 53 | + padding: '0.875rem 1rem', |
| 54 | + paddingRight: detected ? '3.5rem' : '1rem', |
| 55 | + borderRadius: '0.75rem', |
| 56 | + border: `2px solid ${error ? '#fca5a5' : detected ? '#a5b4fc' : '#e5e7eb'}`, |
| 57 | + fontSize: '1rem', |
| 58 | + outline: 'none', |
| 59 | + transition: 'border-color 0.2s', |
| 60 | + boxSizing: 'border-box', |
| 61 | + }} |
| 62 | + /> |
| 63 | + {detected && ( |
| 64 | + <span |
| 65 | + style={{ |
| 66 | + position: 'absolute', |
| 67 | + right: 12, |
| 68 | + top: '50%', |
| 69 | + transform: 'translateY(-50%)', |
| 70 | + fontSize: '1.5rem', |
| 71 | + }} |
| 72 | + title={detected.label} |
| 73 | + > |
| 74 | + {PLATFORM_ICONS[detected.platform] ?? ''} |
| 75 | + </span> |
| 76 | + )} |
| 77 | + </div> |
| 78 | + |
| 79 | + {/* URL hint */} |
| 80 | + <div style={{ fontSize: '0.8rem', color: '#9ca3af', display: 'flex', gap: '1rem', flexWrap: 'wrap' }}> |
| 81 | + {platforms.map((p) => ( |
| 82 | + <span |
| 83 | + key={p.platform} |
| 84 | + style={{ |
| 85 | + padding: '0.2rem 0.5rem', |
| 86 | + borderRadius: '0.4rem', |
| 87 | + background: detected?.platform === p.platform ? '#eef2ff' : '#f9fafb', |
| 88 | + color: detected?.platform === p.platform ? '#6366f1' : '#9ca3af', |
| 89 | + fontWeight: detected?.platform === p.platform ? 600 : 400, |
| 90 | + transition: 'all 0.15s', |
| 91 | + }} |
| 92 | + > |
| 93 | + {PLATFORM_ICONS[p.platform]} {p.urlHint} |
| 94 | + </span> |
| 95 | + ))} |
| 96 | + </div> |
| 97 | + |
| 98 | + {/* Error */} |
| 99 | + {error && ( |
| 100 | + <div |
| 101 | + style={{ |
| 102 | + background: '#fef2f2', |
| 103 | + border: '1px solid #fca5a5', |
| 104 | + borderRadius: '0.6rem', |
| 105 | + padding: '0.6rem 0.75rem', |
| 106 | + color: '#dc2626', |
| 107 | + fontSize: '0.85rem', |
| 108 | + }} |
| 109 | + > |
| 110 | + {error} |
| 111 | + </div> |
| 112 | + )} |
| 113 | + |
| 114 | + <button |
| 115 | + type="submit" |
| 116 | + disabled={loading || !url.trim()} |
| 117 | + style={{ |
| 118 | + display: 'flex', |
| 119 | + alignItems: 'center', |
| 120 | + justifyContent: 'center', |
| 121 | + gap: '0.5rem', |
| 122 | + padding: '0.75rem 2rem', |
| 123 | + borderRadius: '0.75rem', |
| 124 | + border: 'none', |
| 125 | + background: loading || !url.trim() ? '#c7d2fe' : '#6366f1', |
| 126 | + color: '#fff', |
| 127 | + fontWeight: 700, |
| 128 | + fontSize: '1rem', |
| 129 | + cursor: loading || !url.trim() ? 'not-allowed' : 'pointer', |
| 130 | + transition: 'background 0.15s', |
| 131 | + boxShadow: loading || !url.trim() ? 'none' : '0 4px 14px rgba(99,102,241,0.4)', |
| 132 | + }} |
| 133 | + > |
| 134 | + {loading ? ( |
| 135 | + <> |
| 136 | + <Spinner size={18} /> |
| 137 | + Анализируем... |
| 138 | + </> |
| 139 | + ) : ( |
| 140 | + 'Проверить' |
| 141 | + )} |
| 142 | + </button> |
| 143 | + </form> |
| 144 | + ) |
| 145 | +} |
0 commit comments