'use client' import { useState, useRef, useCallback, useEffect, ChangeEvent, } from 'react' import { useMutation } from '@tanstack/react-query' import api from '@/services/api' import { snippetService } from '@/services/snippetService' import { useAuthStore } from '@/store/authStore' // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- type PostType = 'post' | 'snippet' | 'repo' type Privacy = 'public' | 'followers' | 'private' interface RepoPreview { fullName: string description: string stars: number language: string htmlUrl: string } interface CreatePostModalProps { isOpen: boolean onClose: () => void onSuccess?: () => void } interface PostPayload { content: string mediaUrls?: string[] repoFullName?: string audience: Privacy } interface PostResponse { id: string content: string createdAt: string } // --------------------------------------------------------------------------- // Constants // --------------------------------------------------------------------------- const MAX_CHARS = 3000 const SUPPORTED_LANGUAGES = [ 'typescript', 'javascript', 'python', 'rust', 'go', 'java', 'c', 'cpp', 'csharp', 'php', 'ruby', 'swift', 'kotlin', 'bash', 'sql', 'html', 'css', 'json', 'yaml', 'markdown', 'other', ] as const type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number] // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function highlightHashtags(text: string): JSX.Element[] { const parts = text.split(/(\s)/) return parts.map((part, i) => { if (/^#\w+$/.test(part)) { return ( {part} ) } return {part} }) } function isValidGitHubUrl(url: string): boolean { return /^https?:\/\/github\.com\/[\w.-]+\/[\w.-]+\/?$/.test(url.trim()) } function extractRepoPath(url: string): string { const match = url.trim().match(/github\.com\/([\w.-]+\/[\w.-]+)/) return match ? match[1] : '' } // --------------------------------------------------------------------------- // Tab bar // --------------------------------------------------------------------------- interface TabBarProps { active: PostType onChange: (t: PostType) => void } function TabBar({ active, onChange }: TabBarProps) { const tabs: { id: PostType; label: string }[] = [ { id: 'post', label: 'Post' }, { id: 'snippet', label: 'Code Snippet' }, { id: 'repo', label: 'Share Repo' }, ] return (
{tabs.map(({ id, label }) => ( ))}
) } // --------------------------------------------------------------------------- // Post composer tab // --------------------------------------------------------------------------- interface PostTabProps { content: string onContentChange: (val: string) => void repoTag: string onRepoTagChange: (val: string) => void privacy: Privacy onPrivacyChange: (p: Privacy) => void imageFiles: File[] imagePreviewUrls: string[] onImagesAdd: (files: File[]) => void onImageRemove: (index: number) => void } function PostTab({ content, onContentChange, repoTag, onRepoTagChange, privacy, onPrivacyChange, imageFiles, imagePreviewUrls, onImagesAdd, onImageRemove, }: PostTabProps) { const textareaRef = useRef(null) const fileInputRef = useRef(null) const user = useAuthStore((s) => s.user) const remaining = MAX_CHARS - content.length const isOverLimit = remaining < 0 // Auto-resize textarea useEffect(() => { const el = textareaRef.current if (!el) return el.style.height = 'auto' el.style.height = `${el.scrollHeight}px` }, [content]) const handleFileInput = useCallback( (e: ChangeEvent) => { const files = Array.from(e.target.files ?? []).filter((f) => f.type.startsWith('image/'), ) onImagesAdd(files) e.target.value = '' }, [onImagesAdd], ) const privacyOptions: { value: Privacy; label: string }[] = [ { value: 'public', label: 'Public' }, { value: 'followers', label: 'Followers' }, { value: 'private', label: 'Private' }, ] return (
{/* Author row */}
{user?.avatar ? ( {user.username} ) : null}

{user?.fullName ?? user?.username ?? 'You'}

@{user?.username ?? 'you'}

{/* Textarea with overlay highlights */}
{/* Highlight layer — rendered behind the textarea */}