Skip to content

Commit

Permalink
Merge pull request #835 from EdiWang/feature/social-links
Browse files Browse the repository at this point in the history
Social link (preview)
  • Loading branch information
EdiWang authored Oct 21, 2024
2 parents d0bfbbb + 673c31a commit 0b745cf
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Moonglade.Configuration/SocialLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Moonglade.Configuration;

public class SocialLink
{
public string Name { get; set; }

public string Icon { get; set; }

public string Url { get; set; }
}
22 changes: 22 additions & 0 deletions src/Moonglade.Core/GetAllSocialLinksQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Configuration;
using Moonglade.Configuration;

namespace Moonglade.Core;

public record GetAllSocialLinksQuery : IRequest<List<SocialLink>>;

public class GetAllSocialLinksQueryHandler(IConfiguration configuration) : IRequestHandler<GetAllSocialLinksQuery, List<SocialLink>>
{
public Task<List<SocialLink>> Handle(GetAllSocialLinksQuery request, CancellationToken ct)
{
var section = configuration.GetSection("Experimental:SocialLinks");

if (!section.Exists())
{
return Task.FromResult(new List<SocialLink>());
}

var links = section.Get<List<SocialLink>>();
return Task.FromResult(links ?? new List<SocialLink>());
}
}
19 changes: 19 additions & 0 deletions src/Moonglade.Web/Pages/Components/SocialLink/Default.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using Moonglade.Utils
@model List<SocialLink>

@if (Model.Any())
{
<div class="aside-widget p-3 rounded-3 shadow-sm border">
<h6 class="card-subtitle mb-3 text-secondary">@SharedLocalizer["Social Links"]</h6>

<div role="list">
@foreach (var item in Model.OrderBy(c => c.Name))
{
<a href="@Helper.SterilizeLink(item.Url)" target="_blank" class="d-block mb-3 mt-2" role="listitem">
<i class="@item.Icon me-1"></i>
@item.Name
</a>
}
</div>
</div>
}
7 changes: 7 additions & 0 deletions src/Moonglade.Web/Pages/Shared/_Aside.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
</section>
}

@if (Configuration.GetSection("Experimental:SocialLinks").Exists())
{
<section id="aside-sociallink" class="mb-4">
@await Component.InvokeAsync("SocialLink")
</section>
}

@if (BlogConfig.GeneralSettings.WidgetsFriendLink)
{
<section id="aside-friendlink" class="mb-4">
Expand Down
18 changes: 18 additions & 0 deletions src/Moonglade.Web/ViewComponents/SocialLinkViewComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Moonglade.Web.ViewComponents;

public class SocialLinkViewComponent(ILogger<SocialLinkViewComponent> logger, IMediator mediator) : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
try
{
var links = await mediator.Send(new GetAllSocialLinksQuery());
return View(links ?? []);
}
catch (Exception e)
{
logger.LogError(e, "Error Reading SocialLink.");
return Content("ERROR");
}
}
}
3 changes: 3 additions & 0 deletions src/Moonglade.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"Enabled": true,
"HeaderName": "Sec-CH-Prefers-Color-Scheme"
},
"Experimental": {
"SocialLinks": []
},
"Logging": {
"LogLevel": {
"Default": "Warning",
Expand Down

0 comments on commit 0b745cf

Please sign in to comment.