diff --git a/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsPageTests.cs b/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsPageTests.cs index 45e9b5e..244daf3 100644 --- a/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsPageTests.cs +++ b/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsPageTests.cs @@ -1,4 +1,5 @@ using Bunit; +using Microsoft.AspNetCore.Components; using Microsoft.Extensions.DependencyInjection; using Microsoft.FluentUI.AspNetCore.Components; using NoteBookmark.BlazorApp.Tests.Helpers; @@ -62,4 +63,66 @@ public async Task SavingSettings_PersistsBackgroundFeedRefreshAndSelectedInterva _data.Verify(d => d.SaveSettings(It.Is(s => s.FeedRefreshEnabled && s.FeedRefreshIntervalMinutes == 180)), Times.Once); } + + [Fact] + public void ShowsEverySetting_InCollapsibleSections_WithOnlyReadingNotesOpen() + { + var cut = Render< NoteBookmark.SharedUI.Components.Pages.Settings >(); + + string[] labels = + [ + "Theme", "Accent color", "Font size", + "Last bookmark date", "Reading notes counter", + "Favorite domains", "Blocked domains", + "API key", "Base URL", "Model name", "Summary prompt", "Search prompt", + "Background feed refresh", "Refresh interval", + "Bookmarklet code" + ]; + foreach (var label in labels) + { + cut.Markup.Should().Contain(label); + } + + var sections = cut.FindComponents(); + sections.Should().HaveCount(6); + sections.Select(s => s.Instance.Expanded) + .Should().Equal(false, true, false, false, false, false); + } + + [Fact] + public async Task IncrementingTheCounter_UpdatesTheSectionSummary() + { + var cut = Render< NoteBookmark.SharedUI.Components.Pages.Settings >(); + + await cut.InvokeAsync(() => cut.Find("#increment-counter").Click()); + + _settings.ReadingNotesCounter.Should().Be("651"); + cut.Markup.Should().Contain("Next #651"); + } + + [Fact] + public async Task SubmittingAnInvalidPrompt_DoesNotSave_AndOpensTheAiSection() + { + _settings.SummaryPrompt = "No placeholder"; + var cut = Render< NoteBookmark.SharedUI.Components.Pages.Settings >(); + + await cut.InvokeAsync(() => cut.Find("form").Submit()); + + _data.Verify(d => d.SaveSettings(It.IsAny()), Times.Never); + cut.Markup.Should().Contain("The field must contain"); + cut.FindComponents()[3].Instance.Expanded.Should().BeTrue(); + } + + [Fact] + public async Task Cancel_LeavesThePageWithoutSaving() + { + var navigation = Services.GetRequiredService(); + navigation.NavigateTo("settings"); + var cut = Render< NoteBookmark.SharedUI.Components.Pages.Settings >(); + + await cut.InvokeAsync(() => cut.Find("#cancel-settings").Click()); + + navigation.Uri.Should().Be(navigation.BaseUri); + _data.Verify(d => d.SaveSettings(It.IsAny()), Times.Never); + } } diff --git a/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsSummaryTests.cs b/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsSummaryTests.cs new file mode 100644 index 0000000..f1d82f8 --- /dev/null +++ b/src/NoteBookmark.BlazorApp.Tests/Tests/SettingsSummaryTests.cs @@ -0,0 +1,67 @@ +using NoteBookmark.SharedUI; + +namespace NoteBookmark.BlazorApp.Tests.Tests; + +public sealed class SettingsSummaryTests +{ + private static Domain.Settings EmptySettings() => new() { PartitionKey = "setting", RowKey = "setting" }; + + [Fact] + public void Summaries_HandleSettingsWithNoValues() + { + var settings = EmptySettings(); + + SettingsSummary.ReadingNotes(settings).Should().Be("Next not set · last bookmark not set"); + SettingsSummary.ContentFilters(settings).Should().Be("0 favorite · 0 blocked"); + SettingsSummary.Ai(settings).Should().Be("Provider not configured"); + SettingsSummary.Feeds(settings).Should().Be("Background refresh off"); + SettingsSummary.Appearance("System", null, null).Should().Be("System theme · Default color · Medium text"); + } + + [Fact] + public void ReadingNotes_ShowsCounterAndDateWithoutTime() + { + var settings = EmptySettings(); + settings.ReadingNotesCounter = "650"; + settings.LastBookmarkDate = "2026-11-16T08:30:00"; + + SettingsSummary.ReadingNotes(settings).Should().Be("Next #650 · last bookmark 2026-11-16"); + } + + [Fact] + public void ContentFilters_IgnoresBlankEntries() + { + var settings = EmptySettings(); + settings.FavoriteDomains = "a.com, b.com,"; + settings.BlockedDomains = " , "; + + SettingsSummary.ContentFilters(settings).Should().Be("2 favorite · 0 blocked"); + } + + [Theory] + [InlineData("https://openrouter.ai/api/v1", "mistralai/small", "openrouter.ai · mistralai/small")] + [InlineData("not a url", null, "not a url")] + [InlineData(null, "llama3", "llama3")] + public void Ai_ShowsProviderAndModel_NeverTheApiKey(string? baseUrl, string? model, string expected) + { + var settings = EmptySettings(); + settings.AiApiKey = "sk-secret"; + settings.AiBaseUrl = baseUrl; + settings.AiModelName = model; + + SettingsSummary.Ai(settings).Should().Be(expected); + } + + [Theory] + [InlineData(30, "Refresh every 30 min")] + [InlineData(180, "Refresh every 3 h")] + [InlineData(0, "Refresh every 1 h")] + public void Feeds_DescribesTheInterval(int minutes, string expected) + { + var settings = EmptySettings(); + settings.FeedRefreshEnabled = true; + settings.FeedRefreshIntervalMinutes = minutes; + + SettingsSummary.Feeds(settings).Should().Be(expected); + } +} diff --git a/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor b/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor index d07c0a8..a3f3e2e 100644 --- a/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor +++ b/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor @@ -7,149 +7,223 @@ @inject NavigationManager Navigation @inject IJSRuntime JSRuntime +Settings + - -

