Velocity UI
Loading…
Menu

Smart Code Diff Viewer

A fully interactive, responsive VS Code-style diff viewer with working Run/Copy buttons and terminal simulation.

Installation

Add this component to your project using the CLI:

terminal
npx vui-registry-cli-v1 add smart-code-diff

Source Code

smart-code-diff.tsx

'use client'

import React, { useState, useRef, useEffect } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { Terminal, Play, Copy, Check, ChevronRight, FileCode, Search, GitBranch, Settings, X, Minus, Square } from 'lucide-react'
import { cn } from '@/lib/utils'
import Prism from 'prismjs'
import 'prismjs/components/prism-javascript'
import 'prismjs/themes/prism-tomorrow.css'

// @ts-ignore
const CodeLine = ({ code, language = 'javascript' }) => {
  const highlighted = Prism.highlight(code, Prism.languages[language], language)
  return <div dangerouslySetInnerHTML={{ __html: highlighted }} />
}

const ORIGINAL_CODE = `/**
 * @author Vikas Yadav
 * @project Velocity UI
 */
function calculateTotal(items) {
  let total = 0;
  for (let i = 0; i < items.length; i++) {
    total = total + items[i].price;
  }
  return total;
}`

const OPTIMIZED_CODE = `/**
 * @author Vikas Yadav
 * @project Velocity UI
 */
const calculateTotal = (items) => {
  // Refactored by Velocity AI
  return items.reduce((acc, item) => 
    acc + item.price, 0
  );
}`

