using AIStudio.Assistants.SWOTAnalysis;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Chat;
///
/// Shows the structured presentation of a text, or the text itself when it has none.
///
///
/// The one place which knows every kind of presentation and the view which shows it. Chats and
/// assistants render each text through this component, so a new kind needs a case here and in
/// Read, and nothing else in the chat.
///
public partial class ContentPresentationView
{
///
/// The presentation of the text, if it has one.
///
[Parameter]
public ContentPresentation? Presentation { get; set; }
///
/// How the text looks without a presentation, and when its data cannot be read.
///
[Parameter]
public RenderFragment? ChildContent { get; set; }
private ContentPresentation? readPresentation;
private int readDataHashCode;
private object? data;
#region Overrides of ComponentBase
protected override void OnParametersSet()
{
//
// Reading the data means deserializing it, so only a new presentation or changed data are
// read again; a parent renders far more often than either happens:
//
var dataHashCode = this.Presentation?.GetDataHashCode() ?? 0;
if (ReferenceEquals(this.Presentation, this.readPresentation) && dataHashCode == this.readDataHashCode)
return;
this.readPresentation = this.Presentation;
this.readDataHashCode = dataHashCode;
this.data = Read(this.Presentation);
}
#endregion
///
/// Reads the data of a presentation into the type its view shows.
///
/// The presentation to read.
/// The data, or null when there is no presentation or its data cannot be read.
private static object? Read(ContentPresentation? presentation) => presentation?.Kind switch
{
ContentPresentationKind.SWOT_ANALYSIS => SwotAnalysisContentPresentation.TryRead(presentation, out var result) ? result : null,
_ => null,
};
}