Settings

- -
- - - - - - - - - - - @context - - - - - -
+ @bind-OfficeColor="@OfficeColor" + StorageName="theme" /> -
+
+

Settings

-@if( settings != null) -{ -
- + @if (settings is null) + { + + } + else + { + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - AI Provider Configuration - - Any OpenAI-compatible provider works. The Base URL must end at /v1 (without /chat/completions). - See the AI provider setup guide for OpenCode, OpenRouter and Ollama examples. - - - - - - - - - - - - - - - - - Feeds - - - - - - - - - - Save - - - - - - Browser Bookmarklet - - Drag the button below to your browser's bookmarks bar to quickly save articles from any webpage directly into NoteBookmark: - - - - - - + Add to NoteBookmark - - - - Tip: If dragging is not supported, right-click the button, copy the link, and paste it as the URL of a new browser bookmark. - - -
-} + + + + + + + +
+
+
+ +
+
+ + + + + @color + + + +
+
+ +
+
+

Theme and color apply right away on this device. Font size is saved with the other settings.

+
+
+
+ + + + + + +
+
+
+ +
+
+
+ +
+ +
+
+

The counter is the number given to your next reading notes.

+
+
+
+ + + + + + +
+
+ +
+

Comma separated. When set, AI search only looks at these domains.

+
+ +
+

Comma separated. AI search skips these domains when no favorite domains are set.

+
+
+
+ + + + + + +
+
Provider
+

+ Any OpenAI-compatible provider works. The Base URL must end at /v1 (without /chat/completions). + See the AI provider setup guide for OpenCode, OpenRouter and Ollama examples. +

+
+ +
+
+
+ +
+
+ +
+
+
Prompts
+
+ + +
+

Must contain {content}, replaced by the article text.

+
+ + +
+

Must contain {topic}, replaced at search time.

+
+
+
+ + + + + + +
+
+ +
+
+
+ +
+
+

When on, feeds are checked for new posts in the background at this interval.

+
+
+
+ + + + + + +
+

Drag the button to your browser's bookmarks bar to save the page you're reading straight into NoteBookmark.

+ +

Tip: If dragging is not supported, right-click the button, copy the link, and paste it as the URL of a new browser bookmark.

+
+ +
+
+
+
+ +
+ +
+ @* Inside the sticky bar so a failed save is visible wherever the page is scrolled. *@ + + Collapsed sections keep their changes. Save applies them all. + Cancel + Save +
+ + } +
@code { @@ -158,6 +232,19 @@ private Domain.Settings? settings; + private enum Section + { + Appearance, + ReadingNotes, + ContentFilters, + Ai, + Feeds, + Bookmarklet + } + + // Reading notes is the most edited section, so it is the only one open at first. + private readonly HashSet
openSections = [Section.ReadingNotes]; + private record FontSizeOption(string Text, string Value); private record FeedRefreshIntervalOption(string Text, string Value); @@ -168,15 +255,10 @@ new("Large", "large") }; - private static readonly List feedRefreshIntervalOptions = new() - { - new("30 min", "30"), - new("1 h", "60"), - new("3 h", "180"), - new("6 h", "360"), - new("12 h", "720"), - new("24 h", "1440") - }; + private static readonly List feedRefreshIntervalOptions = + Domain.Settings.AllowedFeedRefreshIntervals + .Select(minutes => new FeedRefreshIntervalOption(SettingsSummary.FormatInterval(minutes), minutes.ToString())) + .ToList(); private string FeedRefreshIntervalValue { @@ -208,6 +290,23 @@ } } + private bool IsOpen(Section section) => openSections.Contains(section); + + private void SetOpen(Section section, bool open) + { + if (open) + { + openSections.Add(section); + } + else + { + openSections.Remove(section); + } + } + + // Only the prompts, in the AI section, are validated. Open it so the error is not hidden. + private void ShowInvalidFields() => openSections.Add(Section.Ai); + private async Task SaveSettings() { if (settings != null) @@ -227,6 +326,8 @@ } } + private void Cancel() => Navigation.NavigateTo("/"); + private void IncrementCounter() { var cnt = Convert.ToInt32(settings!.ReadingNotesCounter) + 1; diff --git a/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor.css b/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor.css new file mode 100644 index 0000000..abe6db1 --- /dev/null +++ b/src/NoteBookmark.SharedUI/Components/Pages/Settings.razor.css @@ -0,0 +1,128 @@ +.settings-page { + max-width: 960px; + margin: 0 auto; +} + +.settings-page ::deep fluent-accordion-item::part(heading) { + min-height: 56px; +} + +/* Let long summaries shrink and end with an ellipsis instead of widening the heading. */ +.settings-page ::deep fluent-accordion-item::part(button), +.settings-page ::deep fluent-accordion-item::part(heading-content), +.settings-page ::deep [slot="heading"] { + min-width: 0; + max-width: 100%; + overflow: hidden; +} + +.settings-fields { + display: flex; + flex-direction: column; + gap: 4px; + max-width: 720px; + padding: 4px 0 12px 30px; +} + +.settings-fields > .settings-field { + margin-top: 8px; +} + +.settings-field { + min-width: 0; +} + +.settings-row { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr)); + gap: 12px 16px; + margin-top: 8px; +} + +.settings-row-wide { + grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)); +} + +.settings-inline { + display: flex; + align-items: flex-end; + gap: 8px; +} + +.settings-inline .settings-field { + flex: 1; +} + +.settings-hint { + margin: 2px 0 6px; + font-size: 0.8rem; + color: var(--neutral-foreground-hint); +} + +.settings-subhead { + margin: 16px 0 0; + font-size: 0.8rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.04em; + color: var(--neutral-foreground-hint); +} + +.settings-subhead:first-child { + margin-top: 0; +} + +.settings-bookmarklet { + display: inline-flex; + align-items: center; + gap: 6px; + margin: 6px 0; + padding: 6px 16px; + border-radius: 4px; + background: var(--accent-fill-rest); + color: var(--foreground-on-accent-rest); + font-weight: 600; + text-decoration: none; + cursor: move; +} + +.settings-savebar { + position: sticky; + bottom: 0; + z-index: 2; + display: flex; + flex-wrap: wrap; + align-items: center; + justify-content: flex-end; + gap: 12px; + margin-top: 20px; + padding: 12px 0; + border-top: 1px solid var(--neutral-stroke-divider-rest); + background: var(--neutral-fill-layer-rest); +} + +.settings-savebar ::deep .validation-errors { + flex-basis: 100%; + margin: 0; + padding-left: 20px; +} + +.settings-savebar > span { + margin-right: auto; + font-size: 0.8rem; + color: var(--neutral-foreground-hint); +} + +@media (max-width: 600px) { + .settings-fields { + padding-left: 0; + } + + .settings-savebar > span { + display: none; + } + + .settings-savebar ::deep fluent-button { + flex: 1; + } +} diff --git a/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor b/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor new file mode 100644 index 0000000..19e1f10 --- /dev/null +++ b/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor @@ -0,0 +1,15 @@ +@* Heading of a collapsible Settings section: icon, title and a summary of the current values. *@ + +
+ +
+

