bug_report.cljs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (ns frontend.components.bug-report
  2. (:require [rum.core :as rum]
  3. [frontend.ui :as ui]
  4. [frontend.components.header :as header]
  5. [frontend.util :as util]
  6. [reitit.frontend.easy :as rfe]
  7. [clojure.string :as string]
  8. [frontend.handler.notification :as notification]))
  9. (defn parse-clipboard-data-transfer
  10. "parse dataTransfer
  11. input: dataTransfer
  12. output: {:types {:type :data} :items {:kind :type} :files {:name :size :type}}"
  13. [data]
  14. (let [items (.-items data)
  15. types (.-types data)
  16. files (.-files data)]
  17. (conj
  18. {:items (->> items
  19. (map (fn [item] {:kind (.-kind item) :type (.-type item)}))
  20. (conj))}
  21. {:types (->> types
  22. (map (fn [type] {:type type :data (.getData data type)}))
  23. (conj))}
  24. {:files (->> files
  25. (map (fn [file] {:name (.-name file) :type (.-type file) :size (.-size file)}))
  26. (conj))})))
  27. (rum/defc clipboard-data-inspector
  28. "bug report tool for clipboard"
  29. []
  30. (let [[result set-result!] (rum/use-state {})
  31. [step set-step!] (rum/use-state 0)
  32. paste-handler! (fn [e]
  33. (let [clipboard-data (.-clipboardData e)
  34. result (parse-clipboard-data-transfer clipboard-data)
  35. result (into {} result)]
  36. (set-result! result)
  37. (set-step! 1)))
  38. copy-result-to-clipboard! (fn [result]
  39. (util/copy-to-clipboard! result)
  40. (notification/show! "Copied to clipboard!"))
  41. reset-step! (fn []
  42. (set-step! 0)
  43. (set-result! {}))]
  44. (rum/use-effect!
  45. (fn []
  46. (cond (= step 0) (js/addEventListener "paste" paste-handler!))
  47. (fn [] (cond (= step 0) (js/removeEventListener "paste" paste-handler!))))
  48. [step]) ;; when step === 0
  49. [:div.flex.flex-col
  50. (when (= step 0)
  51. (list [:div.mx-auto "Press Ctrl+V / ⌘+V to inspect your clipboard data"]
  52. [:div.mx-auto "or click here to paste if you are using the mobile version"]
  53. ;; for mobile
  54. [:input.form-input.is-large.transition.duration-150.ease-in-out {:type "text" :placeholder "Long press here to paste if you are on mobile"}]
  55. [:div.flex.justify-between.items-center.mt-2
  56. [:div "Something wrong? No problem, click to go back to the previous step."]
  57. (ui/button "Go back" :on-click #(util/open-url (rfe/href :bug-report)))]))
  58. (when (= step 1)
  59. (list
  60. [:div "Here is the data read from clipboard."]
  61. [:div.flex.justify-between.items-center.mt-2
  62. [:div "If this is okay to share, click the copy button."]
  63. (ui/button "Copy the result" :on-click #(copy-result-to-clipboard! (js/JSON.stringify (clj->js result) nil 2)))]
  64. [:div.flex.justify-between.items-center.mt-2
  65. [:div "Now you can report the result pasted to your clipboard. Please paste the result in the 'Additional Context' section and state where you copied the original content from. Thanks!"]
  66. (ui/button "Create an issue" :href header/bug-report-url)]
  67. [:div.flex.justify-between.items-center.mt-2
  68. [:div "Something wrong? No problem, click to go back to the previous step."]
  69. (ui/button "Go back" :on-click reset-step!)]
  70. [:pre.whitespace-pre-wrap [:code (js/JSON.stringify (clj->js result) nil 2)]]))]))
  71. (rum/defc bug-report-tool-route
  72. [route-match]
  73. (let [name (get-in route-match [:parameters :path :tool])]
  74. [:div.flex.flex-col ;; container
  75. [:h1.text-2xl.mx-auto.mb-4 (ui/icon "clipboard") " " (-> name (string/replace #"-" " ") (string/capitalize))]
  76. (cond ;; TODO any fallback?
  77. (= name "clipboard-data-inspector")
  78. (clipboard-data-inspector))]))
  79. (rum/defc report-item-button
  80. [title description icon-name {:keys [on-click]}]
  81. [:a.cp__bug-report-item-button.flex.items-center.px-4.py-2.my-2.rounded-lg {:on-click on-click}
  82. [(ui/icon icon-name)
  83. [:div.flex.flex-col.ml-2
  84. [:div title]
  85. [:div.opacity-60 description]]]])
  86. (rum/defc bug-report
  87. []
  88. [:div.flex.flex-col
  89. [:div.flex.flex-col.items-center
  90. [:div.flex.items-center.mb-2
  91. (ui/icon "bug")
  92. [:h1.text-3xl.ml-2 "Bug report"]]
  93. [:div.opacity-60 "Can you help us out by submitting a bug report? We'll get it sorted out as soon as we can."]]
  94. [:div.cp__bug-report-reporter.rounded-lg.p-8.mt-8
  95. [:h1.text-2xl "Is the bug you encountered related to these features?"]
  96. [:div.opacity-60 "You can use these handy tools to give us additional information."]
  97. (report-item-button "Clipboard helper"
  98. "Inspect and collect clipboard data"
  99. "clipboard"
  100. {:on-click #(util/open-url (rfe/href :bug-report-tools {:tool "clipboard-data-inspector"}))})
  101. [:div.py-2] ;; divider
  102. [:div.flex.flex-col
  103. [:h1.text-2xl "Or..."]
  104. [:div.opacity-60 "If there are no tools available for you to gather additional information, please report the bug directly."]
  105. (report-item-button "Submit a bug report" "Help Make Logseq Better!" "message-report" {:on-click #(util/open-url header/bug-report-url)})]]])