|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { api } from '~/trpc/react'; |
| 5 | + |
| 6 | +export function RepoStatusButton() { |
| 7 | + const [isUpdating, setIsUpdating] = useState(false); |
| 8 | + const [updateMessage, setUpdateMessage] = useState<string | null>(null); |
| 9 | + const [updateSteps, setUpdateSteps] = useState<string[]>([]); |
| 10 | + const [showSteps, setShowSteps] = useState(false); |
| 11 | + |
| 12 | + // Query repository status |
| 13 | + const { data: repoStatus, refetch: refetchStatus } = api.scripts.getRepoStatus.useQuery(); |
| 14 | + |
| 15 | + // Full update mutation |
| 16 | + const fullUpdateMutation = api.scripts.fullUpdateRepo.useMutation({ |
| 17 | + onSuccess: (data) => { |
| 18 | + setIsUpdating(false); |
| 19 | + setUpdateMessage(data.message); |
| 20 | + setUpdateSteps(data.steps); |
| 21 | + setShowSteps(true); |
| 22 | + |
| 23 | + if (data.success) { |
| 24 | + // Refetch status after successful update |
| 25 | + setTimeout(() => { |
| 26 | + void refetchStatus(); |
| 27 | + }, 1000); |
| 28 | + |
| 29 | + // Clear message after 5 seconds for success |
| 30 | + setTimeout(() => { |
| 31 | + setUpdateMessage(null); |
| 32 | + setShowSteps(false); |
| 33 | + }, 5000); |
| 34 | + } else { |
| 35 | + // Clear message after 10 seconds for errors |
| 36 | + setTimeout(() => { |
| 37 | + setUpdateMessage(null); |
| 38 | + setShowSteps(false); |
| 39 | + }, 10000); |
| 40 | + } |
| 41 | + }, |
| 42 | + onError: (error) => { |
| 43 | + setIsUpdating(false); |
| 44 | + setUpdateMessage(`Error: ${error.message}`); |
| 45 | + setUpdateSteps([`❌ ${error.message}`]); |
| 46 | + setShowSteps(true); |
| 47 | + setTimeout(() => { |
| 48 | + setUpdateMessage(null); |
| 49 | + setShowSteps(false); |
| 50 | + }, 10000); |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const handleFullUpdate = async () => { |
| 55 | + setIsUpdating(true); |
| 56 | + setUpdateMessage(null); |
| 57 | + setUpdateSteps([]); |
| 58 | + setShowSteps(false); |
| 59 | + fullUpdateMutation.mutate(); |
| 60 | + }; |
| 61 | + |
| 62 | + const getStatusColor = () => { |
| 63 | + if (!repoStatus?.isRepo) return 'text-gray-500'; |
| 64 | + if (repoStatus.isBehind) return 'text-orange-500'; |
| 65 | + return 'text-green-500'; |
| 66 | + }; |
| 67 | + |
| 68 | + const getStatusIcon = () => { |
| 69 | + if (!repoStatus?.isRepo) return '❓'; |
| 70 | + if (repoStatus.isBehind) return '⚠️'; |
| 71 | + return '✅'; |
| 72 | + }; |
| 73 | + |
| 74 | + const getStatusText = () => { |
| 75 | + if (!repoStatus?.isRepo) return 'Not a git repository'; |
| 76 | + if (repoStatus.isBehind) return 'Updates available'; |
| 77 | + return 'Up to date'; |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="flex flex-col space-y-4"> |
| 82 | + {/* Status Display */} |
| 83 | + <div className="flex items-center justify-between bg-white rounded-lg shadow-sm border p-4"> |
| 84 | + <div className="flex items-center space-x-3"> |
| 85 | + <div className="flex items-center space-x-2"> |
| 86 | + <span className="text-2xl">{getStatusIcon()}</span> |
| 87 | + <div> |
| 88 | + <div className={`font-medium ${getStatusColor()}`}> |
| 89 | + Repository Status: {getStatusText()} |
| 90 | + </div> |
| 91 | + {repoStatus?.isRepo && ( |
| 92 | + <div className="text-sm text-gray-500"> |
| 93 | + Branch: {repoStatus.branch ?? 'unknown'} | |
| 94 | + Last commit: {repoStatus.lastCommit ? repoStatus.lastCommit.substring(0, 8) : 'unknown'} |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="flex items-center space-x-3"> |
| 102 | + {repoStatus?.isBehind && ( |
| 103 | + <button |
| 104 | + onClick={handleFullUpdate} |
| 105 | + disabled={isUpdating} |
| 106 | + className={`flex items-center space-x-2 px-4 py-2 rounded-lg font-medium transition-colors ${ |
| 107 | + isUpdating |
| 108 | + ? 'bg-gray-400 text-white cursor-not-allowed' |
| 109 | + : 'bg-orange-600 text-white hover:bg-orange-700' |
| 110 | + }`} |
| 111 | + > |
| 112 | + {isUpdating ? ( |
| 113 | + <> |
| 114 | + <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div> |
| 115 | + <span>Updating...</span> |
| 116 | + </> |
| 117 | + ) : ( |
| 118 | + <> |
| 119 | + <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 120 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> |
| 121 | + </svg> |
| 122 | + <span>Update Repository</span> |
| 123 | + </> |
| 124 | + )} |
| 125 | + </button> |
| 126 | + )} |
| 127 | + |
| 128 | + <button |
| 129 | + onClick={() => refetchStatus()} |
| 130 | + className="flex items-center space-x-2 px-3 py-2 text-gray-600 hover:text-gray-800 hover:bg-gray-100 rounded-lg transition-colors" |
| 131 | + title="Refresh status" |
| 132 | + > |
| 133 | + <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 134 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> |
| 135 | + </svg> |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + {/* Update Message */} |
| 141 | + {updateMessage && ( |
| 142 | + <div className={`p-4 rounded-lg ${ |
| 143 | + updateMessage.includes('Error') || updateMessage.includes('Failed') |
| 144 | + ? 'bg-red-100 text-red-700 border border-red-200' |
| 145 | + : 'bg-green-100 text-green-700 border border-green-200' |
| 146 | + }`}> |
| 147 | + <div className="font-medium mb-2">{updateMessage}</div> |
| 148 | + {showSteps && updateSteps.length > 0 && ( |
| 149 | + <div className="mt-3"> |
| 150 | + <button |
| 151 | + onClick={() => setShowSteps(!showSteps)} |
| 152 | + className="text-sm font-medium hover:underline" |
| 153 | + > |
| 154 | + {showSteps ? 'Hide' : 'Show'} update steps |
| 155 | + </button> |
| 156 | + {showSteps && ( |
| 157 | + <div className="mt-2 space-y-1"> |
| 158 | + {updateSteps.map((step, index) => ( |
| 159 | + <div key={index} className="text-sm font-mono"> |
| 160 | + {step} |
| 161 | + </div> |
| 162 | + ))} |
| 163 | + </div> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + )} |
| 169 | + |
| 170 | + {/* Update Steps (always show when updating) */} |
| 171 | + {isUpdating && updateSteps.length > 0 && ( |
| 172 | + <div className="bg-blue-50 border border-blue-200 rounded-lg p-4"> |
| 173 | + <div className="font-medium text-blue-800 mb-2">Update Progress:</div> |
| 174 | + <div className="space-y-1"> |
| 175 | + {updateSteps.map((step, index) => ( |
| 176 | + <div key={index} className="text-sm font-mono text-blue-700"> |
| 177 | + {step} |
| 178 | + </div> |
| 179 | + ))} |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + )} |
| 183 | + </div> |
| 184 | + ); |
| 185 | +} |
0 commit comments