📝 docs: show the count of github star (#2423)

* 📝 : show the count of github star

* format code
This commit is contained in:
capdiem
2025-05-30 09:29:00 +08:00
committed by GitHub
parent ab971e1afe
commit 8bfea5ae41
11 changed files with 147 additions and 94 deletions

View File

@@ -1,3 +1,3 @@
namespace Masa.Docs.Core.Models;
public record Project(string Name, string IconUrl, string RepoUrl, string? Path = null);
public record Project(string Name, string? Repo, string IconUrl, string RepoUrl, string? Path = null);

View File

@@ -127,4 +127,28 @@ public class GithubService(ExpiryLocalStorage localStorage, ILogger<GithubServic
return null;
}
}
}
public async ValueTask<int> GetStarCountAsync(string owner, string repo)
{
var key = repo + "__starCount";
var starCount = await localStorage.GetExpiryItemAsync<int?>(key);
if (starCount.HasValue)
{
return starCount.Value;
}
try
{
var client = CreateClient(owner, repo);
var repository = await client.Repository.Get(owner, repo);
starCount = repository.StargazersCount;
await localStorage.SetExpiryItemAsync(key, starCount, TimeSpan.FromHours(1));
return starCount.Value;
}
catch (Exception ex)
{
logger.LogError(ex, "Error fetching star count");
return 0;
}
}
}

View File

@@ -1,12 +0,0 @@
@namespace Masa.Docs.Shared.Components
<MButton Icon OnClick="OnDotClick">
<MIcon>mdi-dots-vertical</MIcon>
</MButton>
@code {
[Parameter]
public EventCallback OnDotClick { get; set; }
}

View File

@@ -1,38 +0,0 @@
@namespace Masa.Docs.Shared.Components
@if (!string.IsNullOrWhiteSpace(RepoUrl))
{
<AppTooltipButton Href="@RepoUrl"
Target="_blank"
Icon="mdi-github"
Path="github">
</AppTooltipButton>
}
@if (RepoUrl?.Contains("MASA.Blazor", StringComparison.OrdinalIgnoreCase) is true && Culture == "zh-CN")
{
<AppTooltipButton Href="https://gitee.com/blazorcomponent/MASA.Blazor"
Target="_blank"
Icon="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z"
IconSvgAttrs="@(new Dictionary<string, object?>() { { "viewBox", "0 0 1024 1024" } })"
Path="gitee">
</AppTooltipButton>
}
<AppTooltipButton OnClick="@OnSettingsClick"
Icon="mdi-cog-outline"
Path="settings">
</AppTooltipButton>
@code {
[CascadingParameter(Name = "Culture")]
public string? Culture { get; set; }
[Parameter]
public string? RepoUrl { get; set; }
[Parameter]
public EventCallback OnSettingsClick { get; set; }
}

View File

@@ -1,5 +1,4 @@
@using System.Globalization
@inject MasaBlazor MasaBlazor
@inject MasaBlazor MasaBlazor
@implements IDisposable
<MAppBar Id="app-bar"
@@ -14,13 +13,13 @@
<Logo />
</div>
@if (HideAppBarNavIcon is false && Project is not null)
@if (HideAppBarNavIcon is false && ProjectInfo is not null)
{
<MAppBarNavIcon Class="hidden-lg-and-up"
@onclick="OnAppBarNavIconClick" />
}
<AppBarItems RepoUrl="@RepoUrl"
<AppBarItems ProjectInfo="@ProjectInfo"
OnCultureChanged="OnCultureChanged"
OnDotClick="OnDotClick"
OnSettingsClick="OnSettingsClick">
@@ -32,14 +31,11 @@
[CascadingParameter(Name = "Culture")]
public string? Culture { get; set; }
[CascadingParameter(Name = "project")]
public string? Project { get; set; }
[CascadingParameter(Name = "Env")]
public string? Env { get; set; }
[Parameter]
public string? RepoUrl { get; set; }
public Project? ProjectInfo { get; set; }
[Parameter]
public EventCallback<string> OnCultureChanged { get; set; }

