// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using EpicGames.Slack.Elements; namespace EpicGames.Slack { /// /// Describes a Slack view /// public class SlackView : ISlackBlockContainer { /// /// The type of view. Set to modal for modals and home for Home tabs. /// [JsonPropertyName("type")] public string Type { get; } = "modal"; /// /// The title that appears in the top-left of the modal. Must be a plain_text text element with a max length of 24 characters. /// [JsonPropertyName("title")] public PlainTextObject Title { get; set; } /// /// An array of blocks that defines the content of the view. Max of 100 blocks. /// [JsonPropertyName("blocks")] public List Blocks { get; } = new List(); /// /// An optional plain_text element that defines the text displayed in the close button at the bottom-right of the view. Max length of 24 characters. /// [JsonPropertyName("close")] public PlainTextObject? Close { get; set; } /// /// An optional plain_text element that defines the text displayed in the submit button at the bottom-right of the view. submit is required when an input block is within the blocks array. Max length of 24 characters. /// [JsonPropertyName("submit")] public PlainTextObject? Submit { get; set; } /// /// An optional string that will be sent to your app in view_submission and block_actions events. Max length of 3000 characters. /// [JsonPropertyName("private_metadata")] public string? PrivateMetadata { get; set; } /// /// An identifier to recognize interactions and submissions of this particular view.Don't use this to store sensitive information (use private_metadata instead). Max length of 255 characters. /// [JsonPropertyName("callback_id")] public string? CallbackId { get; set; } /// /// When set to true, clicking on the close button will clear all views in a modal and close it. Defaults to false. /// [JsonPropertyName("clear_on_close")] public bool? ClearOnClose { get; set; } /// /// Indicates whether Slack will send your request URL a view_closed event when a user clicks the close button. Defaults to false. /// [JsonPropertyName("notify_on_close")] public bool? NotifyOnClose { get; set; } /// /// A custom identifier that must be unique for all views on a per-team basis. /// [JsonPropertyName("external_id")] public string? ExternalId { get; set; } /// /// When set to true, disables the submit button until the user has completed one or more inputs. This property is for configuration modals. /// [JsonPropertyName("submit_disabled")] public bool? SubmitDisabled { get; set; } /// /// State value when returned via a form notification /// [JsonPropertyName("state")] public SlackViewState? State { get; set; } /// /// Constructor /// /// public SlackView(PlainTextObject title) { Title = title; } } /// /// State object returned from a submission /// public class SlackViewState { /// /// Values for the form, by blockId, then by actionId /// [JsonPropertyName("values")] #pragma warning disable CA2227 // Collection properties should be read only public Dictionary> Values { get; set; } = new Dictionary>(); #pragma warning restore CA2227 // Collection properties should be read only /// /// Gets a value from the state /// public bool TryGetValue(string blockId, string actionId, [NotNullWhen(true)] out string? value) { SlackViewValue? viewValue; if (TryGetValue(blockId, actionId, out viewValue)) { value = viewValue.Value ?? viewValue.SelectedOption?.Value; return value != null; } else { value = null; return false; } } /// /// Gets a value from the state /// public bool TryGetValue(string blockId, string actionId, [NotNullWhen(true)] out SlackViewValue? value) { Dictionary? actions; if (Values.TryGetValue(blockId, out actions) && actions.TryGetValue(actionId, out SlackViewValue? viewValue)) { value = viewValue; return true; } else { value = null; return false; } } } /// /// Value for a form item /// public class SlackViewValue { /// /// The item type /// [JsonPropertyName("type")] public string Type { get; set; } = String.Empty; /// /// Value for the widget (used for text fields) /// [JsonPropertyName("value")] public string? Value { get; set; } /// /// Current selected option (used for radio buttons) /// [JsonPropertyName("selected_option")] public SlackOption? SelectedOption { get; set; } } }