export default function SmartCodeDiff() {
  const [isRunning, setIsRunning] = useState(false)
  const [output, setOutput] = useState<string[]>([])
  const [isCopied, setIsCopied] = useState(false)
  const [activeTab, setActiveTab] = useState('diff')

  const handleRun = () => {
    setIsRunning(true)
    setOutput([])
    
    // Simulate execution
    setTimeout(() => {
        setOutput(prev => [...prev, "> Compiling..."])
    }, 500)
    
    setTimeout(() => {
        setOutput(prev => [...prev, "> Running tests..."])
    }, 1500)
    
    setTimeout(() => {
        setOutput(prev => [...prev, "✓ All tests passed (342ms)", "✓ Optimization: 40% faster"])
        setIsRunning(false)
    }, 2500)
  }

  const handleCopy = () => {
    navigator.clipboard.writeText(OPTIMIZED_CODE)
    setIsCopied(true)
    setTimeout(() => setIsCopied(false), 2000)
  }

  return (
    <div className="flex h-[700px] w-full flex-col overflow-hidden rounded-xl bg-[#1e1e1e] text-[#d4d4d4] shadow-2xl font-mono text-sm border border-[#333] ring-1 ring-white/10">
      
      {/* Title Bar */}
      <div className="flex h-10 items-center justify-between bg-[#181818] px-4 border-b border-[#1f1f1f] select-none">
        <div className="flex items-center gap-2">
           <div className="flex gap-2 group">
             <div className="h-3 w-3 rounded-full bg-[#ff5f56] group-hover:bg-[#ff5f56]/80" />
             <div className="h-3 w-3 rounded-full bg-[#ffbd2e] group-hover:bg-[#ffbd2e]/80" />
             <div className="h-3 w-3 rounded-full bg-[#27c93f] group-hover:bg-[#27c93f]/80" />
           </div>
        </div>
        <div className="flex items-center gap-2 text-xs text-[#909090]">
           <FileCode className="h-3 w-3" />
           <span>Velocity UI - Smart Diff</span>
        </div>
        <div className="flex items-center gap-2 opacity-0">
           {/* Placeholder for symmetry */}
           <div className="h-3 w-3" />
        </div>
      </div>

      <div className="flex flex-1 overflow-hidden">
        {/* Activity Bar */}
        <div className="w-12 flex flex-col items-center py-4 bg-[#181818] border-r border-[#1f1f1f] gap-6 text-[#858585]">
           <FileCode className="h-6 w-6 text-white cursor-pointer hover:text-white transition-colors" />
           <Search className="h-6 w-6 cursor-pointer hover:text-white transition-colors" />
           <GitBranch className="h-6 w-6 cursor-pointer hover:text-white transition-colors" />
           <div className="flex-1" />
           <Settings className="h-6 w-6 cursor-pointer hover:text-white transition-colors" />
        </div>

        {/* Sidebar */}
        <div className="hidden md:flex w-48 flex-col bg-[#181818] border-r border-[#1f1f1f]">
           <div className="px-4 py-2 text-xs font-bold uppercase tracking-wider text-[#6f6f6f]">Explorer</div>
           <div className="flex flex-col gap-1 px-2">
              <div className="flex items-center gap-2 px-2 py-1 bg-[#37373d] text-white rounded-sm cursor-pointer">
                 <span className="text-yellow-400">JS</span>
                 <span>utils.js</span>
              </div>
              <div className="flex items-center gap-2 px-2 py-1 text-[#909090] hover:bg-[#2a2d2e] rounded-sm cursor-pointer transition-colors">
                 <span className="text-blue-400">TS</span>
                 <span>types.ts</span>
              </div>
              <div className="flex items-center gap-2 px-2 py-1 text-[#909090] hover:bg-[#2a2d2e] rounded-sm cursor-pointer transition-colors">
                 <span className="text-orange-400">JSON</span>
                 <span>package.json</span>
              </div>
           </div>
        </div>

        {/* Main Editor Area */}
        <div className="flex flex-1 flex-col bg-[#1e1e1e]">
           
           {/* Tabs */}
           <div className="flex h-9 bg-[#181818] border-b border-[#1f1f1f]">
              <div 
                className={cn(
                    "flex items-center gap-2 px-4 border-r border-[#1f1f1f] cursor-pointer min-w-[120px]",
                    activeTab === 'diff' ? "bg-[#1e1e1e] text-white border-t-2 border-t-blue-500" : "bg-[#181818] text-[#909090] hover:bg-[#1f1f1f]"
                )}
                onClick={() => setActiveTab('diff')}
              >
                 <span className="text-yellow-400 text-xs">JS</span>
                 <span className="text-xs">utils.js (Diff)</span>
                 <X className="h-3 w-3 ml-auto hover:bg-[#333] rounded p-0.5" />
              </div>
           </div>

           {/* Toolbar */}
           <div className="flex h-10 items-center justify-between px-4 border-b border-[#1f1f1f] bg-[#1e1e1e]">
              <div className="flex items-center gap-4 text-xs text-[#909090]">
                 <span>src > utils.js</span>
                 <span className="text-[#6f6f6f]">Verified by Velocity AI</span>
              </div>
              <div className="flex items-center gap-2">
                 <button 
                    onClick={handleRun}
                    disabled={isRunning}
                    className="flex items-center gap-2 px-3 py-1 bg-green-700 hover:bg-green-600 text-white rounded text-xs transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
                 >
                    {isRunning ? <div className="h-3 w-3 animate-spin rounded-full border-2 border-white/20 border-t-white" /> : <Play className="h-3 w-3 fill-current" />}
                    Run
                 </button>
                 <button 
                    onClick={handleCopy}
                    className="p-1.5 hover:bg-[#333] rounded text-[#909090] hover:text-white transition-colors"
                 >
                    {isCopied ? <Check className="h-4 w-4 text-green-500" /> : <Copy className="h-4 w-4" />}
                 </button>
              </div>
           </div>

           {/* Editor Content */}
           <div className="flex flex-1 overflow-hidden relative">
              
              {/* Diff View */}
              <div className="grid grid-cols-1 md:grid-cols-2 h-full w-full divide-y md:divide-y-0 md:divide-x divide-[#333]">
                 {/* Original */}
                 <div className="relative p-4 overflow-auto bg-[#1e1e1e]">
                    <div className="absolute top-0 right-0 px-2 py-1 bg-red-500/10 text-red-400 text-xs rounded-bl">Original</div>
                    <pre className="text-xs leading-relaxed font-mono">
                       <code className="language-javascript">
                          {ORIGINAL_CODE}
                       </code>
                    </pre>
                 </div>
                 
                 {/* Optimized */}
                 <div className="relative p-4 overflow-auto bg-[#1e1e1e]">
                    <div className="absolute top-0 right-0 px-2 py-1 bg-green-500/10 text-green-400 text-xs rounded-bl">Optimized</div>
                    <pre className="text-xs leading-relaxed font-mono">
                       <code className="language-javascript">
                          {OPTIMIZED_CODE}
                       </code>
                    </pre>
                    {/* Connection Lines Simulation (CSS Only) */}
                    <div className="absolute top-20 left-0 w-full h-[1px] bg-gradient-to-r from-transparent via-green-500/20 to-transparent" />
                 </div>
              </div>

              {/* Terminal Overlay */}
              <AnimatePresence>
                {(isRunning || output.length > 0) && (
                    <motion.div 
                        initial={{ y: "100%" }}
                        animate={{ y: 0 }}
                        exit={{ y: "100%" }}
                        transition={{ type: "spring", stiffness: 300, damping: 30 }}
                        className="absolute bottom-0 left-0 right-0 h-48 bg-[#181818] border-t border-[#333] shadow-2xl z-10 flex flex-col"
                    >
                        <div className="flex items-center justify-between px-4 py-2 bg-[#1f1f1f] border-b border-[#333]">
                            <div className="flex items-center gap-2 text-xs text-[#909090]">
                                <Terminal className="h-3 w-3" />
                                <span>TERMINAL</span>
                            </div>
                            <button onClick={() => setOutput([])} className="text-[#909090] hover:text-white"><X className="h-3 w-3" /></button>
                        </div>
                        <div className="p-4 font-mono text-xs overflow-y-auto flex-1">
                            {output.map((line, i) => (
                                <div key={i} className="mb-1 text-[#d4d4d4]">
                                    {line}
                                </div>
                            ))}
                            {isRunning && (
                                <div className="animate-pulse">_</div>
                            )}
                        </div>
                    </motion.div>
                )}
              </AnimatePresence>

           </div>

           {/* Status Bar */}
           <div className="h-6 bg-[#007acc] text-white flex items-center justify-between px-3 text-[10px] select-none">
              <div className="flex items-center gap-4">
                 <div className="flex items-center gap-1"><GitBranch className="h-3 w-3" /> main</div>
                 <div className="flex items-center gap-1"><X className="h-3 w-3" /> 0 <div className="h-3 w-3 ml-1 text-yellow-200"></div> 0</div>
              </div>
              <div className="flex items-center gap-4">
                 <span>Ln 12, Col 34</span>
                 <span>UTF-8</span>
                 <span>JavaScript</span>
                 <div className="flex items-center gap-1 ml-2">
                    <div className="h-2 w-2 rounded-full bg-white animate-pulse" />
                    <span>Velocity AI Active</span>
                 </div>
              </div>
           </div>

        </div>
      </div>
    </div>
  )
}

Dependencies

  • framer-motion: latest
  • lucide-react: latest
  • prismjs: latest
  • clsx: latest
  • tailwind-merge: latest

Props

Component property reference.

NameTypeDefaultDescription
originalCodestringBuilt-in sampleOriginal code to diff against.
optimizedCodestringBuilt-in sampleOptimized code to display in diff.
languagestring'javascript'Language used for syntax highlighting.
onRun() => voidundefinedCallback for run action.
onCopy() => voidundefinedCallback when optimized code is copied.
classNamestringundefinedAdditional CSS classes.
Context Worth Keeping In Orbit

Most components here are inspired by outstanding libraries and creators in the ecosystem. I don’t claim to be the original author — this is my space for learning, rebuilding, and understanding great work at a deeper level.

I’m still a student of the craft, constantly studying the best and translating what I learn through my own perspective. Every piece reflects curiosity, respect for the community, and small creative touches that feel true to me.

I’ve done my best to credit inspirations properly. If anything is missing or inaccurate, I truly appreciate a message so it can be corrected with care.