1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::SharedWorkerDefinition;
use css_in_rust_next::Style;
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
use web_sys::Node;
use yew::virtual_dom::VNode;
use yew::{html, Component, Context, Html, Properties};

#[derive(PartialEq, Properties)]
pub struct WorkerDetailsPanelProperties {
  pub height: String,
  pub width: String,
  pub worker_definition: SharedWorkerDefinition,
}

pub struct WorkerDetailsPanel {
  style: Style,
}

impl Component for WorkerDetailsPanel {
  type Message = ();
  type Properties = WorkerDetailsPanelProperties;

  fn create(_ctx: &Context<Self>) -> Self {
    let style = Style::create("Component", include_str!("panel.css")).unwrap();
    Self { style }
  }

  fn view(&self, ctx: &Context<Self>) -> Html {
    let style = format!(
      "height: {}; width: {};",
      ctx.props().height,
      ctx.props().width,
    );

    let worker = ctx.props().worker_definition.lock().unwrap();

    let description = web_sys::window()
      .unwrap()
      .document()
      .unwrap()
      .create_element("div")
      .unwrap();

    description.set_inner_html(&worker.description.description.replace('\n', "<br/>"));

    let description = VNode::VRef(Node::from(description));

    let parameters: Html = worker
      .parameters
      .schema
      .object
      .as_ref()
      .unwrap()
      .properties
      .iter()
      .map(|(key, kind)| {
        let (parameter_title, parameter_type) = match kind {
          Schema::Object(SchemaObject {
            metadata,
            instance_type: Some(SingleOrVec::Single(instance_type)),
            ..
          }) => {
            let kind = match *instance_type.as_ref() {
              InstanceType::String => "String",
              InstanceType::Boolean => "Boolean",
              InstanceType::Array => "Array",
              InstanceType::Integer => "Integer",
              InstanceType::Number => "Number",
              InstanceType::Object => "Object",
              InstanceType::Null => "Null",
            };

            let title: String = metadata
              .as_ref()
              .and_then(|metadata| metadata.title.clone())
              .unwrap_or_default();

            (html!(title), html!(kind))
          }
          Schema::Object(SchemaObject {
            metadata,
            instance_type: Some(SingleOrVec::Vec(instance_type)),
            ..
          }) => {
            let mut kinds = instance_type
              .iter()
              .filter(|kind| kind != &&InstanceType::Null)
              .map(|kind| match *kind {
                InstanceType::String => "String",
                InstanceType::Boolean => "Boolean",
                InstanceType::Array => "Array",
                InstanceType::Integer => "Integer",
                InstanceType::Number => "Number",
                InstanceType::Object => "Object",
                InstanceType::Null => "Null",
              })
              .collect::<Vec<&str>>()
              .join(", ");

            if instance_type.contains(&InstanceType::Null) {
              kinds += " (optional)";
            }

            let title: String = metadata
              .as_ref()
              .and_then(|metadata| metadata.title.clone())
              .unwrap_or_default();

            (html!(title), html!({ kinds }))
          }
          _ => (html!(""), html!({ "Complex type" })),
        };

        html!(
          <div class="field">
            <label>
              {key}
              <span class="type">
                {parameter_type}
              </span>
            </label>
            <span class="description">
              {parameter_title}
            </span>
          </div>
        )
      })
      .collect();

    html!(
      <span class={self.style.clone()} style={style}>
        <div class="title">
          <label>{format!("Worker {}", worker.description.label)}</label>
        </div>

        <div class="content">
          <div class="field">
            <label>
              {"Short description"}
            </label>
            <span>
              <div class="description">
                {{&worker.description.short_description}}
              </div>
            </span>
          </div>
          <div class="field">
            <label>
              {"Description"}
            </label>
            <span>
              <div class="description">
                {description}
              </div>
            </span>
          </div>
          <div class="field">
            <label>
              {"Version"}
            </label>
            <span>
              {{&worker.description.version}}
            </span>
          </div>
          <div class="field">
            <label>
              {"SDK Version"}
            </label>
            <span>
              {{&worker.description.sdk_version}}
            </span>
          </div>
          <h2>{"Parameters"}</h2>
          {parameters}
        </div>
      </span>
    )
  }
}