Module: Bugsage::PageActions

Defined in:
lib/bugsage/page_actions.rb

Class Method Summary collapse

Class Method Details

.render_clear_button(label: Bugsage.t("ui.page_actions.clear_session_logs")) ⇒ Object



9
10
11
12
13
# File 'lib/bugsage/page_actions.rb', line 9

def render_clear_button(label: Bugsage.t("ui.page_actions.clear_session_logs"))
  <<~HTML
    <button type="button" class="bugsage-clear-logs secondary-button">#{CodeContext.escape_html(label)}</button>
  HTML
end

.render_fix_actions(location:, suffix: "", hidden: true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bugsage/page_actions.rb', line 15

def render_fix_actions(location:, suffix: "", hidden: true)
  editor = EditorLinks.for_location(location)
  return "" if editor.empty?

  hidden_class = hidden ? " hidden" : ""
  location_attr = CodeContext.escape_html(location)

  <<~HTML
    <div class="fix-actions#{hidden_class}" id="bugsage-fix-actions#{suffix}" data-location="#{location_attr}">
      <button type="button" class="secondary-button bugsage-open-editor" data-editor="cursor" data-url="#{CodeContext.escape_html(editor[:cursor])}">
        #{CodeContext.escape_html(Bugsage.t("ui.page_actions.open_in_cursor"))}
      </button>
      <button type="button" class="secondary-button bugsage-open-editor" data-editor="vscode" data-url="#{CodeContext.escape_html(editor[:vscode])}">
        #{CodeContext.escape_html(Bugsage.t("ui.page_actions.open_in_vscode"))}
      </button>
      <button type="button" class="secondary-button bugsage-copy-fix">#{CodeContext.escape_html(Bugsage.t("ui.page_actions.copy_fix"))}</button>
      <button type="button" class="secondary-button bugsage-apply-fix">#{CodeContext.escape_html(Bugsage.t("ui.page_actions.apply_fix_to_file"))}</button>
    </div>
  HTML
end

.render_scriptObject



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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/bugsage/page_actions.rb', line 76

def render_script
  empty_list_html = <<~HTML.strip.gsub("\n", "")
    <div class="empty-list"><p>#{CodeContext.escape_html(Bugsage.t("ui.dashboard.empty_list_title"))}</p><p class="empty-hint">#{CodeContext.escape_html(Bugsage.t("ui.dashboard.empty_list_hint"))}</p></div>
  HTML
  empty_detail_html = <<~HTML.strip.gsub("\n", "")
    <div class="detail-placeholder"><h2>#{CodeContext.escape_html(Bugsage.t("ui.dashboard.empty_detail_title"))}</h2><p>#{CodeContext.escape_html(Bugsage.t("ui.dashboard.empty_detail_body"))}</p></div>
  HTML

  <<~HTML
    <script>
      (function () {
        if (window.__bugsagePageActionsInitialized) return;
        window.__bugsagePageActionsInitialized = true;

        var EMPTY_LIST_HTML = #{JSON.generate(empty_list_html)};
        var EMPTY_DETAIL_HTML = #{JSON.generate(empty_detail_html)};
        var CAUGHT_COUNT_ZERO = #{JSON.generate(Bugsage.t("ui.dashboard.caught_count", count: 0))};
        var AVG_EMPTY = #{JSON.generate(Bugsage.t("ui.dashboard.avg_empty"))};
        var CONFIRM_CLEAR = #{JSON.generate(Bugsage.t("ui.page_actions.confirm_clear"))};
        var COULD_NOT_CLEAR = #{JSON.generate(Bugsage.t("ui.page_actions.could_not_clear"))};
        var QUICK_FIX_TITLE = #{JSON.generate(Bugsage.t("ui.page_actions.quick_fix_title"))};
        var QUICK_FIX_LOCATION = #{JSON.generate(Bugsage.t("ui.page_actions.quick_fix_location"))};
        var QUICK_FIX_SUGGESTION = #{JSON.generate(Bugsage.t("ui.page_actions.quick_fix_suggestion"))};
        var COPIED = #{JSON.generate(Bugsage.t("ui.page_actions.copied"))};
        var COPY_FIX = #{JSON.generate(Bugsage.t("ui.page_actions.copy_fix"))};
        var CONFIRM_APPLY = #{JSON.generate(Bugsage.t("ui.page_actions.confirm_apply"))};
        var APPLYING = #{JSON.generate(Bugsage.t("ui.page_actions.applying"))};
        var COULD_NOT_APPLY = #{JSON.generate(Bugsage.t("ui.page_actions.could_not_apply"))};
        var APPLIED = #{JSON.generate(Bugsage.t("ui.page_actions.applied"))};

        function selectedFixText(actions) {
          var panel = actions.closest(".bug-detail, .container");
          if (!panel) return "";

          var activeFix = panel.querySelector(".fixes li.selected, .fixes li");
          return activeFix ? activeFix.textContent.trim() : "";
        }

        function clipboardCopy(text) {
          if (navigator.clipboard && navigator.clipboard.writeText) {
            return navigator.clipboard.writeText(text);
          }

          var area = document.createElement("textarea");
          area.value = text;
          document.body.appendChild(area);
          area.select();
          document.execCommand("copy");
          document.body.removeChild(area);
          return Promise.resolve();
        }

        function clearDashboardUi() {
          var bugList = document.querySelector(".bug-list");
          var detailPanel = document.querySelector(".detail-panel");
          var clearButton = document.querySelector(".bugsage-clear-logs");
          var statPills = document.querySelectorAll(".sidebar-stats .stat-pill");

          if (bugList) {
            bugList.innerHTML = EMPTY_LIST_HTML;
          }

          if (detailPanel) {
            detailPanel.innerHTML = EMPTY_DETAIL_HTML;
          }

          if (statPills[0]) statPills[0].textContent = CAUGHT_COUNT_ZERO;
          if (statPills[1]) statPills[1].textContent = AVG_EMPTY;
          if (clearButton) clearButton.hidden = true;
        }

        function openEditorUrl(url) {
          var link = document.createElement("a");
          link.href = url;
          link.rel = "noopener";
          link.style.display = "none";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }

        document.addEventListener("click", function (event) {
          var clearButton = event.target.closest(".bugsage-clear-logs");
          if (clearButton) {
            event.preventDefault();
            if (!window.confirm(CONFIRM_CLEAR)) return;

            clearButton.disabled = true;
            fetch("#{SessionClear::ENDPOINT}", {
              method: "POST",
              headers: { "Content-Type": "application/json", "Accept": "application/json" }
            })
              .then(function (response) { return response.json(); })
              .then(function () { clearDashboardUi(); })
              .catch(function (error) {
                clearButton.disabled = false;
                window.alert(error.message || COULD_NOT_CLEAR);
              });
            return;
          }

          var openButton = event.target.closest(".bugsage-open-editor");
          if (openButton) {
            event.preventDefault();
            var url = openButton.dataset.url;
            if (url) openEditorUrl(url);
            return;
          }

          var copyButton = event.target.closest(".bugsage-copy-fix");
          if (copyButton) {
            var actions = copyButton.closest(".fix-actions");
            var fix = selectedFixText(actions);
            var location = actions.dataset.location || "";
            var prompt = [
              QUICK_FIX_TITLE,
              QUICK_FIX_LOCATION.replace("%{location}", location),
              QUICK_FIX_SUGGESTION.replace("%{fix}", fix)
            ].join("\\n");
            clipboardCopy(prompt).then(function () {
              copyButton.textContent = COPIED;
              setTimeout(function () { copyButton.textContent = COPY_FIX; }, 1500);
            });
            return;
          }

          var applyButton = event.target.closest(".bugsage-apply-fix");
          if (applyButton) {
            event.preventDefault();
            var actionBar = applyButton.closest(".fix-actions");
            var fixText = selectedFixText(actionBar);
            var fixLocation = actionBar.dataset.location || "";
            if (!fixText) return;
            if (!window.confirm(CONFIRM_APPLY)) return;

            var originalText = applyButton.textContent;
            applyButton.disabled = true;
            applyButton.textContent = APPLYING;
            fetch("#{FixApplicator::ENDPOINT}", {
              method: "POST",
              headers: { "Content-Type": "application/json" },
              body: JSON.stringify({ location: fixLocation, fix: fixText })
            })
              .then(function (response) { return response.json(); })
              .then(function (result) {
                applyButton.disabled = false;
                applyButton.textContent = originalText;
                if (!result.ok) {
                  window.alert(result.error || COULD_NOT_APPLY);
                  return;
                }

                applyButton.textContent = APPLIED;
                setTimeout(function () { applyButton.textContent = originalText; }, 2000);
              })
              .catch(function (error) {
                applyButton.disabled = false;
                applyButton.textContent = originalText;
                window.alert(error.message);
              });
          }
        });

        document.addEventListener("click", function (event) {
          var fixItem = event.target.closest(".fixes li");
          if (!fixItem) return;

          var panel = fixItem.closest(".bug-detail, .container");
          if (!panel) return;

          panel.querySelectorAll(".fixes li").forEach(function (item) {
            item.classList.toggle("selected", item === fixItem);
          });
        });
      })();
    </script>
  HTML
end

.stylesObject



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
# File 'lib/bugsage/page_actions.rb', line 36

def styles
  <<~CSS
    .page-actions {
      display: flex;
      justify-content: flex-end;
      gap: 8px;
      margin-bottom: 16px;
    }
    .fix-actions {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-top: 12px;
    }
    .fix-actions.hidden {
      display: none;
    }
    .secondary-button {
      background: #45475a;
      color: #cdd6f4;
      border: 1px solid #585869;
      border-radius: 6px;
      padding: 8px 12px;
      font-family: inherit;
      font-size: 12px;
      font-weight: bold;
      cursor: pointer;
    }
    .secondary-button:hover {
      border-color: #89b4fa;
      color: #89b4fa;
    }
    .bugsage-clear-logs {
      background: rgba(243, 139, 168, 0.15);
      border-color: #f38ba8;
      color: #f5c2e7;
    }
  CSS
end