View File

@@ -1,71 +1,105 @@
@inject I18n I18n
@inject MasaBlazor MasaBlazor
@inject GithubService GithubService
@implements IDisposable
<div class="flex-1-1-auto d-flex align-center justify-end ml-4">
@if (MasaBlazor.Breakpoint.LgAndUp)
{
<Tabs />
<Tabs/>
}
@if (MasaBlazor.Breakpoint.XsOnly)
{
<MSpacer />
<MSpacer/>
}
@if (!string.IsNullOrEmpty(Project))
@if (ProjectInfo != null)
{
<Search @ref="_algoliaSearch" Class="mr-2" />
<Search @ref="_algoliaSearch" Class="mr-2"/>
}
<LanguageMenu OnCultureChanged="OnCultureChangedLocal" />
<LanguageMenu OnCultureChanged="OnCultureChangedLocal"/>
<NotificationsMenu />
<ThemeToggle />
<ThemeToggle/>
@if (MasaBlazor.Breakpoint.LgAndUp)
{
<ToolIcons RepoUrl="@RepoUrl" OnSettingsClick="OnSettingsClick" />
@if (!string.IsNullOrWhiteSpace(ProjectInfo?.RepoUrl))
{
@if (_starCount == 0)
{
<AppTooltipButton Href="@ProjectInfo.RepoUrl"
Target="_blank"
Icon="mdi-github"
Path="star-me">
</AppTooltipButton>
}
else
{
<MButton Href="@ProjectInfo.RepoUrl"
Target="_blank"
Text
Large
Class="pr-2">
<MTooltip Activator="parent" Text="@(I18n.T("star-me"))" Bottom></MTooltip>
<MIcon Left Size="24">mdi-github</MIcon>
@StarCount
</MButton>
}
}
@if (ProjectInfo?.RepoUrl.Contains("MASA.Blazor", StringComparison.OrdinalIgnoreCase) is true && Culture == "zh-CN")
{
<AppTooltipButton Href="https://gitee.com/blazorcomponent/MASA.Blazor"
Target="_blank"
Icon="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z"
IconSvgAttrs="@(new Dictionary<string, object?>() { { "viewBox", "0 0 1024 1024" } })"
Path="gitee">
</AppTooltipButton>
}
<NotificationsMenu/>
<AppTooltipButton OnClick="@OnSettingsClick"
Icon="mdi-cog-outline"
Path="settings">
</AppTooltipButton>
}
else
{
<MobileToolIcons OnDotClick="OnDotClick" />
<MButton IconName="mdi-dots-vertical" OnClick="@OnDotClick"/>
}
</div>
@code {
[CascadingParameter(Name = "project")]
public string? Project { get; set; }
[CascadingParameter(Name = "Culture")]
public string? Culture { get; set; }
[CascadingParameter(Name = "Env")]
public string? Env { get; set; }
[Parameter]
public string? RepoUrl { get; set; }
public Project? ProjectInfo { get; set; }
[Parameter]
public EventCallback<string> OnCultureChanged { get; set; }
private Search? _algoliaSearch;
public async Task OnCultureChangedLocal(string value)
{
await OnCultureChanged.InvokeAsync(value);
await _algoliaSearch!.InitDocSearchAsync();
}
[Parameter]
public EventCallback OnDotClick { get; set; }
[Parameter]
public EventCallback OnSettingsClick { get; set; }
private int _starCount;
private string? _prevRepo;
private Search? _algoliaSearch;
protected override void OnInitialized()
{
base.OnInitialized();
MasaBlazor.MobileChanged += MasaBlazorOnMobileChanged;
}
@@ -74,6 +108,51 @@
InvokeAsync(StateHasChanged);
}
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
if (_prevRepo != ProjectInfo?.Repo)
{
_prevRepo = ProjectInfo?.Repo;
await UpdateStarCountAsync();
}
}
private async Task UpdateStarCountAsync()
{
if (string.IsNullOrWhiteSpace(ProjectInfo?.Repo))
{
_starCount = 0;
}
else
{
_starCount = await GithubService.GetStarCountAsync("masastack", ProjectInfo.Repo);
}
StateHasChanged();
}
private string? StarCount
{
get
{
return _starCount switch
{
0 => null,
< 1000 => _starCount.ToString(),
_ => $"{_starCount / 1000.0:F1}K"
};
}
}
public async Task OnCultureChangedLocal(string value)
{
await OnCultureChanged.InvokeAsync(value);
await _algoliaSearch!.InitDocSearchAsync();
}
public void Dispose()
{
MasaBlazor.MobileChanged -= MasaBlazorOnMobileChanged;

View File

@@ -20,10 +20,10 @@
<CascadingValue Value="@_env" Name="Env">
<AppBar OnCultureChanged="@OnCultureChanged"
OnAppBarNavIconClick="@HandleOnAppBarNavIconClick"
OnDotClick="HandleOnDotClick"
OnSettingsClick="HandleSettingsClick"
OnDotClick="@HandleOnDotClick"
OnSettingsClick="@HandleSettingsClick"
HideAppBarNavIcon="@_hideAppBarNavIcon"
RepoUrl="@_projectInfo?.RepoUrl" />
ProjectInfo="@_projectInfo" />
</CascadingValue>
<CascadingValue Value="this" IsFixed>

View File

@@ -6,10 +6,6 @@ namespace Masa.Docs.Shared.Shared;
public partial class BaseLayout
{
private static readonly Project s_emptyProject = new("MASA Stack",
"https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png?x-oss-process=image/resize,h_24,m_lfit",
"https://github.com/masastack");
private bool? _showSettings;
private bool? _showMobileMenuList;
private bool _hideAppBarNavIcon;
@@ -103,10 +99,13 @@ public partial class BaseLayout
private void UpdateProjectInfo()
{
if (!(_project is not null && _projectMap.TryGetValue(_project, out _projectInfo)))
{
_projectInfo = s_emptyProject;
}
if (_project is not null && _projectMap.TryGetValue(_project, out _projectInfo)) return;
_projectInfo = new Project(
"MASA Stack",
null,
"https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png?x-oss-process=image/resize,h_24,m_lfit",
"https://github.com/masastack");
}
private void OnCultureChanged(string cultureName)

View File

@@ -2,18 +2,21 @@
"blazor": {
"name": "MASA Blazor",
"path": "Masa.Blazor.Docs",
"repo": "Masa.Blazor",
"iconUrl": "https://cdn.masastack.com/stack/images/website/masa-blazor/logo.png?x-oss-process=image/resize,h_24,m_lfit",
"repoUrl": "https://github.com/masastack/MASA.Blazor"
},
"framework": {
"name": "MASA Framework",
"path": "Masa.Framework.Docs",
"repo": "Masa.Framework",
"iconUrl": "https://cdn.masastack.com/images/framework_logo.png?x-oss-process=image/resize,h_24,m_lfit",
"repoUrl": "https://github.com/masastack/MASA.Framework"
},
"stack": {
"name": "MASA Stack",
"path": "Masa.Stack.Docs",
"repo": "Masa.Stack",
"iconUrl": "https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png?x-oss-process=image/resize,h_24,m_lfit",
"repoUrl": "https://github.com/masastack/MASA.Stack"
}

View File

@@ -291,6 +291,7 @@
"not-supported": "Not supported",
"toggle-theme": "Toggle theme",
"github": "Github",
"star-me": "Star me on Github",
"gitee": "Gitee",
"settings": "Settings",
"wireframes": "Wireframes",

View File

@@ -287,6 +287,7 @@
"not-supported": "不支持",
"toggle-theme": "切换主题",
"github": "Github",
"star-me": "每颗 Star 都会变成更新的动力 ⚡",
"gitee": "Gitee",
"settings": "设置",
"wireframes": "预置布局",