feat(ui/quiz): implement real-time global chapter quiz generation, submit results to database, and display dynamic statistics on dashboard

This commit is contained in:
2026-05-25 10:23:38 +02:00
parent 1c6ee82d01
commit f8d1ceabd3
18 changed files with 1198 additions and 45 deletions
@@ -11,4 +11,5 @@ public interface IIdentityService
Task<Result> LogoutAsync();
Task<Result<UserProfileDto>> GetProfileAsync();
Task<Result> RefreshTokenAsync();
void ClearCache();
}
@@ -0,0 +1,10 @@
using FluentResults;
using NexusReader.Application.Abstractions.Messaging;
namespace NexusReader.Application.Commands.Quiz;
public record SubmitQuizResultCommand(
string UserId,
string Topic,
int Score,
int TotalQuestions) : ICommand;
@@ -0,0 +1,44 @@
using FluentResults;
using Microsoft.EntityFrameworkCore;
using NexusReader.Application.Abstractions.Messaging;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
namespace NexusReader.Application.Commands.Quiz;
public sealed class SubmitQuizResultCommandHandler : ICommandHandler<SubmitQuizResultCommand>
{
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
public SubmitQuizResultCommandHandler(IDbContextFactory<AppDbContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public async Task<Result> Handle(SubmitQuizResultCommand request, CancellationToken cancellationToken)
{
using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var user = await context.Users.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
if (user == null)
{
return Result.Fail("User not found.");
}
var quizResult = new QuizResult
{
Id = Guid.NewGuid(),
UserId = request.UserId,
TenantId = string.IsNullOrEmpty(user.TenantId) ? "global" : user.TenantId,
Topic = request.Topic,
Score = request.Score,
TotalQuestions = request.TotalQuestions,
CompletedDate = DateTime.UtcNow
};
context.QuizResults.Add(quizResult);
await context.SaveChangesAsync(cancellationToken);
return Result.Ok();
}
}
@@ -5,6 +5,7 @@ namespace NexusReader.Application.DTOs.User;
public record UserProfileDto
{
public string Email { get; init; } = string.Empty;
public string UserId { get; init; } = string.Empty;
public int AITokensUsed { get; init; }
public Guid TenantId { get; init; }
@@ -15,11 +16,11 @@ public record UserProfileDto
public int AverageQuizScore { get; init; }
/// <summary>
/// Summary of the last read book.
/// </summary>
public string? DisplayName { get; init; }
public int BooksReadCount { get; init; }
public int ConceptsMappedCount { get; init; }
public LastReadBookDto? LastReadBook { get; init; }
public IReadOnlyList<QuizResultDto> RecentQuizzes { get; init; } = Array.Empty<QuizResultDto>();
public string[] Roles { get; init; } = Array.Empty<string>();
// Helper properties for UI compatibility
@@ -40,3 +41,13 @@ public record LastReadBookDto
public string? Description { get; init; }
public bool IsReadyForReading { get; init; }
}
public record QuizResultDto
{
public Guid Id { get; init; }
public string Topic { get; init; } = string.Empty;
public int Score { get; init; }
public int TotalQuestions { get; init; }
public double Percentage { get; init; }
public DateTime CompletedDate { get; init; }
}
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace NexusReader.Application.Queries.Graph;
@@ -7,7 +8,9 @@ public record GraphNodeDto(
[property: JsonPropertyName("label")] string Label,
[property: JsonPropertyName("group")] string Group,
[property: JsonPropertyName("description")] string? Description = null,
[property: JsonPropertyName("type")] string? Type = null
[property: JsonPropertyName("type")] string? Type = null,
[property: JsonPropertyName("summary")] string? Summary = null,
[property: JsonPropertyName("key_terms")] List<string>? KeyTerms = null
);
public record GraphLinkDto(
@@ -23,6 +23,7 @@ public class GetUserProfileQueryHandler : IRequestHandler<GetUserProfileQuery, R
.Select(u => new UserProfileDto
{
Email = u.Email ?? string.Empty,
UserId = u.Id,
AITokensUsed = u.AITokensUsed,
TenantId = u.TenantId != null && u.TenantId.Length == 36 ? new Guid(u.TenantId) : Guid.Empty,
Plan = u.SubscriptionPlan != null ? new SubscriptionPlanDto
@@ -35,6 +36,9 @@ public class GetUserProfileQueryHandler : IRequestHandler<GetUserProfileQuery, R
AverageQuizScore = u.QuizResults.Any(q => q.TotalQuestions > 0)
? (int)u.QuizResults.Where(q => q.TotalQuestions > 0).Average(q => (double)q.Score / q.TotalQuestions * 100)
: 0,
DisplayName = u.DisplayName,
BooksReadCount = u.Ebooks.Count(),
ConceptsMappedCount = dbContext.KnowledgeUnits.Count(k => k.TenantId == u.TenantId),
LastReadBook = u.Ebooks.OrderByDescending(e => e.LastReadDate).Select(e => new LastReadBookDto
{
Id = e.Id,
@@ -51,6 +55,15 @@ public class GetUserProfileQueryHandler : IRequestHandler<GetUserProfileQuery, R
Description = e.Description,
IsReadyForReading = e.IsReadyForReading
}).FirstOrDefault(),
RecentQuizzes = u.QuizResults.OrderByDescending(q => q.CompletedDate).Take(5).Select(q => new QuizResultDto
{
Id = q.Id,
Topic = q.Topic,
Score = q.Score,
TotalQuestions = q.TotalQuestions,
Percentage = q.Percentage,
CompletedDate = q.CompletedDate
}).ToList(),
Roles = dbContext.UserRoles
.Where(ur => ur.UserId == u.Id)
.Join(dbContext.Roles, ur => ur.RoleId, r => r.Id, (ur, r) => r.Name!)
@@ -33,7 +33,7 @@ public class KnowledgeService : IKnowledgeService
private readonly ILogger<KnowledgeService> _logger;
private readonly QdrantClient _qdrantClient;
private readonly IDriver _neo4jDriver;
private const string PromptVersion = "1.6";
private const string PromptVersion = "1.7";
private static readonly ConcurrentDictionary<string, Lazy<Task<Result<KnowledgePacket>>>> _activeRequests = new();
public KnowledgeService(
@@ -16,18 +16,31 @@ public static class PromptRegistry
"}.";
public const string GraphExtractionPrompt =
"You are an expert at information architecture. Extract a highly strategic, clean, and educational knowledge graph from the provided technical text to act as a clear structural roadmap, avoiding clutter or hyper-connected noise hubs. " +
"**LANGUAGE CRITICAL**: Detect the language of the provided text. The 'label' and 'description' fields MUST be generated in the EXACT SAME LANGUAGE as the source text. Do NOT translate them to English. " +
"The input text consists of several paragraphs, each starting with its unique block ID in the format '[ID: seg-X]'. " +
"Extract three distinct types of nodes based on strict hierarchical validation: " +
"1. Concept Nodes (group: 'concept'): Extract major global architectural pillars discussed. Max 6 per segment. Labels must be 1-3 words max. " +
"2. Bridge Nodes (group: 'bridge'): If the text directly compares a legacy paradigm (e.g., Desktop/WPF) to a modern framework alternative (.NET 10/Blazor), extract them as paired concepts to visually bridge the structural evolution. " +
"3. Block Nodes (group: 'current'): Create a node ONLY for significant structural landmarks in the text (e.g., major headings). Do NOT connect every concept to every individual paragraph wrapper. Connect concepts only to the main section block where they are anchored. " +
"CRITICAL NOISE SUPPRESSION: Absolutely forbid creating separate nodes for individual configuration files, files names, simple classes, servers, or methods (e.g., 'appsettings.json', 'Kestrel', 'Thread.Sleep', 'OnInitializedAsync'). These low-level details MUST be collapsed and described only within the 'description' field of their parent concept node. " +
"CRITICAL: Code blocks must be completely ignored as separate nodes; represent them only as contextual attributes within descriptions. " +
"Limit topology connections to a MAXIMUM of 10 highly relevant links total per segment. " +
"System keys configuration: 'group' must be strictly 'concept', 'bridge', 'current', 'rule', 'definition', 'table', or 'section'. " +
"Return ONLY minified JSON. Schema: { \"graph\": { \"nodes\": [ { \"id\": \"string\", \"label\": \"string\", \"group\": \"concept|bridge|current\", \"description\": \"string\" } ], \"links\": [ { \"source\": \"string\", \"target\": \"string\", \"type\": \"maps_to|contains|relates_to\" } ] } }";
"You are a strict Minimalist Information Architect. Your sole job is to build a high-level, sparse linear backbone for a textbook chapter. " +
"**LANGUAGE CRITICAL**: Detect the language of the provided text. The 'label', 'summary', and 'key_terms' fields MUST be in the EXACT SAME LANGUAGE as the source text. " +
"The input text consists of sections starting with block IDs (e.g., '[ID: seg-4]'). " +
"CRITICAL TOPOLOGY RULES (ZERO TOLERANCE FOR CLUTTER): " +
"1. HARD NODE LIMIT: You are strictly forbidden from extracting more than 4 to 5 nodes IN TOTAL for the entire text. If there are more sections, select ONLY the 4-5 absolute most critical, high-level structural pillars. " +
"2. NO CONCEPT CLOUDS: Do NOT create nodes for individual technologies, files, terms, or phrases (e.g., 'Kestrel', 'appsettings.json', 'DI', 'Blazor Server' must NEVER be nodes). They must ONLY exist as text strings inside the 'key_terms' array of a major node. " +
"3. LINEAR SPINE PATTERN: Nodes must form a clear, clean path or simple tree representing the chronological reading journey (e.g., Node 1 -> Node 2 -> Node 3). Do NOT create complex web loops or interconnect every node. Limit total links in the entire JSON to maximum 4 or 5 links. " +
"4. NODE DATA STRUCTURE: " +
" - 'id': must be the exact block ID (e.g., 'seg-16'). " +
" - 'label': clear technical title (Max 3 words, e.g., 'Blazor Hosting Models'). " +
" - 'group': strictly either 'bridge' (if it compares legacy vs modern) or 'concept' (for standalone core pillars). " +
" - 'summary': exact 2-sentence distillation for the Contextual Panel. " +
" - 'key_terms': array of max 5 short strings representing the micro-concepts hidden inside this section. " +
"System keys configuration: All JSON keys ('nodes', 'links', 'id', 'label', 'group', 'summary', 'key_terms', 'source', 'target', 'type') must remain strictly in English. " +
"Return ONLY minified JSON. Schema: " +
"{ " +
" \"graph\": { " +
" \"nodes\": [ " +
" { \"id\": \"seg-X\", \"label\": \"string\", \"group\": \"concept|bridge\", \"summary\": \"string\", \"key_terms\": [ \"string\" ] } " +
" ], " +
" \"links\": [ " +
" { \"source\": \"seg-X\", \"target\": \"seg-Y\", \"type\": \"maps_to|contains\" } " +
" ] " +
" } " +
"}";
public const string SummaryAndQuizPrompt =
"You are an expert educator. Provide a concise summary of the text and generate a challenging quiz (3-5 questions). " +
@@ -2,9 +2,14 @@
@using NexusReader.Application.Queries.Quiz
@using NexusReader.Application.Commands.Quiz
@using NexusReader.Application.Abstractions.Services
@using NexusReader.UI.Shared.Components.Atoms
@using NexusReader.UI.Shared.Services
@inject IMediator Mediator
@inject IPlatformService PlatformService
@inject IQuizStateService QuizService
@inject IIdentityService IdentityService
@inject IKnowledgeGraphService GraphService
@inject KnowledgeCoordinator Coordinator
<div class="knowledge-check">
<div class="quiz-header">
@@ -12,10 +17,33 @@
<button class="expand-btn">⌵</button>
</div>
@if (QuizService.IsHydrating)
@if (QuizService.IsHydrating || _isGenerating)
{
<div class="loading-state shimmer">Skanowanie wiedzy przez AI...</div>
}
else if (_isSubmitted)
{
<div class="submitted-container">
<div class="success-icon-wrapper">
<NexusIcon Name="check" Size="48" Class="success-glow" />
</div>
<h3 class="submitted-title">Gratulacje!</h3>
<p class="submitted-text">Sprawdzian zakończony pomyślnie. Twój wynik został zapisany w bazie danych.</p>
<div class="score-card">
<div class="score-main">
<span class="score-num">@_score</span>
<span class="score-divider">/</span>
<span class="score-total">@_totalQuestions</span>
</div>
<div class="score-percent">@((int)_percentage)% poprawnych odpowiedzi</div>
</div>
<button class="reset-quiz-btn" @onclick="CloseQuiz">
<span>ZAKOŃCZ</span>
</button>
</div>
}
else if (QuizService.CurrentQuiz != null)
{
<div class="quiz-body">
@@ -41,17 +69,45 @@
}
<div class="quiz-footer">
<button class="submit-btn" disabled="@(!AllQuestionsAnswered())">Wyślij</button>
<button class="submit-btn" disabled="@(!AllQuestionsAnswered() || _isSubmitting)" @onclick="SubmitQuizAsync">
@if (_isSubmitting)
{
<span>Zapisywanie...</span>
}
else
{
<span>Wyślij</span>
}
</button>
</div>
</div>
}
</div>
else
{
<div class="empty-quiz-state">
<div class="empty-icon-wrapper">
<NexusIcon Name="robot" Size="48" Class="neon-glow" />
</div>
<h3 class="empty-title">Brak Aktywnego Quizu</h3>
<p class="empty-text">Generuj spersonalizowany sprawdzian wiedzy na podstawie bieżącego rozdziału książki.</p>
<button class="generate-quiz-btn" @onclick="GenerateChapterQuizAsync" disabled="@(string.IsNullOrWhiteSpace(Coordinator.CurrentFullPageContent))">
<span>GENERUJ QUIZ DLA ROZDZIAŁU</span>
</button>
</div>
}
</div>
@code {
[Parameter] public string ContextBlockId { get; set; } = string.Empty;
private Dictionary<QuizQuestionDto, (int SelectedIndex, bool IsCorrect)> _states = new();
private bool _isSubmitting = false;
private bool _isSubmitted = false;
private bool _isGenerating = false;
private int _score = 0;
private int _totalQuestions = 0;
private double _percentage = 0.0;
protected override void OnInitialized()
{
@@ -65,6 +121,24 @@
QuizService.OnQuizUpdated -= HandleUpdate;
}
private async Task GenerateChapterQuizAsync()
{
if (_isGenerating || string.IsNullOrWhiteSpace(Coordinator.CurrentFullPageContent)) return;
_isGenerating = true;
StateHasChanged();
try
{
await Coordinator.RequestSummaryAndQuizAsync(Coordinator.CurrentFullPageContent);
}
finally
{
_isGenerating = false;
StateHasChanged();
}
}
private async Task SelectOptionAsync(QuizQuestionDto question, int index)
{
if (_states.ContainsKey(question)) return;
@@ -90,6 +164,67 @@
return QuizService.CurrentQuiz != null && _states.Count == QuizService.CurrentQuiz.Questions.Count;
}
private async Task SubmitQuizAsync()
{
if (QuizService.CurrentQuiz == null || !AllQuestionsAnswered() || _isSubmitting) return;
_isSubmitting = true;
StateHasChanged();
try
{
_score = _states.Values.Count(s => s.IsCorrect);
_totalQuestions = QuizService.CurrentQuiz.Questions.Count;
_percentage = _totalQuestions > 0 ? ((double)_score / _totalQuestions) * 100 : 0.0;
string topic = "Quiz wiedzy";
var graph = GraphService.CurrentGraphData;
if (graph != null && !string.IsNullOrEmpty(QuizService.CurrentQuizBlockId))
{
var node = graph.Nodes.FirstOrDefault(n => n.Id == QuizService.CurrentQuizBlockId);
if (node != null && !string.IsNullOrEmpty(node.Label))
{
topic = $"Test: {node.Label}";
}
}
var profileResult = await IdentityService.GetProfileAsync();
if (profileResult.IsSuccess && profileResult.Value != null)
{
var userId = profileResult.Value.UserId;
var cmd = new SubmitQuizResultCommand(userId, topic, _score, _totalQuestions);
var result = await Mediator.Send(cmd);
if (result.IsSuccess)
{
IdentityService.ClearCache();
_isSubmitted = true;
await PlatformService.VibrateSuccessAsync();
}
else
{
await PlatformService.VibrateErrorAsync();
}
}
}
catch
{
await PlatformService.VibrateErrorAsync();
}
finally
{
_isSubmitting = false;
StateHasChanged();
}
}
private void CloseQuiz()
{
_isSubmitted = false;
_states.Clear();
QuizService.SetQuiz(null, null);
}
private string GetBlockClass(QuizQuestionDto question)
{
@@ -121,3 +121,217 @@
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
.option-revealed-correct {
border-color: #00ff99 !important;
background: rgba(0, 255, 153, 0.08) !important;
box-shadow: 0 0 8px rgba(0, 255, 153, 0.15);
}
.option-faded {
opacity: 0.45;
}
.submitted-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 2rem 1rem;
animation: fadeIn 0.4s ease-out;
}
.success-icon-wrapper {
background: rgba(0, 255, 153, 0.1);
border: 1px solid rgba(0, 255, 153, 0.3);
border-radius: 50%;
width: 80px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 1.5rem;
box-shadow: 0 0 20px rgba(0, 255, 153, 0.15);
}
.success-glow {
color: var(--nexus-neon, #00ff99);
filter: drop-shadow(0 0 8px var(--nexus-neon, #00ff99));
}
.submitted-title {
font-size: 1.5rem;
font-weight: 700;
color: #fff;
margin-bottom: 0.5rem;
letter-spacing: -0.5px;
}
.submitted-text {
font-size: 0.9rem;
color: #888;
margin-bottom: 2rem;
line-height: 1.5;
}
.score-card {
background: rgba(255, 255, 255, 0.02);
border: 1px solid rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 1.5rem 2.5rem;
margin-bottom: 2rem;
display: flex;
flex-direction: column;
align-items: center;
backdrop-filter: blur(10px);
}
.score-main {
display: flex;
align-items: baseline;
gap: 0.2rem;
margin-bottom: 0.5rem;
}
.score-num {
font-size: 3rem;
font-weight: 800;
color: var(--nexus-neon, #00ff99);
line-height: 1;
text-shadow: 0 0 15px rgba(0, 255, 153, 0.3);
}
.score-divider {
font-size: 1.8rem;
color: #444;
}
.score-total {
font-size: 1.8rem;
font-weight: 600;
color: #fff;
}
.score-percent {
font-size: 0.85rem;
color: #666;
text-transform: uppercase;
letter-spacing: 1px;
}
.reset-quiz-btn {
padding: 0.8rem 3rem;
background: transparent;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 30px;
color: #fff;
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
letter-spacing: 0.5px;
}
.reset-quiz-btn:hover {
background: rgba(255, 255, 255, 0.05);
border-color: #fff;
box-shadow: 0 0 15px rgba(255, 255, 255, 0.1);
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.empty-quiz-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 2.5rem 1rem;
animation: fadeIn 0.4s ease-out;
}
.empty-icon-wrapper {
background: rgba(0, 255, 153, 0.03);
border: 1px solid rgba(0, 255, 153, 0.15);
border-radius: 50%;
width: 80px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 1.5rem;
box-shadow: 0 0 30px rgba(0, 255, 153, 0.05);
transition: all 0.3s ease;
}
.empty-quiz-state:hover .empty-icon-wrapper {
background: rgba(0, 255, 153, 0.08);
border-color: rgba(0, 255, 153, 0.4);
box-shadow: 0 0 35px rgba(0, 255, 153, 0.15);
transform: scale(1.05);
}
.neon-glow {
color: var(--nexus-neon, #00ff99);
filter: drop-shadow(0 0 6px var(--nexus-neon, #00ff99));
}
.empty-title {
font-size: 1.3rem;
font-weight: 700;
color: #fff;
margin-bottom: 0.5rem;
letter-spacing: -0.3px;
}
.empty-text {
font-size: 0.9rem;
color: #888;
margin-bottom: 2rem;
line-height: 1.5;
max-width: 280px;
}
.generate-quiz-btn {
padding: 0.85rem 2rem;
background: rgba(0, 255, 153, 0.08);
border: 1px solid var(--nexus-neon, #00ff99);
border-radius: 30px;
color: var(--nexus-neon, #00ff99);
font-size: 0.85rem;
font-weight: 700;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
letter-spacing: 0.8px;
text-shadow: 0 0 10px rgba(0, 255, 153, 0.3);
box-shadow: 0 0 15px rgba(0, 255, 153, 0.1);
}
.generate-quiz-btn:not(:disabled):hover {
background: var(--nexus-neon, #00ff99);
color: #000;
box-shadow: 0 0 25px rgba(0, 255, 153, 0.4);
transform: translateY(-2px);
text-shadow: none;
}
.generate-quiz-btn:disabled {
opacity: 0.4;
cursor: not-allowed;
border-color: rgba(255, 255, 255, 0.15);
background: rgba(255, 255, 255, 0.02);
color: #666;
text-shadow: none;
box-shadow: none;
}
@@ -3,10 +3,13 @@
@using NexusReader.UI.Shared.Services
@using NexusReader.UI.Shared.Components.Molecules
@using NexusReader.UI.Shared.Components.Organisms
@using NexusReader.Application.Queries.Graph
@using Microsoft.Extensions.Logging
@inject IPlatformService PlatformService
@inject IFocusModeService FocusMode
@inject IQuizStateService QuizService
@inject IReaderInteractionService InteractionService
@inject IKnowledgeGraphService GraphService
@inject IJSRuntime JS
@inject IIdentityService IdentityService
@inject NavigationManager NavigationManager
@@ -41,13 +44,92 @@
<button class="close-btn">×</button>
</div>
<div class="intelligence-scroll-area">
@if (_activeTab == SidebarTab.Knowledge)
{
<div class="intelligence-scroll-area stacked-layout">
@if (!_isMobile)
{
<div class="visual-workspace">
<KnowledgeGraph />
</div>
}
<div class="contextual-intelligence-panel">
<div class="panel-header">
<NexusIcon Name="brain" Size="18" Class="neon-accent-icon" />
<span class="panel-title">Contextual Intelligence Panel</span>
</div>
<div class="panel-body">
@if (_selectedNode != null)
{
<div class="node-details">
<div class="node-header-section">
<span class="node-group-badge @(_selectedNode.Group.ToLower())">@(_selectedNode.Group.ToUpper())</span>
<h3 class="node-label">@_selectedNode.Label</h3>
</div>
@if (!string.IsNullOrEmpty(_selectedNode.Description))
{
<div class="detail-section">
<p class="node-description">@_selectedNode.Description</p>
</div>
}
@if (!string.IsNullOrEmpty(_selectedNode.Summary))
{
<div class="detail-section summary-section">
<h4 class="section-title neon-sub-header">Podsumowanie</h4>
<p class="node-summary">@_selectedNode.Summary</p>
</div>
}
@if (_selectedNode.KeyTerms != null && _selectedNode.KeyTerms.Any())
{
<div class="detail-section key-terms-section">
<h4 class="section-title neon-sub-header">Kluczowe Pojęcia</h4>
<ul class="key-terms-list">
@foreach (var term in _selectedNode.KeyTerms)
{
<li class="key-term-item">
<span class="term-bullet">•</span>
<span class="term-text">@term</span>
</li>
}
</ul>
</div>
}
</div>
}
else
{
<div class="no-node-selected">
<div class="placeholder-glow"></div>
<p class="placeholder-text">Wybierz węzeł na wykresie, aby wyświetlić szczegóły architektoniczne.</p>
</div>
}
</div>
</div>
</div>
<div class="sidebar-footer">
<button class="open-quiz-btn neon-glow-btn @(QuizService.HasNewQuiz ? "quiz-pulse-btn" : "")" @onclick="() => SetActiveTab(SidebarTab.Quiz)">
<NexusIcon Name="quiz" Size="18" />
<span>OPEN KNOWLEDGE QUIZ</span>
</button>
</div>
}
else
{
<div class="intelligence-scroll-area quiz-layout">
<div class="quiz-nav">
<button class="back-to-graph-btn" @onclick="() => SetActiveTab(SidebarTab.Knowledge)">
<NexusIcon Name="arrow-left" Size="16" />
<span>← Powrót do wykresu</span>
</button>
</div>
<KnowledgeCheck />
</div>
}
</div>
</div>
</Authorized>
@@ -67,6 +149,16 @@
</div>
@code {
private enum SidebarTab
{
Knowledge,
Quiz
}
private SidebarTab _activeTab = SidebarTab.Knowledge;
private string? _selectedNodeId;
private GraphNodeDto? _selectedNode;
private string _platformClass = "platform-desktop";
private bool _isMobile = false;
@@ -74,6 +166,10 @@
{
FocusMode.OnFocusModeChanged += HandleUpdate;
QuizService.OnQuizUpdated += HandleUpdate;
QuizService.OnQuizRequested += HandleQuizRequestedAsync;
InteractionService.OnNodeSelected += HandleNodeSelectedAsync;
GraphService.OnGraphUpdated += HandleGraphUpdatedAsync;
var context = PlatformService.GetDeviceContext();
if (context.IsSuccess)
@@ -88,7 +184,34 @@
}
}
private void SetActiveTab(SidebarTab tab)
{
_activeTab = tab;
StateHasChanged();
}
private async Task HandleQuizRequestedAsync(string blockId)
{
_activeTab = SidebarTab.Quiz;
await InvokeAsync(StateHasChanged);
}
private async Task HandleNodeSelectedAsync(string nodeId)
{
_selectedNodeId = nodeId;
if (GraphService.CurrentGraphData != null)
{
_selectedNode = GraphService.CurrentGraphData.Nodes.FirstOrDefault(n => n.Id == nodeId);
}
await InvokeAsync(StateHasChanged);
}
private async Task HandleGraphUpdatedAsync()
{
_selectedNodeId = null;
_selectedNode = null;
await InvokeAsync(StateHasChanged);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
@@ -112,5 +235,8 @@
{
FocusMode.OnFocusModeChanged -= HandleUpdate;
QuizService.OnQuizUpdated -= HandleUpdate;
QuizService.OnQuizRequested -= HandleQuizRequestedAsync;
InteractionService.OnNodeSelected -= HandleNodeSelectedAsync;
GraphService.OnGraphUpdated -= HandleGraphUpdatedAsync;
}
}
@@ -153,3 +153,318 @@ main {
50% { filter: drop-shadow(0 0 10px var(--nexus-neon)); transform: scale(1.1); }
100% { filter: drop-shadow(0 0 2px var(--nexus-neon)); transform: scale(1); }
}
/* Contextual Intelligence Panel Layout */
.stacked-layout {
display: flex;
flex-direction: column;
height: 100%;
}
.visual-workspace {
flex-shrink: 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.contextual-intelligence-panel {
flex: 1;
display: flex;
flex-direction: column;
background: rgba(13, 13, 13, 0.6);
border-top: 1px solid rgba(255, 255, 255, 0.03);
overflow: hidden;
}
.panel-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1.25rem;
background: rgba(255, 255, 255, 0.01);
border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}
.neon-accent-icon {
color: var(--nexus-neon, #00f0ff);
filter: drop-shadow(0 0 5px var(--nexus-neon, #00f0ff));
}
.panel-title {
font-family: var(--nexus-font-sans, "Outfit", sans-serif);
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.1em;
color: rgba(255, 255, 255, 0.5);
font-weight: 600;
}
.panel-body {
flex: 1;
overflow-y: auto;
padding: 1.25rem;
}
.no-node-selected {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
min-height: 150px;
text-align: center;
color: rgba(255, 255, 255, 0.35);
padding: 2rem;
}
.placeholder-glow {
width: 48px;
height: 48px;
border-radius: 50%;
background: radial-gradient(circle, rgba(0, 240, 255, 0.15) 0%, transparent 70%);
animation: glow-pulse 2s infinite ease-in-out;
margin-bottom: 1rem;
}
@keyframes glow-pulse {
0% { transform: scale(0.9); opacity: 0.5; }
50% { transform: scale(1.1); opacity: 1; }
100% { transform: scale(0.9); opacity: 0.5; }
}
.placeholder-text {
font-size: 0.85rem;
line-height: 1.4;
font-weight: 300;
}
.node-details {
display: flex;
flex-direction: column;
gap: 1.25rem;
animation: fade-in 0.3s ease-out;
}
@keyframes fade-in {
from { opacity: 0; transform: translateY(5px); }
to { opacity: 1; transform: translateY(0); }
}
.node-header-section {
display: flex;
flex-direction: column;
gap: 0.5rem;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
padding-bottom: 0.75rem;
}
.node-group-badge {
align-self: flex-start;
font-size: 0.65rem;
font-weight: 700;
padding: 0.15rem 0.5rem;
border-radius: 4px;
letter-spacing: 0.08em;
border: 1px solid transparent;
}
/* Badge specific styling matching category theme colors */
.node-group-badge.rule {
background: rgba(244, 63, 94, 0.1);
color: #f43f5e;
border-color: rgba(244, 63, 94, 0.3);
}
.node-group-badge.definition {
background: rgba(234, 179, 8, 0.1);
color: #eab308;
border-color: rgba(234, 179, 8, 0.3);
}
.node-group-badge.table {
background: rgba(168, 85, 247, 0.1);
color: #a855f7;
border-color: rgba(168, 85, 247, 0.3);
}
.node-group-badge.section {
background: rgba(59, 130, 246, 0.1);
color: #3b82f6;
border-color: rgba(59, 130, 246, 0.3);
}
.node-group-badge.bridge {
background: rgba(236, 72, 153, 0.1);
color: #ec4899;
border-color: rgba(236, 72, 153, 0.3);
}
.node-group-badge.current {
background: rgba(16, 185, 129, 0.1);
color: #10b981;
border-color: rgba(16, 185, 129, 0.3);
}
.node-group-badge.concept {
background: rgba(0, 240, 255, 0.1);
color: #00f0ff;
border-color: rgba(0, 240, 255, 0.3);
}
.node-label {
font-family: var(--nexus-font-sans, "Outfit", sans-serif);
font-size: 1.15rem;
font-weight: 600;
color: #ffffff;
margin: 0;
}
.detail-section {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.section-title {
font-size: 0.7rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.08em;
margin: 0;
color: rgba(255, 255, 255, 0.4);
}
.neon-sub-header {
border-left: 2px solid var(--nexus-neon, #00f0ff);
padding-left: 0.5rem;
text-shadow: 0 0 10px rgba(0, 240, 255, 0.2);
}
.node-description {
font-size: 0.85rem;
line-height: 1.5;
color: rgba(255, 255, 255, 0.85);
margin: 0;
}
.node-summary {
font-size: 0.82rem;
line-height: 1.5;
color: rgba(255, 255, 255, 0.75);
background: rgba(255, 255, 255, 0.02);
border-left: 2px solid rgba(255, 255, 255, 0.1);
padding: 0.5rem 0.75rem;
border-radius: 0 4px 4px 0;
margin: 0;
}
.key-terms-list {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.key-term-item {
display: flex;
align-items: flex-start;
gap: 0.5rem;
font-size: 0.82rem;
color: rgba(255, 255, 255, 0.8);
}
.term-bullet {
color: var(--nexus-neon, #00f0ff);
filter: drop-shadow(0 0 3px var(--nexus-neon, #00f0ff));
font-weight: bold;
}
.term-text {
line-height: 1.4;
}
/* Sidebar Footer & Open Quiz Button */
.sidebar-footer {
padding: 1rem 1.25rem;
background: rgba(13, 13, 13, 0.95);
border-top: 1px solid rgba(255, 255, 255, 0.05);
display: flex;
flex-direction: column;
gap: 0.5rem;
z-index: 10;
}
.open-quiz-btn {
width: 100%;
padding: 0.75rem 1rem;
border-radius: 8px;
font-family: var(--nexus-font-sans, "Outfit", sans-serif);
font-size: 0.75rem;
font-weight: 700;
letter-spacing: 0.12em;
display: flex;
align-items: center;
justify-content: center;
gap: 0.6rem;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
background: rgba(0, 240, 255, 0.03);
border: 1px solid rgba(0, 240, 255, 0.3);
color: var(--nexus-neon, #00f0ff);
box-shadow: 0 4px 15px rgba(0, 240, 255, 0.05);
}
.open-quiz-btn:hover {
background: rgba(0, 240, 255, 0.1);
border-color: var(--nexus-neon, #00f0ff);
color: #ffffff;
box-shadow: 0 0 20px rgba(0, 240, 255, 0.25);
transform: translateY(-2px);
}
.open-quiz-btn:active {
transform: translateY(0);
}
.quiz-pulse-btn {
animation: quiz-pulse-glow 2s infinite ease-in-out;
}
@keyframes quiz-pulse-glow {
0% { border-color: rgba(0, 240, 255, 0.3); box-shadow: 0 0 5px rgba(0, 240, 255, 0.1); }
50% { border-color: var(--nexus-neon, #00f0ff); box-shadow: 0 0 25px rgba(0, 240, 255, 0.3); }
100% { border-color: rgba(0, 240, 255, 0.3); box-shadow: 0 0 5px rgba(0, 240, 255, 0.1); }
}
/* Quiz Navigation Header */
.quiz-layout {
display: flex;
flex-direction: column;
}
.quiz-nav {
padding: 0.75rem 1.25rem;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
background: rgba(255, 255, 255, 0.01);
}
.back-to-graph-btn {
background: none;
border: none;
color: rgba(255, 255, 255, 0.5);
font-size: 0.8rem;
font-weight: 500;
cursor: pointer;
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.25rem 0.5rem;
border-radius: 4px;
transition: all 0.2s;
}
.back-to-graph-btn:hover {
color: var(--nexus-neon, #00f0ff);
background: rgba(255, 255, 255, 0.02);
}
+60 -16
View File
@@ -6,6 +6,8 @@
@inject IIdentityService IdentityService
@inject NavigationManager NavigationManager
@attribute [Authorize]
@implements IDisposable
<PageTitle>Dashboard | Nexus Reader</PageTitle>
@@ -18,20 +20,20 @@
<img src="https://api.dicebear.com/7.x/bottts/svg?seed=Nexus" alt="Profile" class="profile-img" />
<div class="avatar-glow"></div>
</div>
<h1 class="username">[User_Explorer1988]</h1>
<h1 class="username">@(string.IsNullOrEmpty(_profile?.DisplayName) ? (_profile?.Email.Split('@')[0] ?? "Użytkownik") : _profile.DisplayName)</h1>
<div class="status-pills">
<div class="status-pill">
<span class="pill-label">Books Read:</span>
<span class="pill-value">12</span>
<span class="pill-label">Książki:</span>
<span class="pill-value">@(_profile?.BooksReadCount ?? 0)</span>
</div>
<div class="status-pill">
<span class="pill-label">Concepts Mapped:</span>
<span class="pill-value">450</span>
<span class="pill-label">Pojęcia:</span>
<span class="pill-value">@(_profile?.ConceptsMappedCount ?? 0)</span>
</div>
<div class="status-pill">
<span class="pill-label">Quiz Mastery:</span>
<span class="pill-value">88%</span>
<span class="pill-label">Średni Wynik:</span>
<span class="pill-value">@(_profile?.AverageQuizScore ?? 0)%</span>
</div>
</div>
</div>
@@ -39,7 +41,7 @@
<!-- Main Content Area -->
<main class="dashboard-content">
<h2 class="section-title">Witaj, @(_profile?.Email.Split('@')[0] ?? "Użytkowniku")</h2>
<h2 class="section-title">Witaj, @(string.IsNullOrEmpty(_profile?.DisplayName) ? (_profile?.Email.Split('@')[0] ?? "Użytkowniku") : _profile.DisplayName)</h2>
<div class="main-grid">
<!-- Current Reading Card -->
@@ -49,7 +51,7 @@
<!-- Knowledge Integration -->
<section class="integration-card glass-panel">
<div class="panel-header">
<h4>Knowledge Integration Progress</h4>
<h4>Integracja Wiedzy</h4>
<NexusIcon Name="arrow-right" Size="16" />
</div>
<div class="graph-placeholder">
@@ -64,19 +66,36 @@
<!-- Quiz Summary -->
<section class="quiz-card glass-panel">
<div class="panel-header">
<h4>Quiz Summary: Key Thinkers</h4>
<h4>Rozwiązane Quizy</h4>
<NexusIcon Name="arrow-right" Size="16" />
</div>
<div class="quiz-preview">
<p class="question">Który artysta namalował 'Ostatnią Wieczerzę'?</p>
<div class="quiz-options">
<div class="quiz-option active">
<span class="option-letter">A)</span> Michal Anioł
@if (_profile?.RecentQuizzes != null && _profile.RecentQuizzes.Any())
{
<div class="quiz-history-list">
@foreach (var quiz in _profile.RecentQuizzes)
{
<div class="quiz-history-item">
<div class="quiz-item-header">
<span class="quiz-topic">@quiz.Topic</span>
<span class="quiz-score badge @(quiz.Percentage >= 80 ? "badge-success" : quiz.Percentage >= 50 ? "badge-warning" : "badge-danger")">
@quiz.Score / @quiz.TotalQuestions (@((int)quiz.Percentage)%)
</span>
</div>
<div class="quiz-option">
<span class="option-letter">B)</span> Leonardo da Vinci
<div class="quiz-item-meta">
<span class="quiz-date">@quiz.CompletedDate.ToString("g")</span>
</div>
</div>
}
</div>
}
else
{
<div class="empty-quiz-state">
<p class="question">Brak rozwiązanych quizów</p>
<p class="sub-text">Rozwiązuj quizy w trakcie czytania książek, aby śledzić swoje postępy.</p>
</div>
}
</div>
</section>
</div>
@@ -88,11 +107,36 @@
private UserProfileDto? _profile;
protected override async Task OnInitializedAsync()
{
IdentityService.OnStateInvalidated += HandleStateInvalidatedAsync;
await LoadProfileAsync();
}
private async Task LoadProfileAsync()
{
var result = await IdentityService.GetProfileAsync();
if (result.IsSuccess)
{
_profile = result.Value;
}
else
{
_profile = null;
}
StateHasChanged();
}
private async Task HandleStateInvalidatedAsync()
{
await InvokeAsync(async () =>
{
await LoadProfileAsync();
});
}
public void Dispose()
{
IdentityService.OnStateInvalidated -= HandleStateInvalidatedAsync;
}
}
@@ -404,3 +404,79 @@
grid-template-columns: 1fr;
}
}
/* --- Quiz History Styling --- */
.quiz-history-list {
display: flex;
flex-direction: column;
gap: 1rem;
}
.quiz-history-item {
background: rgba(255, 255, 255, 0.02);
border: 1px solid rgba(255, 255, 255, 0.05);
border-radius: 12px;
padding: 1rem;
transition: all 0.2s ease;
}
.quiz-history-item:hover {
background: rgba(255, 255, 255, 0.04);
border-color: rgba(255, 255, 255, 0.1);
}
.quiz-item-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
margin-bottom: 0.5rem;
}
.quiz-topic {
font-size: 0.95rem;
font-weight: 500;
color: #ffffff;
}
.quiz-item-meta {
display: flex;
font-size: 0.75rem;
color: #666666;
}
.badge {
padding: 0.25rem 0.5rem;
border-radius: 6px;
font-size: 0.75rem;
font-weight: 600;
}
.badge-success {
background: rgba(0, 255, 153, 0.1);
color: var(--nexus-neon);
border: 1px solid rgba(0, 255, 153, 0.3);
}
.badge-warning {
background: rgba(255, 170, 0, 0.1);
color: #ffa800;
border: 1px solid rgba(255, 170, 0, 0.3);
}
.badge-danger {
background: rgba(255, 50, 50, 0.1);
color: #ff3232;
border: 1px solid rgba(255, 50, 50, 0.3);
}
.empty-quiz-state {
text-align: center;
padding: 2rem 1rem;
}
.empty-quiz-state .sub-text {
font-size: 0.8rem;
color: #666666;
margin-top: 0.5rem;
}
@@ -249,6 +249,25 @@ public class IdentityService : IIdentityService
}
}
public void ClearCache()
{
_cachedProfile = null;
if (OnStateInvalidated != null)
{
_ = Task.Run(async () =>
{
try
{
await OnStateInvalidated.Invoke();
}
catch
{
// Ignore exceptions from event handlers
}
});
}
}
private class LoginResponse
{
public string TokenType { get; set; } = string.Empty;
@@ -17,6 +17,8 @@ public sealed partial class KnowledgeCoordinator : IDisposable
private readonly IReaderInteractionService _interactionService;
private readonly ILogger<KnowledgeCoordinator> _logger;
public string CurrentFullPageContent { get; private set; } = string.Empty;
/// <summary>
/// Raised when the knowledge graph has been updated with new data.
/// Subscribers must return a Task to enable proper async handling.
@@ -77,6 +79,7 @@ public sealed partial class KnowledgeCoordinator : IDisposable
{
if (string.IsNullOrWhiteSpace(fullContent)) return;
CurrentFullPageContent = fullContent;
LogGeneratingGraph(tenantId);
await _graphService.Clear();
@@ -148,6 +151,7 @@ public sealed partial class KnowledgeCoordinator : IDisposable
public async Task ClearAsync()
{
CurrentFullPageContent = string.Empty;
await _graphService.Clear();
await _quizService.SetQuiz(null, null);
}
@@ -118,4 +118,22 @@ public class ServerIdentityService : IIdentityService
return Result.Ok(result.Value);
}
public void ClearCache()
{
if (OnStateInvalidated != null)
{
_ = Task.Run(async () =>
{
try
{
await OnStateInvalidated.Invoke();
}
catch
{
// Ignore
}
});
}
}
}
@@ -0,0 +1,107 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using NexusReader.Application.Commands.Quiz;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
using Xunit;
namespace NexusReader.Application.Tests.Commands;
public class SubmitQuizResultCommandHandlerTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<AppDbContext> _contextOptions;
private readonly Mock<IDbContextFactory<AppDbContext>> _dbContextFactoryMock;
public SubmitQuizResultCommandHandlerTests()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_contextOptions = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(_connection)
.Options;
using var context = new AppDbContext(_contextOptions);
context.Database.EnsureCreated();
_dbContextFactoryMock = new Mock<IDbContextFactory<AppDbContext>>();
_dbContextFactoryMock.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(() => new AppDbContext(_contextOptions));
}
[Fact]
public async Task Handle_WithValidRequest_PersistsQuizResultToDatabase()
{
// Arrange
using (var context = new AppDbContext(_contextOptions))
{
var user = new NexusUser
{
Id = "user-abc",
UserName = "testuser",
Email = "test@example.com",
TenantId = "tenant-xyz",
SubscriptionPlanId = 1
};
context.Users.Add(user);
await context.SaveChangesAsync();
}
var command = new SubmitQuizResultCommand(
UserId: "user-abc",
Topic: "Sprawdzian: .NET 10",
Score: 4,
TotalQuestions: 5
);
var handler = new SubmitQuizResultCommandHandler(_dbContextFactoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeTrue();
using (var context = new AppDbContext(_contextOptions))
{
var quizResult = await context.QuizResults.FirstOrDefaultAsync(q => q.UserId == "user-abc");
quizResult.Should().NotBeNull();
quizResult!.Topic.Should().Be("Sprawdzian: .NET 10");
quizResult.Score.Should().Be(4);
quizResult.TotalQuestions.Should().Be(5);
quizResult.TenantId.Should().Be("tenant-xyz");
}
}
[Fact]
public async Task Handle_WithNonExistentUser_ReturnsFailureResult()
{
// Arrange
var command = new SubmitQuizResultCommand(
UserId: "non-existent",
Topic: "Sprawdzian: .NET 10",
Score: 4,
TotalQuestions: 5
);
var handler = new SubmitQuizResultCommandHandler(_dbContextFactoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "User not found.");
}
public void Dispose()
{
_connection.Dispose();
}
}