|
| 1 | +import { useState, useEffect, useMemo } from 'react' |
| 2 | +import * as monaco from 'monaco-editor' |
| 3 | +import hljs from 'highlight.js/lib/core' |
| 4 | +import diff from 'highlight.js/lib/languages/diff' |
| 5 | +import 'highlight.js/styles/atom-one-dark.css' |
| 6 | +import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog' |
| 7 | +import { Button } from './ui/button' |
| 8 | +import { generateUnifiedDiff } from '@renderer/lib/unifiedDiffGenerator' |
| 9 | +import { cn } from '../lib/utils' |
| 10 | + |
| 11 | +hljs.registerLanguage('diff', diff) |
| 12 | + |
| 13 | +interface UnifiedDiffDialogProps { |
| 14 | + open: boolean |
| 15 | + onOpenChange: (open: boolean) => void |
| 16 | + diffEditor: monaco.editor.IStandaloneDiffEditor | null |
| 17 | + themeName: 'light' | 'dark' |
| 18 | +} |
| 19 | + |
| 20 | +export function UnifiedDiffDialog({ |
| 21 | + open, |
| 22 | + onOpenChange, |
| 23 | + diffEditor, |
| 24 | + themeName |
| 25 | +}: UnifiedDiffDialogProps): JSX.Element { |
| 26 | + const [copied, setCopied] = useState(false) |
| 27 | + const [unifiedDiffContent, setUnifiedDiffContent] = useState('') |
| 28 | + const [hasError, setHasError] = useState(false) |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (open && diffEditor) { |
| 32 | + try { |
| 33 | + const unifiedDiff = generateUnifiedDiff(diffEditor, 'before') |
| 34 | + |
| 35 | + const isError = unifiedDiff.startsWith('[ERROR]') || unifiedDiff.startsWith('[INFO]') |
| 36 | + |
| 37 | + setUnifiedDiffContent(unifiedDiff) |
| 38 | + setHasError(isError) |
| 39 | + console.log('Unified diff generated successfully') // ログメッセージを変更 |
| 40 | + } catch (error) { |
| 41 | + console.error('Error in UnifiedDiffDialog:', error) |
| 42 | + const errorMsg = `[ERROR] ${error instanceof Error ? error.message : String(error)}` |
| 43 | + setUnifiedDiffContent(errorMsg) |
| 44 | + setHasError(true) |
| 45 | + } |
| 46 | + } |
| 47 | + }, [open, diffEditor]) |
| 48 | + |
| 49 | + // highlight.jsでdiffをハイライト |
| 50 | + const highlightedHtml = useMemo(() => { |
| 51 | + if (!unifiedDiffContent || hasError) return unifiedDiffContent |
| 52 | + try { |
| 53 | + return hljs.highlight(unifiedDiffContent, { language: 'diff' }).value |
| 54 | + } catch (error) { |
| 55 | + console.error('Highlight error:', error) |
| 56 | + return unifiedDiffContent |
| 57 | + } |
| 58 | + }, [unifiedDiffContent, hasError]) |
| 59 | + |
| 60 | + const handleOpenChange = (isOpen: boolean): void => { |
| 61 | + onOpenChange(isOpen) |
| 62 | + if (!isOpen) { |
| 63 | + setCopied(false) |
| 64 | + setHasError(false) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const copyToClipboard = async (): Promise<void> => { |
| 69 | + try { |
| 70 | + await navigator.clipboard.writeText(unifiedDiffContent) |
| 71 | + setCopied(true) |
| 72 | + setTimeout(() => setCopied(false), 2000) |
| 73 | + } catch (err) { |
| 74 | + console.error('Failed to copy:', err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <Dialog open={open} onOpenChange={handleOpenChange}> |
| 80 | + {/* ダイアログの高さをコンテンツ依存にする */} |
| 81 | + <DialogContent |
| 82 | + className={cn( |
| 83 | + 'max-w-[calc(100vw-4rem)] w-full max-h-[90vh] flex flex-col p-6', |
| 84 | + themeName === 'dark' && 'dark' |
| 85 | + )} |
| 86 | + > |
| 87 | + <DialogHeader className="pb-4"> |
| 88 | + <DialogTitle className="text-foreground">Unified Diff</DialogTitle> |
| 89 | + </DialogHeader> |
| 90 | + |
| 91 | + <div className="relative border rounded-md max-h-[70vh] min-h-0 overflow-y-auto"> |
| 92 | + {!hasError && ( |
| 93 | + <Button |
| 94 | + size="sm" |
| 95 | + onClick={copyToClipboard} |
| 96 | + disabled={!unifiedDiffContent || hasError} |
| 97 | + className="absolute top-3 right-3 z-50" |
| 98 | + > |
| 99 | + {copied ? ( |
| 100 | + <> |
| 101 | + <i className="codicon codicon-check mr-2"></i> |
| 102 | + Copied! |
| 103 | + </> |
| 104 | + ) : ( |
| 105 | + <> |
| 106 | + <i className="codicon codicon-copy mr-2"></i> |
| 107 | + Copy |
| 108 | + </> |
| 109 | + )} |
| 110 | + </Button> |
| 111 | + )} |
| 112 | + |
| 113 | + <pre className="text-xs font-mono"> |
| 114 | + <code className="hljs" dangerouslySetInnerHTML={{ __html: highlightedHtml }} /> |
| 115 | + </pre> |
| 116 | + </div> |
| 117 | + </DialogContent> |
| 118 | + </Dialog> |
| 119 | + ) |
| 120 | +} |
0 commit comments