|
| 1 | +using System.Windows.Forms; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Tql.Abstractions; |
| 4 | +using Tql.Utilities; |
| 5 | + |
| 6 | +namespace Tql.Plugins.GitHub.Categories; |
| 7 | + |
| 8 | +internal class NewMatch(NewMatchDto dto) : IRunnableMatch, ISerializableMatch, ICopyableMatch |
| 9 | +{ |
| 10 | + public string Text => |
| 11 | + dto.Type switch |
| 12 | + { |
| 13 | + NewMatchType.Issue |
| 14 | + => MatchText.Path($"{dto.Owner}/{dto.Repository}", Labels.NewMatch_NewIssue), |
| 15 | + NewMatchType.PullRequest |
| 16 | + => MatchText.Path($"{dto.Owner}/{dto.Repository}", Labels.NewMatch_NewPullRequest), |
| 17 | + NewMatchType.Repository => Labels.NewMatch_NewRepository, |
| 18 | + NewMatchType.Gist => Labels.NewMatch_NewGist, |
| 19 | + NewMatchType.Organization => Labels.NewMatch_NewOrganization, |
| 20 | + NewMatchType.ImportRepository => Labels.NewMatch_ImportRepository, |
| 21 | + NewMatchType.Codespace => Labels.NewMatch_NewCodespace, |
| 22 | + _ => throw new ArgumentOutOfRangeException() |
| 23 | + }; |
| 24 | + |
| 25 | + public ImageSource Icon => |
| 26 | + dto.Type switch |
| 27 | + { |
| 28 | + NewMatchType.Issue => Images.Issue, |
| 29 | + NewMatchType.PullRequest => Images.PullRequest, |
| 30 | + NewMatchType.Repository => Images.Repository, |
| 31 | + NewMatchType.Gist => Images.Gist, |
| 32 | + NewMatchType.Organization => Images.Organization, |
| 33 | + NewMatchType.ImportRepository => Images.ImportRepository, |
| 34 | + NewMatchType.Codespace => Images.Codespace, |
| 35 | + _ => throw new ArgumentOutOfRangeException() |
| 36 | + }; |
| 37 | + |
| 38 | + public MatchTypeId TypeId => TypeIds.New; |
| 39 | + |
| 40 | + public Task Run(IServiceProvider serviceProvider, IWin32Window owner) |
| 41 | + { |
| 42 | + serviceProvider.GetRequiredService<IUI>().OpenUrl(GetUrl()); |
| 43 | + |
| 44 | + return Task.CompletedTask; |
| 45 | + } |
| 46 | + |
| 47 | + public string Serialize() |
| 48 | + { |
| 49 | + return JsonSerializer.Serialize(dto); |
| 50 | + } |
| 51 | + |
| 52 | + public Task Copy(IServiceProvider serviceProvider) |
| 53 | + { |
| 54 | + serviceProvider.GetRequiredService<IClipboard>().CopyUri(Text, GetUrl()); |
| 55 | + |
| 56 | + return Task.CompletedTask; |
| 57 | + } |
| 58 | + |
| 59 | + private string GetUrl() => |
| 60 | + dto.Type switch |
| 61 | + { |
| 62 | + NewMatchType.Issue |
| 63 | + => $"https://github.com/{dto.Owner}/{dto.Repository}/issues/new/choose", |
| 64 | + NewMatchType.PullRequest => $"https://github.com/{dto.Owner}/{dto.Repository}/compare", |
| 65 | + NewMatchType.Repository => "https://github.com/new", |
| 66 | + NewMatchType.Gist => "https://gist.github.com/", |
| 67 | + NewMatchType.Organization => "https://github.com/account/organizations/new", |
| 68 | + NewMatchType.ImportRepository => "https://github.com/new/import", |
| 69 | + NewMatchType.Codespace => "https://github.com/codespaces/new", |
| 70 | + _ => throw new ArgumentOutOfRangeException() |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +internal record NewMatchDto(Guid? Id, string? Owner, string? Repository, NewMatchType Type); |
| 75 | + |
| 76 | +internal enum NewMatchType |
| 77 | +{ |
| 78 | + Issue, |
| 79 | + PullRequest, |
| 80 | + Repository, |
| 81 | + Gist, |
| 82 | + Organization, |
| 83 | + ImportRepository, |
| 84 | + Codespace |
| 85 | +} |
0 commit comments