-
-
Notifications
You must be signed in to change notification settings - Fork 362
doc(Vote): add gitee vote toast #7009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,14 @@ | |
|
|
||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.JSInterop; | ||
| using System.Globalization; | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Layout; | ||
|
|
||
| /// <summary> | ||
| /// 母版页基类 | ||
| /// </summary> | ||
| public partial class BaseLayout : IDisposable | ||
| public partial class BaseLayout : IAsyncDisposable | ||
| { | ||
| [Inject] | ||
| [NotNull] | ||
|
|
@@ -50,6 +51,8 @@ public partial class BaseLayout : IDisposable | |
| private string? CancelText { get; set; } | ||
|
|
||
| private bool _init = false; | ||
| private JSModule? _module; | ||
| private DotNetObjectReference<BaseLayout>? _interop; | ||
|
||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
|
|
@@ -71,13 +74,18 @@ protected override void OnInitialized() | |
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override async Task OnInitializedAsync() | ||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| await base.OnInitializedAsync(); | ||
| await base.OnAfterRenderAsync(firstRender); | ||
|
|
||
| var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js"); | ||
| await module.InvokeVoidAsync("initTheme"); | ||
| _init = true; | ||
| if (firstRender) | ||
| { | ||
| _module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js"); | ||
| _interop = DotNetObjectReference.Create(this); | ||
| await _module.InvokeVoidAsync("doTask", _interop); | ||
| _init = true; | ||
| StateHasChanged(); | ||
| } | ||
| } | ||
|
|
||
| private async Task NotifyCommit(DispatchEntry<GiteePostBody> payload) | ||
|
|
@@ -121,25 +129,55 @@ private async Task NotifyReboot(DispatchEntry<bool> payload) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 显示投票弹窗 | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [JSInvokable] | ||
| public async Task ShowVoteToast() | ||
| { | ||
| // 英文环境不投票 | ||
| if(CultureInfo.CurrentUICulture.Name == "en-US") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _option = new ToastOption() | ||
| { | ||
| Category = ToastCategory.Information, | ||
| Title = "Gitee 评选活动", | ||
| IsAutoHide = false, | ||
| ChildContent = RenderVote, | ||
| PreventDuplicates = true | ||
| }; | ||
| await Toast.Show(_option); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 释放资源 | ||
| /// </summary> | ||
| /// <param name="disposing"></param> | ||
| private void Dispose(bool disposing) | ||
| private async ValueTask DisposeAsync(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| CommitDispatchService.UnSubscribe(NotifyCommit); | ||
| RebootDispatchService.UnSubscribe(NotifyReboot); | ||
|
|
||
| if (_module != null) | ||
| { | ||
| await _module.InvokeVoidAsync("dispose"); | ||
| await _module.DisposeAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 释放资源 | ||
| /// </summary> | ||
| public void Dispose() | ||
| public async ValueTask DisposeAsync() | ||
| { | ||
| Dispose(true); | ||
| await DisposeAsync(true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,41 @@ | ||
| import { getTheme, setTheme } from "../../_content/BootstrapBlazor/modules/utility.js" | ||
| import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js" | ||
|
|
||
| export function initTheme() { | ||
| function initTheme() { | ||
| const currentTheme = getTheme(); | ||
| setTheme(currentTheme, false); | ||
| } | ||
|
|
||
| export function doTask(invoke) { | ||
| initTheme(); | ||
|
|
||
| const v = localStorage.getItem('bb-gitee-vote'); | ||
| if (v) { | ||
| try { | ||
| const differ = new Date().getTime() - v; | ||
| if (differ < 86400000) { | ||
|
||
| return; | ||
| } | ||
| } | ||
| catch { | ||
| localStorage.removeItem('bb-gitee-vote'); | ||
| } | ||
| } | ||
| const handler = setTimeout(async () => { | ||
| clearTimeout(handler); | ||
| await invoke.invokeMethodAsync("ShowVoteToast"); | ||
|
Comment on lines
+24
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Using setTimeout with async function may cause unhandled promise rejections. Since errors in the async callback won't be caught, wrap the function body in try/catch or handle the promise to prevent unhandled rejections. |
||
| }, 10000); | ||
|
||
|
|
||
| EventHandler.on(document, 'click', '#bb-gitee-vote', e => { | ||
| const toast = e.delegateTarget.closest('.toast'); | ||
| if (toast) { | ||
| toast.classList.remove('show'); | ||
|
|
||
| localStorage.setItem('bb-gitee-vote', new Date().getTime()); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export function dispose() { | ||
| EventHandler.off(document, 'click', '#bb-gitee-vote'); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The text '我正在参加' (I am participating) should be '我们正在参加' (We are participating) to correctly represent the project rather than an individual.