< Summary

Information
Class: Chronicis.Client.Components.Search.SearchResultCard
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Components/Articles/SearchResultCard.razor
Line coverage
77%
Covered lines: 37
Uncovered lines: 11
Coverable lines: 48
Total lines: 118
Line coverage: 77%
Branch coverage
70%
Covered branches: 21
Total branches: 30
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Result()100%11100%
get_Query()100%11100%
get_OnClick()100%11100%
GetMatchTypeDisplay()83.33%6685.71%
GetMatchTypeColor()83.33%6685.71%
GetRelativeTime()30%301041.66%
GetBreadcrumbItems()100%11100%
HighlightMatch(...)100%4480%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Components/Articles/SearchResultCard.razor

#LineLine coverage
 1@namespace Chronicis.Client.Components.Search
 2@using Chronicis.Shared.DTOs
 3@using System.Text.RegularExpressions
 4
 5<MudCard Class="mb-3 search-result-card" @onclick="OnClick" Style="cursor: pointer;" Elevation="2">
 6    <MudCardContent>
 7        <div class="d-flex align-center mb-2">
 8            <MudChip T="string" Size="Size.Small"
 9                     Color="@GetMatchTypeColor()"
 10                     Variant="Variant.Text"
 11                     Style="font-weight: 500;">
 1512                @GetMatchTypeDisplay()
 13            </MudChip>
 14
 15            <MudSpacer />
 16
 17            <MudText Typo="Typo.caption" Color="Color.Secondary">
 1518                @GetRelativeTime()
 19            </MudText>
 20        </div>
 21
 22        <MudText Typo="Typo.h6" Class="mb-2">
 1523            @((MarkupString)HighlightMatch(Result.Title, Query))
 24        </MudText>
 25
 1526        @if (Result.AncestorPath.Any())
 27        {
 28            <MudBreadcrumbs Items="@GetBreadcrumbItems()"
 29                           Class="mb-2"
 30                           Style="font-size: 0.875rem; color: #666;" />
 31        }
 32
 1533        @if (!string.IsNullOrEmpty(Result.MatchSnippet))
 34        {
 35            <MudText Typo="Typo.body2" Class="search-snippet mt-2">
 236                @((MarkupString)HighlightMatch(Result.MatchSnippet, Query))
 37            </MudText>
 38        }
 39    </MudCardContent>
 40</MudCard>
 41
 42@code {
 43    [Parameter, EditorRequired]
 10844    public ArticleSearchResultDto Result { get; set; } = null!;
 45
 46    [Parameter, EditorRequired]
 4747    public string Query { get; set; } = string.Empty;
 48
 49    [Parameter]
 1650    public EventCallback OnClick { get; set; }
 51
 52    private string GetMatchTypeDisplay()
 53    {
 1554        return Result.MatchType switch
 1555        {
 1156            "title" => "Title Match",
 257            "content" => "Content Match",
 258            "hashtag" => "Hashtag Match",
 059            _ => "Match"
 1560        };
 61    }
 62
 63    private Color GetMatchTypeColor()
 64    {
 1565        return Result.MatchType switch
 1566        {
 1167            "title" => Color.Primary,
 268            "content" => Color.Info,
 269            "hashtag" => Color.Success,
 070            _ => Color.Default
 1571        };
 72    }
 73
 74    private string GetRelativeTime()
 75    {
 1576        var diff = DateTime.UtcNow - Result.LastModified;
 77
 1578        if (diff.TotalMinutes < 1)
 079            return "Just now";
 1580        if (diff.TotalMinutes < 60)
 081            return $"{(int)diff.TotalMinutes}m ago";
 1582        if (diff.TotalHours < 24)
 1583            return $"{(int)diff.TotalHours}h ago";
 084        if (diff.TotalDays < 7)
 085            return $"{(int)diff.TotalDays}d ago";
 086        if (diff.TotalDays < 30)
 087            return $"{(int)(diff.TotalDays / 7)}w ago";
 88
 089        return Result.LastModified.ToString("MMM d, yyyy");
 90    }
 91
 92    private List<BreadcrumbItem> GetBreadcrumbItems()
 93    {
 194        return Result.AncestorPath.Select(b =>
 295            new BreadcrumbItem(b.Title, href: null, disabled: true)
 196        ).ToList();
 97    }
 98
 99    private string HighlightMatch(string text, string query)
 100    {
 17101        if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(query))
 1102            return text;
 103
 104        try
 105        {
 16106            var regex = new Regex(
 16107                Regex.Escape(query),
 16108                RegexOptions.IgnoreCase);
 109
 16110            return regex.Replace(text, match =>
 30111                $"<mark class=\"search-highlight\">{match.Value}</mark>");
 112        }
 0113        catch
 114        {
 0115            return text;
 116        }
 16117    }
 118}