@Title

+

@Summary

+
+
+ +@code { + [Parameter, EditorRequired] public Icon Icon { get; set; } = default!; + [Parameter, EditorRequired] public string Title { get; set; } = ""; + [Parameter, EditorRequired] public string Summary { get; set; } = ""; +} diff --git a/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor.css b/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor.css new file mode 100644 index 0000000..79f69f5 --- /dev/null +++ b/src/NoteBookmark.SharedUI/Components/Shared/SettingsSectionHeading.razor.css @@ -0,0 +1,25 @@ +.settings-heading { + display: flex; + align-items: center; + gap: 10px; + min-width: 0; + padding: 6px 0; +} + +.settings-heading-text { + min-width: 0; +} + +.settings-heading h4 { + margin: 0; + font-size: 1rem; +} + +.settings-heading p { + margin: 0; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; + font-size: 0.8rem; + color: var(--neutral-foreground-hint); +} diff --git a/src/NoteBookmark.SharedUI/SettingsSummary.cs b/src/NoteBookmark.SharedUI/SettingsSummary.cs new file mode 100644 index 0000000..f8fa37d --- /dev/null +++ b/src/NoteBookmark.SharedUI/SettingsSummary.cs @@ -0,0 +1,68 @@ +using System.Globalization; +using NoteBookmark.Domain; + +namespace NoteBookmark.SharedUI; + +/// +/// One-line summaries of the current values, shown in the headers of the Settings page sections. +/// Every value can be missing, and the AI API key is never included. +/// +public static class SettingsSummary +{ + private const string NotSet = "not set"; + + public static string Appearance(string theme, string? color, string? fontSize) => + $"{theme} theme · {color ?? "Default"} color · {Capitalize(fontSize ?? FontSizeHelper.DefaultFontSize)} text"; + + public static string ReadingNotes(Settings settings) + { + var counter = string.IsNullOrWhiteSpace(settings.ReadingNotesCounter) + ? NotSet + : $"#{settings.ReadingNotesCounter}"; + + var date = settings.LastBookmarkDate?.Split('T')[0]; + + return $"Next {counter} · last bookmark {(string.IsNullOrWhiteSpace(date) ? NotSet : date)}"; + } + + public static string ContentFilters(Settings settings) => + $"{CountDomains(settings.FavoriteDomains)} favorite · {CountDomains(settings.BlockedDomains)} blocked"; + + public static string Ai(Settings settings) + { + var provider = Uri.TryCreate(settings.AiBaseUrl, UriKind.Absolute, out var uri) && uri.Host.Length > 0 + ? uri.Host + : settings.AiBaseUrl; + + var parts = new[] { provider, settings.AiModelName } + .Where(part => !string.IsNullOrWhiteSpace(part)) + .ToArray(); + + return parts.Length == 0 ? "Provider not configured" : string.Join(" · ", parts); + } + + public static string Feeds(Settings settings) + { + if (!settings.FeedRefreshEnabled) + { + return "Background refresh off"; + } + + var minutes = settings.FeedRefreshIntervalMinutes > 0 + ? settings.FeedRefreshIntervalMinutes + : Settings.DefaultFeedRefreshIntervalMinutes; + + return $"Refresh every {FormatInterval(minutes)}"; + } + + public static string FormatInterval(int minutes) => + minutes >= 60 && minutes % 60 == 0 + ? $"{minutes / 60} h" + : $"{minutes} min"; + + private static int CountDomains(string? domains) => + domains?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Length ?? 0; + + private static string Capitalize(string value) => + value.Length == 0 ? value : char.ToUpper(value[0], CultureInfo.InvariantCulture) + value[1..]; +}