GitStatusIndicator.razor 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. @using RackPeek.Domain.Git
  2. @using RackPeek.Domain.Git.UseCases
  3. @using RackPeek.Domain.Persistence
  4. @inject InitRepoUseCase InitRepo
  5. @inject CommitAllUseCase CommitAll
  6. @inject RestoreAllUseCase RestoreAll
  7. @inject PushUseCase PushUseCase
  8. @inject PullUseCase PullUseCase
  9. @inject AddRemoteUseCase AddRemoteUseCase
  10. @inject IGitRepository GitRepo
  11. @inject IResourceCollection Resources
  12. @implements IDisposable
  13. <div class="flex items-center gap-3 text-xs">
  14. @if (_status == GitRepoStatus.Clean)
  15. {
  16. <span class="flex items-center gap-1 text-zinc-400">
  17. <span class="w-2 h-2 rounded-full bg-emerald-400"></span>
  18. Saved
  19. </span>
  20. }
  21. else if (_status == GitRepoStatus.Dirty)
  22. {
  23. <span class="flex items-center gap-1 text-zinc-400">
  24. <span class="w-2 h-2 rounded-full bg-amber-400 animate-pulse"></span>
  25. </span>
  26. <span class="text-zinc-600">·</span>
  27. <button class="hover:text-emerald-400"
  28. disabled="@_isBusy"
  29. @onclick="CommitAsync">
  30. @(_isCommitting ? "Saving…" : "Save")
  31. </button>
  32. <span class="text-zinc-600">·</span>
  33. <button class="hover:text-red-400"
  34. disabled="@_isBusy"
  35. @onclick="DiscardAsync">
  36. @(_isRestoring ? "Discarding…" : "Discard")
  37. </button>
  38. }
  39. @if (_status == GitRepoStatus.NotAvailable)
  40. {
  41. <span class="flex items-center gap-1 text-amber-400" data-testid="git-unavailable">
  42. <span class="w-2 h-2 rounded-full bg-amber-400"></span>
  43. Git configured but config directory is not writable
  44. </span>
  45. }
  46. else if (!_hasRemote)
  47. {
  48. <span class="text-zinc-600">·</span>
  49. @if (_showAddRemote)
  50. {
  51. <input type="text"
  52. class="px-2 py-1 text-xs rounded bg-zinc-800 border border-zinc-700 text-zinc-200 w-56"
  53. placeholder="https://your-git-host/user/repo.git"
  54. @bind="_remoteUrl"
  55. @bind:event="oninput" />
  56. <button class="hover:text-emerald-400"
  57. disabled="@(string.IsNullOrWhiteSpace(_remoteUrl))"
  58. @onclick="AddRemoteAsync">
  59. Add
  60. </button>
  61. <button class="hover:text-zinc-400"
  62. @onclick="CancelAddRemote">
  63. Cancel
  64. </button>
  65. }
  66. else
  67. {
  68. <button class="text-zinc-400 hover:text-emerald-400"
  69. @onclick="() => _showAddRemote = true">
  70. Add Remote
  71. </button>
  72. }
  73. }
  74. else
  75. {
  76. <span class="text-zinc-600">·</span>
  77. <button class="text-zinc-400 hover:text-white"
  78. disabled="@(_isSyncing || _isFetching)"
  79. @onclick="ToggleSyncAsync">
  80. @if (_isFetching)
  81. {
  82. <span>Checking…</span>
  83. }
  84. else
  85. {
  86. <span>
  87. Sync
  88. @if (_syncStatus.Ahead > 0)
  89. {
  90. <span class="text-emerald-400"> ↑@_syncStatus.Ahead</span>
  91. }
  92. @if (_syncStatus.Behind > 0)
  93. {
  94. <span class="text-blue-400"> ↓@_syncStatus.Behind</span>
  95. }
  96. </span>
  97. }
  98. </button>
  99. @if (_syncStatus.Ahead > 0)
  100. {
  101. <span class="text-zinc-600">·</span>
  102. <button class="hover:text-emerald-400"
  103. disabled="@_isSyncing"
  104. @onclick="PushAsync">
  105. @(_isPushing ? "Pushing…" : "Push")
  106. </button>
  107. }
  108. @if (_syncStatus.Behind > 0)
  109. {
  110. <span class="text-zinc-600">·</span>
  111. <button class="hover:text-blue-400"
  112. disabled="@_isSyncing"
  113. @onclick="PullAsync">
  114. @(_isPulling ? "Pulling…" : "Pull")
  115. </button>
  116. }
  117. }
  118. @if (_errorMessage is not null)
  119. {
  120. <span class="text-red-400">@_errorMessage</span>
  121. }
  122. </div>
  123. @code {
  124. private GitRepoStatus _status = GitRepoStatus.NotAvailable;
  125. private bool _isCommitting;
  126. private bool _isRestoring;
  127. private bool _showAddRemote;
  128. private bool _hasRemote;
  129. private bool _isFetching;
  130. private bool _isPushing;
  131. private bool _isPulling;
  132. private string? _errorMessage;
  133. private string _remoteUrl = "";
  134. private PeriodicTimer? _timer;
  135. private CancellationTokenSource? _cts;
  136. private GitSyncStatus _syncStatus = new(0, 0, false);
  137. private bool _isBusy => _isCommitting || _isRestoring || _isSyncing;
  138. private bool _isSyncing => _isPushing || _isPulling || _isFetching;
  139. protected override async Task OnInitializedAsync()
  140. {
  141. _status = await Task.Run(() => GitRepo.GetStatus());
  142. if (_status == GitRepoStatus.NotAvailable)
  143. return;
  144. _hasRemote = await Task.Run(() => GitRepo.HasRemote());
  145. _cts = new CancellationTokenSource();
  146. _timer = new PeriodicTimer(TimeSpan.FromSeconds(15));
  147. _ = PollStatusAsync(_cts.Token);
  148. }
  149. private async Task PollStatusAsync(CancellationToken ct)
  150. {
  151. try
  152. {
  153. while (_timer != null && await _timer.WaitForNextTickAsync(ct))
  154. {
  155. if (_isBusy)
  156. continue;
  157. var newStatus = await Task.Run(() => GitRepo.GetStatus(), ct);
  158. if (newStatus != _status)
  159. {
  160. _status = newStatus;
  161. await InvokeAsync(StateHasChanged);
  162. }
  163. }
  164. }
  165. catch (OperationCanceledException) {}
  166. }
  167. private void CancelAddRemote()
  168. {
  169. _showAddRemote = false;
  170. _remoteUrl = "";
  171. }
  172. private async Task AddRemoteAsync()
  173. {
  174. _errorMessage = null;
  175. try
  176. {
  177. var error = await AddRemoteUseCase.ExecuteAsync(_remoteUrl);
  178. if (error != null)
  179. {
  180. _errorMessage = error;
  181. return;
  182. }
  183. _hasRemote = true;
  184. _showAddRemote = false;
  185. _remoteUrl = "";
  186. _syncStatus = await Task.Run(() => GitRepo.FetchAndGetSyncStatus());
  187. if (_syncStatus.Behind > 0)
  188. await PullAsync();
  189. }
  190. catch (Exception ex)
  191. {
  192. _errorMessage = $"Remote error: {ex.Message}";
  193. }
  194. }
  195. private async Task CommitAsync()
  196. {
  197. _errorMessage = null;
  198. _isCommitting = true;
  199. try
  200. {
  201. var error = await CommitAll.ExecuteAsync(
  202. $"rackpeek: save config {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");
  203. if (error != null)
  204. _errorMessage = error;
  205. _status = await Task.Run(() => GitRepo.GetStatus());
  206. await Resources.LoadAsync();
  207. }
  208. catch (Exception ex)
  209. {
  210. _errorMessage = $"Commit error: {ex.Message}";
  211. }
  212. finally
  213. {
  214. _isCommitting = false;
  215. }
  216. }
  217. private async Task ToggleSyncAsync()
  218. {
  219. _isFetching = true;
  220. try
  221. {
  222. _syncStatus = await Task.Run(() => GitRepo.FetchAndGetSyncStatus());
  223. }
  224. finally
  225. {
  226. _isFetching = false;
  227. }
  228. }
  229. private async Task PushAsync()
  230. {
  231. _errorMessage = null;
  232. _isPushing = true;
  233. try
  234. {
  235. var error = await PushUseCase.ExecuteAsync();
  236. if (error != null)
  237. _errorMessage = error;
  238. _syncStatus = await Task.Run(() => GitRepo.FetchAndGetSyncStatus());
  239. }
  240. catch (Exception ex)
  241. {
  242. _errorMessage = $"Push error: {ex.Message}";
  243. }
  244. finally
  245. {
  246. _isPushing = false;
  247. }
  248. }
  249. private async Task PullAsync()
  250. {
  251. _errorMessage = null;
  252. _isPulling = true;
  253. try
  254. {
  255. var error = await PullUseCase.ExecuteAsync();
  256. if (error != null)
  257. _errorMessage = error;
  258. _syncStatus = await Task.Run(() => GitRepo.FetchAndGetSyncStatus());
  259. _status = await Task.Run(() => GitRepo.GetStatus());
  260. await Resources.LoadAsync();
  261. }
  262. catch (Exception ex)
  263. {
  264. _errorMessage = $"Pull error: {ex.Message}";
  265. }
  266. finally
  267. {
  268. _isPulling = false;
  269. }
  270. }
  271. private async Task DiscardAsync()
  272. {
  273. _errorMessage = null;
  274. _isRestoring = true;
  275. try
  276. {
  277. var error = await RestoreAll.ExecuteAsync();
  278. if (error != null)
  279. _errorMessage = error;
  280. _status = await Task.Run(() => GitRepo.GetStatus());
  281. await Resources.LoadAsync();
  282. }
  283. catch (Exception ex)
  284. {
  285. _errorMessage = $"Discard error: {ex.Message}";
  286. }
  287. finally
  288. {
  289. _isRestoring = false;
  290. }
  291. }
  292. public void Dispose()
  293. {
  294. _cts?.Cancel();
  295. _cts?.Dispose();
  296. _timer?.Dispose();
  297. }
  298. }