command_tower 0.21.1 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/command_tower/execution/http_boundary.rb +2 -1
  3. data/docs/api_reference.md +7 -3
  4. data/docs/bootstrap/11-auth-client-path.md +2 -0
  5. data/docs/extending.md +1 -1
  6. data/docs/messaging_integration_guide.md +2 -0
  7. data/docs/ui.md +138 -0
  8. data/docs/upgrades/0.22.0.md +29 -0
  9. data/docs/upgrades/README.md +1 -0
  10. data/lib/command_tower/configuration/config.rb +6 -0
  11. data/lib/command_tower/configuration/ui/config.rb +47 -0
  12. data/lib/command_tower/current.rb +1 -0
  13. data/lib/command_tower/execution.rb +2 -1
  14. data/lib/command_tower/ui/builder/frame.rb +19 -0
  15. data/lib/command_tower/ui/builder/item_builder.rb +27 -0
  16. data/lib/command_tower/ui/builder/token_resolver.rb +54 -0
  17. data/lib/command_tower/ui/builder.rb +157 -0
  18. data/lib/command_tower/ui/catalog.rb +18 -0
  19. data/lib/command_tower/ui/composer.rb +45 -0
  20. data/lib/command_tower/ui/document.rb +167 -0
  21. data/lib/command_tower/ui/email/layout.rb +95 -0
  22. data/lib/command_tower/ui/email/renderer/containers.rb +72 -0
  23. data/lib/command_tower/ui/email/renderer/inline_nodes.rb +70 -0
  24. data/lib/command_tower/ui/email/renderer/styling.rb +91 -0
  25. data/lib/command_tower/ui/email/renderer/text_nodes.rb +78 -0
  26. data/lib/command_tower/ui/email/renderer.rb +89 -0
  27. data/lib/command_tower/ui/email/styles.rb +77 -0
  28. data/lib/command_tower/ui/fragment.rb +54 -0
  29. data/lib/command_tower/ui/fragments/cta_group.rb +60 -0
  30. data/lib/command_tower/ui/fragments/fact_row.rb +20 -0
  31. data/lib/command_tower/ui/fragments/facts.rb +35 -0
  32. data/lib/command_tower/ui/fragments/header.rb +51 -0
  33. data/lib/command_tower/ui/fragments/notice_frame.rb +36 -0
  34. data/lib/command_tower/ui/hardening.rb +73 -0
  35. data/lib/command_tower/ui/hostname.rb +124 -0
  36. data/lib/command_tower/ui/legacy_mapping.rb +66 -0
  37. data/lib/command_tower/ui/limits.rb +19 -0
  38. data/lib/command_tower/ui/nodes/action.rb +53 -0
  39. data/lib/command_tower/ui/nodes/avatar.rb +35 -0
  40. data/lib/command_tower/ui/nodes/badge.rb +17 -0
  41. data/lib/command_tower/ui/nodes/banner.rb +17 -0
  42. data/lib/command_tower/ui/nodes/base.rb +125 -0
  43. data/lib/command_tower/ui/nodes/button.rb +34 -0
  44. data/lib/command_tower/ui/nodes/card.rb +19 -0
  45. data/lib/command_tower/ui/nodes/code.rb +28 -0
  46. data/lib/command_tower/ui/nodes/component.rb +39 -0
  47. data/lib/command_tower/ui/nodes/divider.rb +11 -0
  48. data/lib/command_tower/ui/nodes/facts.rb +14 -0
  49. data/lib/command_tower/ui/nodes/image.rb +41 -0
  50. data/lib/command_tower/ui/nodes/instant.rb +59 -0
  51. data/lib/command_tower/ui/nodes/key_value.rb +63 -0
  52. data/lib/command_tower/ui/nodes/span.rb +76 -0
  53. data/lib/command_tower/ui/nodes/stack.rb +24 -0
  54. data/lib/command_tower/ui/nodes/stat_tile.rb +20 -0
  55. data/lib/command_tower/ui/nodes/text.rb +24 -0
  56. data/lib/command_tower/ui/text_derivation.rb +89 -0
  57. data/lib/command_tower/ui/time_formatter.rb +59 -0
  58. data/lib/command_tower/ui/tokens/ui_token_mapping.json +277 -0
  59. data/lib/command_tower/ui/tokens.rb +56 -0
  60. data/lib/command_tower/ui/zone_abbreviation.rb +130 -0
  61. data/lib/command_tower/ui.rb +80 -0
  62. data/lib/command_tower/version.rb +1 -1
  63. data/lib/command_tower.rb +1 -0
  64. data/lib/tasks/ui.rake +16 -0
  65. metadata +67 -1
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ # Base class for host and platform document composers. A subclass implements
6
+ # `compose(d, **inputs)` against the Builder and the class method returns a
7
+ # validated Document:
8
+ #
9
+ # class ClassCanceled < CommandTower::UI::Composer
10
+ # def compose(d, starts_at:)
11
+ # d.card { |c| c.text "Class canceled" }
12
+ # end
13
+ # end
14
+ #
15
+ # ClassCanceled.call(starts_at: time) # => CommandTower::UI::Document
16
+ #
17
+ # `text_fallback` is derived from the tree (see TextDerivation). A subclass
18
+ # may override `text_fallback(root, **inputs)` to return its own string.
19
+ #
20
+ # Raises InvalidDocumentError (paths and codes only) when the result is not
21
+ # valid. No registry (design D3).
22
+ class Composer
23
+ def self.call(**inputs)
24
+ new.call(**inputs)
25
+ end
26
+
27
+ def call(**inputs)
28
+ built = UI.document(text_fallback: "-") { |d| compose(d, **inputs) }
29
+ built.validate! # the tree must be valid before text is derived from it
30
+ fallback = text_fallback(built.root, **inputs) || TextDerivation.call(built.root)
31
+ Document.new(root: built.root, text_fallback: fallback).validate!
32
+ end
33
+
34
+ # Subclasses implement this.
35
+ def compose(_builder, **_inputs)
36
+ raise NotImplementedError, "#{self.class.name} must implement compose(builder, **inputs)"
37
+ end
38
+
39
+ # Hook: return a String to replace the derived fallback, or nil to derive.
40
+ def text_fallback(_root, **_inputs)
41
+ nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module CommandTower
6
+ module UI
7
+ # Envelope { schema, catalog, root, textFallback } plus document-wide
8
+ # validation. Errors carry node paths and codes only, never values
9
+ # (a `code` value, spans and times must not reach logs or exceptions).
10
+ #
11
+ # `validate!(profile:)` is the seam for later rule sets (the blocking-notice
12
+ # profile in Set 3 adds external-links-only). Only :default exists.
13
+ class Document
14
+ PROFILES = %i[default].freeze
15
+ MESSAGE_ERROR_LIMIT = 10
16
+
17
+ attr_reader :root, :text_fallback
18
+
19
+ def initialize(root:, text_fallback:)
20
+ @root = root
21
+ @text_fallback = text_fallback
22
+ end
23
+
24
+ def catalog
25
+ CATALOG
26
+ end
27
+
28
+ def as_json(_options = nil)
29
+ {
30
+ "schema" => SCHEMA,
31
+ "catalog" => catalog,
32
+ "root" => root.as_json,
33
+ "textFallback" => text_fallback
34
+ }
35
+ end
36
+
37
+ # Array of { path:, code: } hashes. Empty when the document is valid.
38
+ def validation_errors(profile: :default)
39
+ raise ArgumentError, "unknown validation profile" unless PROFILES.include?(profile)
40
+
41
+ walker = Walker.new
42
+ walker.check_envelope(self)
43
+ walker.walk(root, "root", 1) if root.is_a?(Nodes::Node)
44
+ walker.check_size(self)
45
+ walker.details
46
+ end
47
+
48
+ def validate!(profile: :default)
49
+ details = validation_errors(profile:)
50
+ return self if details.empty?
51
+
52
+ raise InvalidDocumentError.new(message_for(details), details:)
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class.name} catalog=#{catalog}>"
57
+ end
58
+ alias to_s inspect
59
+
60
+ private
61
+
62
+ def message_for(details)
63
+ shown = details.first(MESSAGE_ERROR_LIMIT).map { |detail| "#{detail[:path]}:#{detail[:code]}" }
64
+ extra = details.length > MESSAGE_ERROR_LIMIT ? " (+#{details.length - MESSAGE_ERROR_LIMIT} more)" : ""
65
+ "Invalid UI document (#{details.length} errors): #{shown.join(', ')}#{extra}"
66
+ end
67
+
68
+ # Depth-first traversal collecting per-node ActiveModel errors and
69
+ # document-wide limits.
70
+ class Walker
71
+ attr_reader :details
72
+
73
+ def initialize
74
+ @details = []
75
+ @nodes = 0
76
+ @components = 0
77
+ @primary_buttons = 0
78
+ @open_facts = []
79
+ @structure_overflow = false
80
+ end
81
+
82
+ def check_envelope(document)
83
+ add("root", :invalid_node) unless document.root.is_a?(Nodes::Node)
84
+ fallback = document.text_fallback
85
+ if !fallback.is_a?(String) || fallback.empty?
86
+ add("textFallback", :blank)
87
+ elsif fallback.length > Limits::MAX_TEXT_FALLBACK_LENGTH
88
+ add("textFallback", :too_long)
89
+ end
90
+ end
91
+
92
+ def walk(object, path, depth)
93
+ return if @structure_overflow
94
+
95
+ return if object.node? && reject_node?(object, path, depth)
96
+
97
+ collect_errors(object, path)
98
+ object.each_child { |relative, child| walk(child, "#{path}.#{relative}", depth + (child.node? ? 1 : 0)) }
99
+ leave_node(object, path)
100
+ end
101
+
102
+ def check_size(document)
103
+ return if @structure_overflow || @details.any? { |detail| detail[:code] == "too_deep" }
104
+
105
+ add("document", :too_large) if JSON.generate(document.as_json).bytesize > Limits::MAX_BYTES
106
+ end
107
+
108
+ private
109
+
110
+ # True when the walk must not descend into this node (limit breached).
111
+ def reject_node?(node, path, depth)
112
+ @nodes += 1
113
+ code = limit_code(depth)
114
+ if code
115
+ @structure_overflow = true if code == :too_many_nodes
116
+ add(path, code)
117
+ else
118
+ track(node, path)
119
+ end
120
+ !code.nil?
121
+ end
122
+
123
+ def limit_code(depth)
124
+ return :too_many_nodes if @nodes > Limits::MAX_NODES
125
+
126
+ :too_deep if depth > Limits::MAX_DEPTH
127
+ end
128
+
129
+ def track(node, path)
130
+ @open_facts.each { |entry| entry[:count] += 1 }
131
+ @open_facts.push({ path:, count: 0 }) if node.is_a?(Nodes::Facts)
132
+ count_singletons(node, path)
133
+ end
134
+
135
+ def leave_node(object, path)
136
+ return unless object.is_a?(Nodes::Facts)
137
+
138
+ entry = @open_facts.pop
139
+ add(path, :too_many_facts_nodes) if entry[:count] > Limits::MAX_FACTS_NODES
140
+ end
141
+
142
+ def count_singletons(node, path)
143
+ if node.is_a?(Nodes::Component)
144
+ @components += 1
145
+ add(path, :multiple_components) if @components > 1
146
+ elsif node.is_a?(Nodes::Button) && node.primary?
147
+ @primary_buttons += 1
148
+ add(path, :multiple_primary_buttons) if @primary_buttons > 1
149
+ end
150
+ end
151
+
152
+ def collect_errors(object, path)
153
+ object.valid?
154
+ object.errors.each do |error|
155
+ suffix = error.attribute == :base ? "" : ".#{error.attribute.to_s.camelize(:lower)}"
156
+ add("#{path}#{suffix}", error.type)
157
+ end
158
+ end
159
+
160
+ def add(path, code)
161
+ @details << { path:, code: code.to_s }
162
+ end
163
+ end
164
+ private_constant :Walker
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module CommandTower
6
+ module UI
7
+ module Email
8
+ # Email chrome around a rendered body. Spec: design ux/states.md 5.H.
9
+ # `body_html` and `footer_html` are trusted, already-escaped HTML; `preheader`
10
+ # and `app_name` are plain text and escaped here. Light only, no dark mode.
11
+ module Layout
12
+ CONTAINER_WIDTH = 600
13
+ CONTAINER_RADIUS = 12
14
+ CONTAINER_PADDING = 24
15
+ CANVAS_PADDING_Y = 24
16
+ CANVAS_PADDING_X = 16
17
+ APP_NAME_PADDING_BOTTOM = 12
18
+ FOOTER_PADDING_TOP = 16
19
+ SMALL_TEXT = "font-size:14px;line-height:20px;"
20
+ PREHEADER_STYLE = "display:none;font-size:1px;line-height:1px;max-height:0;max-width:0;" \
21
+ "opacity:0;overflow:hidden;mso-hide:all;visibility:hidden;"
22
+ HEAD = <<~HTML
23
+ <!DOCTYPE html>
24
+ <html lang="en">
25
+ <head>
26
+ <meta charset="utf-8">
27
+ <meta name="viewport" content="width=device-width, initial-scale=1">
28
+ <meta name="color-scheme" content="light">
29
+ <meta name="supported-color-schemes" content="light">
30
+ </head>
31
+ HTML
32
+
33
+ class << self
34
+ def call(body_html:, palette:, preheader:, footer_html: nil, app_name: nil)
35
+ palette = Styles.validate_palette!(palette)
36
+ [
37
+ HEAD,
38
+ body_open(palette, preheader),
39
+ app_name_row(app_name, palette),
40
+ container_row(body_html, palette),
41
+ footer_row(footer_html, palette),
42
+ "</table>\n</td>\n</tr>\n</table>\n</body>\n</html>\n"
43
+ ].join
44
+ end
45
+
46
+ private
47
+
48
+ def body_open(palette, preheader)
49
+ background = palette.fetch("background")
50
+ font = Tokens.mapping.fetch("fonts").fetch("sans")
51
+ <<~HTML
52
+ <body style="margin:0;padding:0;background-color:#{background};font-family:#{font};">
53
+ <span aria-hidden="true" style="#{PREHEADER_STYLE}">#{ERB::Util.html_escape(preheader)}</span>
54
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#{background};table-layout:fixed;">
55
+ <tr>
56
+ <td align="center" style="padding:#{CANVAS_PADDING_Y}px #{CANVAS_PADDING_X}px;">
57
+ <table role="presentation" width="#{CONTAINER_WIDTH}" cellpadding="0" cellspacing="0" border="0" style="width:100%;max-width:#{CONTAINER_WIDTH}px;table-layout:fixed;">
58
+ HTML
59
+ end
60
+
61
+ def container_row(body_html, palette)
62
+ card_style = "background-color:#{palette.fetch('card')};border:1px solid #{palette.fetch('border')};" \
63
+ "border-radius:#{CONTAINER_RADIUS}px;padding:#{CONTAINER_PADDING}px;"
64
+ %(<tr>\n<td style="#{card_style}">\n#{body_html}\n</td>\n</tr>\n)
65
+ end
66
+
67
+ def app_name_row(app_name, palette)
68
+ return "" if app_name.nil?
69
+
70
+ style = "padding-bottom:#{APP_NAME_PADDING_BOTTOM}px;#{SMALL_TEXT}font-weight:600;" \
71
+ "color:#{palette.fetch('textPrimary')};"
72
+ %(<tr><td style="#{style}">#{ERB::Util.html_escape(app_name)}</td></tr>\n)
73
+ end
74
+
75
+ def footer_row(footer_html, palette)
76
+ return "" if footer_html.nil?
77
+
78
+ style = "padding-top:#{FOOTER_PADDING_TOP}px;#{SMALL_TEXT}color:#{palette.fetch('textSecondary')};"
79
+ %(<tr><td style="#{style}">#{style_footer_links(footer_html, palette)}</td></tr>\n)
80
+ end
81
+
82
+ # Footer links take the footer text colour and are underlined (email.html).
83
+ # A link that already carries its own `style` is left alone.
84
+ def style_footer_links(footer_html, palette)
85
+ link_style = "color:#{palette.fetch('textSecondary')};text-decoration:underline;"
86
+ footer_html.to_str.gsub(/<a(?<attrs>\s[^>]*)?>/i) do |tag|
87
+ attrs = Regexp.last_match[:attrs].to_s
88
+ attrs.match?(/\sstyle\s*=/i) ? tag : %(<a#{attrs} style="#{link_style}">)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Email
6
+ class Renderer
7
+ # stack, facts, card, divider and component. Stack gaps become child
8
+ # bottom padding; `between` has no email equivalent and renders as stretch.
9
+ module Containers
10
+ ALIGN_ATTR = { "start" => "left", "center" => "center", "end" => "right" }.freeze
11
+ HUGGING = [Nodes::Badge, Nodes::Avatar, Nodes::Button].freeze
12
+ CARD_RADIUS = 12
13
+
14
+ private
15
+
16
+ def stack(node)
17
+ rows(node.children, Tokens.spacing_px(node.gap), node.align)
18
+ end
19
+
20
+ def facts(node)
21
+ rows(node.children, Tokens.spacing_px("md"), nil)
22
+ end
23
+
24
+ def card(node)
25
+ padding = Tokens.spacing_px(node.padding)
26
+ inner = rows(node.children, 0, nil)
27
+ case node.surface
28
+ when "raised" then framed(inner, @palette.fetch("card"), @palette.fetch("border"), CARD_RADIUS, padding)
29
+ when "tinted" then tinted(node.tone || "neutral", CARD_RADIUS, padding, inner)
30
+ else framed(inner, nil, nil, 0, padding)
31
+ end
32
+ end
33
+
34
+ def divider(_node)
35
+ style = css("background-color" => @palette.fetch("border"), "font-size" => "1px", "line-height" => "1px")
36
+ tag.table(tag.tr(tag.td("", height: 1, style:)), **FILL_TABLE)
37
+ end
38
+
39
+ def component(node)
40
+ render(node.fallback)
41
+ end
42
+
43
+ def rows(children, gap_px, align)
44
+ last = children.size - 1
45
+ trs = children.each_with_index.map do |child, index|
46
+ attr = cell_align(child, align)
47
+ style = css("padding-bottom" => (index == last ? nil : "#{gap_px}px"), "text-align" => attr)
48
+ tag.tr(tag.td(render(child), align: attr, style:))
49
+ end
50
+ tag.table(safe_join(trs), **FILL_TABLE)
51
+ end
52
+
53
+ def cell_align(child, align)
54
+ ALIGN_ATTR[align] || (HUGGING.any? { |klass| child.is_a?(klass) } ? "left" : nil)
55
+ end
56
+
57
+ # A one-cell table with optional background, 1px border and radius.
58
+ def framed(inner, background, border, radius, padding)
59
+ style = css("background-color" => background, "border" => border && "1px solid #{border}",
60
+ "border-radius" => (radius.positive? ? "#{radius}px" : nil), "padding" => "#{padding}px")
61
+ tag.table(tag.tr(tag.td(inner, style:)), **FILL_TABLE)
62
+ end
63
+
64
+ def tinted(tone, radius, padding, inner)
65
+ tint = Styles.tint(tone, @palette)
66
+ framed(inner, tint.fetch(:background), tint.fetch(:border), radius, padding)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Email
6
+ class Renderer
7
+ # badge, button, image and avatar, plus link and image-source checks.
8
+ module InlineNodes
9
+ IMAGE_WIDTH = 552 # container 600 minus 2 x 24 padding
10
+
11
+ private
12
+
13
+ def badge(node)
14
+ tag.table(tag.tr(tag.td(node.label, style: badge_style(node.tone))), **TABLE)
15
+ end
16
+
17
+ def button(node)
18
+ return tag.a(node.label, href: link_href(node.action), style: small_link_style) if node.variant == "ghost"
19
+
20
+ tag.table(tag.tr(tag.td(button_link(node), style: button_cell_style(node.variant))), **TABLE)
21
+ end
22
+
23
+ def button_link(node)
24
+ tag.a(node.label, href: link_href(node.action), style: button_link_style(node.variant))
25
+ end
26
+
27
+ def image(node)
28
+ check_image!(node.src)
29
+ tag.img(src: node.src, alt: node.decorative ? "" : node.alt.to_s, width: IMAGE_WIDTH,
30
+ height: (IMAGE_WIDTH / node.aspect).round,
31
+ style: css("display" => "block", "border" => 0, "max-width" => "100%", "height" => "auto"))
32
+ end
33
+
34
+ def avatar(node)
35
+ size = Tokens.avatar_px(node.size)
36
+ return avatar_image(node, size) if node.src.present?
37
+
38
+ cell = tag.td(node.initials.to_s, width: size, height: size, align: "center", valign: "middle",
39
+ style: avatar_style(size))
40
+ tag.table(tag.tr(cell), **TABLE)
41
+ end
42
+
43
+ def avatar_image(node, size)
44
+ check_image!(node.src)
45
+ tag.img(src: node.src, alt: node.alt.to_s, width: size, height: size,
46
+ style: css("display" => "block", "border" => 0, "border-radius" => "50%"))
47
+ end
48
+
49
+ def link_href(action)
50
+ return @origin + action.path if action.path
51
+
52
+ code = Hardening.external_href_code(action.href)
53
+ reject!("link", code) if code
54
+ action.href
55
+ end
56
+
57
+ def check_image!(src)
58
+ code = Hardening.image_src_code(src, hosts: CommandTower.config.ui.image_hosts)
59
+ reject!("image", code) if code
60
+ end
61
+
62
+ def reject!(path, code)
63
+ message = "Invalid UI document: #{path}:#{code}"
64
+ raise InvalidDocumentError.new(message, details: [{ path:, code: code.to_s }])
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Email
6
+ class Renderer
7
+ # Inline-style building blocks shared by the node renderers. Colours come
8
+ # only from the validated palette; sizes only from the token mapping.
9
+ module Styling
10
+ private
11
+
12
+ def css(pairs)
13
+ pairs.filter_map { |name, value| "#{name}:#{value};" unless value.nil? }.join
14
+ end
15
+
16
+ def sans
17
+ Tokens.mapping.fetch("fonts").fetch("sans")
18
+ end
19
+
20
+ def type_style(variant, tone, extra = {})
21
+ css(type_pairs(variant, tone).merge(extra))
22
+ end
23
+
24
+ def type_pairs(variant, tone)
25
+ step = Tokens.type_step(variant)
26
+ font = step.fetch("mono") ? Tokens.mapping.fetch("fonts").fetch("mono") : sans
27
+ {
28
+ "margin" => 0, "font-family" => font,
29
+ "font-size" => "#{step.fetch('size')}px", "line-height" => "#{step.fetch('lineHeight')}px",
30
+ "font-weight" => step.fetch("weight"), "letter-spacing" => step["tracking"],
31
+ "text-transform" => step.fetch("uppercase") ? "uppercase" : nil,
32
+ "color" => @palette.fetch(Tokens.mapping.fetch("textTones").fetch(tone))
33
+ }
34
+ end
35
+
36
+ def link_pairs
37
+ { "color" => @palette.fetch("primary"), "text-decoration" => "underline" }
38
+ end
39
+
40
+ def small_link_style
41
+ css(link_pairs.merge("font-size" => "14px", "line-height" => "20px", "font-weight" => 500))
42
+ end
43
+
44
+ def label_style
45
+ css(type_pairs("caption", "secondary").merge("line-height" => "24px", "padding-right" => "12px"))
46
+ end
47
+
48
+ def value_style
49
+ css(type_pairs("body", "primary").merge("word-break" => "break-word"))
50
+ end
51
+
52
+ def badge_style(tone)
53
+ background, color = badge_colors(tone)
54
+ css("background-color" => background, "color" => color, "font-size" => "12px", "line-height" => "16px",
55
+ "font-weight" => 600, "padding" => "2px 10px", "border-radius" => "9999px", "font-family" => sans)
56
+ end
57
+
58
+ def badge_colors(tone)
59
+ case tone
60
+ when "neutral" then [@palette.fetch("muted"), @palette.fetch("foreground")]
61
+ when "warning" then [@palette.fetch("warning"), @palette.fetch("foreground")]
62
+ else [@palette.fetch(Tokens.tone_palette_key(tone)), @palette.fetch("primaryForeground")]
63
+ end
64
+ end
65
+
66
+ def button_cell_style(variant)
67
+ if variant == "primary"
68
+ css("background-color" => @palette.fetch("primary"), "border-radius" => "6px")
69
+ else
70
+ css("background-color" => @palette.fetch("background"),
71
+ "border" => "1px solid #{@palette.fetch('border')}", "border-radius" => "6px")
72
+ end
73
+ end
74
+
75
+ def button_link_style(variant)
76
+ primary = variant == "primary"
77
+ css("display" => "block", "padding" => primary ? "10px 16px" : "9px 15px", "text-decoration" => "none",
78
+ "font-family" => sans, "font-size" => "14px", "line-height" => "20px", "font-weight" => 500,
79
+ "color" => @palette.fetch(primary ? "primaryForeground" : "foreground"))
80
+ end
81
+
82
+ def avatar_style(size)
83
+ css("width" => "#{size}px", "height" => "#{size}px", "background-color" => @palette.fetch("muted"),
84
+ "color" => @palette.fetch("foreground"), "font-family" => sans, "font-size" => "14px",
85
+ "font-weight" => 500, "border-radius" => "50%", "text-align" => "center")
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module UI
5
+ module Email
6
+ class Renderer
7
+ # text, time, banner, key_value and stat_tile. Icons are dropped.
8
+ module TextNodes
9
+ LABEL_WIDTH = 96 # label column; label_style adds the 12px gap, 108 total (states.md 5.H)
10
+ ROW_GAP = 8
11
+
12
+ private
13
+
14
+ def text(node)
15
+ tag.div(spans(node.spans), style: type_style(node.variant, node.text_tone))
16
+ end
17
+
18
+ def instant(node)
19
+ tag.div(time_text(node), style: type_style(node.variant, node.text_tone))
20
+ end
21
+
22
+ def banner(node)
23
+ body = tag.div(spans(node.spans), style: type_style("body", "primary"))
24
+ bold = type_style("body", "primary", "font-weight" => 600)
25
+ title = tag.div(node.title, style: bold) if node.title.present?
26
+ tinted(node.tone, 6, 12, safe_join([title, body].compact))
27
+ end
28
+
29
+ def key_value(node)
30
+ labelled = node.items.any? { |item| item.label.present? }
31
+ rows = node.items.map { |item| key_value_row(item, labelled) }
32
+ tag.table(safe_join(rows, spacer_row(labelled ? 2 : 1)), **FILL_TABLE)
33
+ end
34
+
35
+ def key_value_row(item, labelled)
36
+ label = tag.td(item.label.to_s, valign: "top", width: LABEL_WIDTH, style: label_style) if labelled
37
+ tag.tr(safe_join([label, tag.td(item_value(item), valign: "top", style: value_style)].compact))
38
+ end
39
+
40
+ def stat_tile(node)
41
+ label = tag.div(node.label, style: type_style("caption", "secondary"))
42
+ big = type_style("heading_m", "primary", "font-size" => "24px", "line-height" => "32px")
43
+ value = tag.div(node.value, style: big)
44
+ tinted(node.tone || "neutral", 6, 12, safe_join([label, value]))
45
+ end
46
+
47
+ def spans(list)
48
+ safe_join(list.map { |span| span_html(span) })
49
+ end
50
+
51
+ def span_html(span)
52
+ if span.type == "link"
53
+ weight = span.strong ? 600 : 500
54
+ tag.a(span.text, href: link_href(span.action), style: css(link_pairs.merge("font-weight" => weight)))
55
+ elsif span.strong
56
+ tag.span(span.text, style: css("font-weight" => 600))
57
+ else
58
+ span.text
59
+ end
60
+ end
61
+
62
+ def item_value(item)
63
+ item.value.is_a?(Nodes::Instant) ? time_text(item.value) : item.value.to_s
64
+ end
65
+
66
+ def time_text(node)
67
+ TimeFormatter.call(at: node.at, zone: node.zone, format: node.format, label: node.label)
68
+ end
69
+
70
+ def spacer_row(columns)
71
+ style = css("font-size" => "#{ROW_GAP}px", "line-height" => "#{ROW_GAP}px")
72
+ tag.tr(tag.td("", colspan: columns, height: ROW_GAP, style:))
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end