bug_report.cljs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. [frontend.context.i18n :refer [t]]))
  10. (defn parse-clipboard-data-transfer
  11. "parse dataTransfer
  12. input: dataTransfer
  13. output: {:types {:type :data} :items {:kind :type} :files {:name :size :type}}"
  14. [data]
  15. (let [items (.-items data)
  16. types (.-types data)
  17. files (.-files data)]
  18. (conj
  19. {:items (->> items
  20. (map (fn [item] {:kind (.-kind item) :type (.-type item)}))
  21. (conj))}
  22. {:types (->> types
  23. (map (fn [type] {:type type :data (.getData data type)}))
  24. (conj))}
  25. {:files (->> files
  26. (map (fn [file] {:name (.-name file) :type (.-type file) :size (.-size file)}))
  27. (conj))})))
  28. (rum/defc clipboard-data-inspector
  29. "bug report tool for clipboard"
  30. []
  31. (let [[result set-result!] (rum/use-state {})
  32. [step set-step!] (rum/use-state 0)
  33. paste-handler! (fn [e]
  34. (let [clipboard-data (.-clipboardData e)
  35. result (parse-clipboard-data-transfer clipboard-data)
  36. result (into {} result)]
  37. (set-result! result)
  38. (set-step! 1)))
  39. copy-result-to-clipboard! (fn [result]
  40. (util/copy-to-clipboard! result)
  41. (notification/show! (t :bug-report/inspector-page-copy-notif)))
  42. reset-step! (fn []
  43. (set-step! 0)
  44. (set-result! {}))]
  45. (rum/use-effect!
  46. (fn []
  47. (cond (= step 0) (js/addEventListener "paste" paste-handler!))
  48. (fn [] (cond (= step 0) (js/removeEventListener "paste" paste-handler!))))
  49. [step]) ;; when step === 0
  50. [:div.flex.flex-col
  51. (when (= step 0)
  52. (list [:div.mx-auto (t :bug-report/inspector-page-desc-1)]
  53. [:div.mx-auto (t :bug-report/inspector-page-desc-2)]
  54. ;; for mobile
  55. [:input.form-input.is-large.transition.duration-150.ease-in-out {:type "text" :placeholder (t :bug-report/inspector-page-placeholder)}]
  56. [:div.flex.justify-between.items-center.mt-2
  57. [:div (t :bug-report/inspector-page-tip)]
  58. (ui/button (t :bug-report/inspector-page-btn-back) :on-click #(util/open-url (rfe/href :bug-report)))]))
  59. (when (= step 1)
  60. (list
  61. [:div (t :bug-report/inspector-page-desc-clipboard)]
  62. [:div.flex.justify-between.items-center.mt-2
  63. [:div (t :bug-report/inspector-page-desc-copy)]
  64. (ui/button (t :bug-report/inspector-page-btn-copy) :on-click #(copy-result-to-clipboard! (js/JSON.stringify (clj->js result) nil 2)))]
  65. [:div.flex.justify-between.items-center.mt-2
  66. [:div (t :bug-report/inspector-page-desc-create-issue)]
  67. (ui/button (t :bug-report/inspector-page-btn-create-issue) :href (header/bug-report-url))]
  68. [:div.flex.justify-between.items-center.mt-2
  69. [:div (t :bug-report/inspector-page-tip)]
  70. (ui/button (t :bug-report/inspector-page-btn-back) :on-click reset-step!)]
  71. [:pre.whitespace-pre-wrap [:code (js/JSON.stringify (clj->js result) nil 2)]]))]))
  72. (rum/defc bug-report-tool-route
  73. [route-match]
  74. (let [name (get-in route-match [:parameters :path :tool])]
  75. [:div.flex.flex-col ;; container
  76. (cond
  77. (= name "clipboard-data-inspector")
  78. [:h1.text-2xl.mx-auto.mb-4 (ui/icon "clipboard") " " (-> (t :bug-report/clipboard-inspector-title) (string/capitalize))])
  79. (cond
  80. (= name "clipboard-data-inspector")
  81. (clipboard-data-inspector))]))
  82. (rum/defc report-item-button
  83. [title description icon-name {:keys [on-click]}]
  84. [:a.cp__bug-report-item-button.flex.items-center.px-4.py-2.my-2.rounded-lg {:on-click on-click}
  85. [(ui/icon icon-name)
  86. [:div.flex.flex-col.ml-2
  87. [:div title]
  88. [:div.opacity-60 description]]]])
  89. (rum/defc bug-report
  90. []
  91. [:div.flex.flex-col
  92. [:div.flex.flex-col.items-center
  93. [:div.flex.items-center.mb-2
  94. (ui/icon "bug")
  95. [:h1.text-3xl.ml-2 (t :bug-report/main-title)]]
  96. [:div.opacity-60 (t :bug-report/main-desc)]]
  97. [:div.cp__bug-report-reporter.rounded-lg.p-8.mt-8
  98. [:h1.text-2xl (t :bug-report/section-clipboard-title)]
  99. [:div.opacity-60 (t :bug-report/section-clipboard-desc)]
  100. (report-item-button (t :bug-report/section-clipboard-btn-title)
  101. (t :bug-report/section-clipboard-btn-desc)
  102. "clipboard"
  103. {:on-click #(util/open-url (rfe/href :bug-report-tools {:tool "clipboard-data-inspector"}))})
  104. [:div.py-2] ;; divider
  105. [:div.flex.flex-col
  106. [:h1.text-2xl (t :bug-report/section-issues-title)]
  107. [:div.opacity-60 (t :bug-report/section-issues-desc)]
  108. (report-item-button (t :bug-report/section-issues-btn-title) (t :bug-report/section-issues-btn-desc) "message-report" {:on-click #(util/open-url (header/bug-report-url))})]]])