Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: display an error when access to localStorage is blocked #130

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions frontend/src/app/contribute.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
["html-entities" :as html-entities]
[app.helpers :refer
[current-path
remove-trailing-slash]]
remove-trailing-slash
local-storage-enabled
local-storage-get
local-storage-error]]
[app.three-column-layout.core :refer
[three-column-layout
instructions-item
Expand Down Expand Up @@ -75,11 +78,15 @@
(set-atoms data)))))))

(defn fetch-logs-upload []
(let [data {:build_id_title "Upload"
:logs [{:name (.getItem js/localStorage "name")
:content (.getItem js/localStorage "content")}]}]

(set-atoms data)))
(if (local-storage-enabled)
(let [data {:build_id_title "Upload"
:logs [{:name (.getItem js/localStorage "name")
:content (.getItem js/localStorage "content")}]}]
(set-atoms data))
(do
(reset! status "error")
(reset! error-title (:title (local-storage-error)))
(reset! error-description (:description (local-storage-error))))))

(defn init-data []
(if (= (remove-trailing-slash (current-path)) "/contribute/upload")
Expand Down Expand Up @@ -169,7 +176,7 @@
[:input {:type "text"
:class "form-control"
:placeholder "Optional - Your FAS username"
:value (or @fas (.getItem js/localStorage "fas"))
:value (or @fas (local-storage-get "fas"))
:on-change #(on-change-fas %)}]]

[:label {:class "form-label"} "Interesting snippets:"]
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/app/contribute_events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[app.helpers :refer
[current-path
remove-trailing-slash
previous-siblings]]
previous-siblings
local-storage-enabled]]
[app.editor.core :refer [active-file]]
[app.contribute-logic :refer
[file-id
Expand Down Expand Up @@ -57,7 +58,7 @@
:container_file @container}]

;; Remember the username, so we can prefill it the next time
(when @fas
(when (and @fas (local-storage-enabled))
(.setItem js/localStorage "fas" @fas))

(reset! status "submitting")
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/app/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@
(if-not sibling
[]
(conj (previous-siblings sibling) sibling))))

(defn local-storage-enabled []
(try
(.getItem js/localStorage "isenabled") true
(catch js/DOMException _ false)))

(defn local-storage-get [name]
(when (local-storage-enabled)
(.getItem js/localStorage name)))

(defn local-storage-error []
{:title "Local storage is blocked by the browser"
:description
(str "Log Detective needs to upload the file to a localStorage in order "
"to work properly. Please check your browser privacy settings.")})
27 changes: 18 additions & 9 deletions frontend/src/app/homepage.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
[cljs.core.match :refer-macros [match]]
[app.homepage-validation :refer [validate]]
[lambdaisland.fetch :as fetch]
[app.components.jumbotron :refer [render-error]]
[app.helpers :refer
[current-path
local-storage-enabled
local-storage-error
remove-trailing-slash]]))

(defn current-hash []
Expand All @@ -17,6 +20,7 @@
(def backend-stats (r/atom nil))
(def current-hash-atom (r/atom (or (current-hash) "#copr")))
(def report-target (r/atom 1000))
(def error (r/atom nil))

(defn fetch-stats-backend []
(let [url (remove-trailing-slash (str "/stats" (current-path)))]
Expand Down Expand Up @@ -44,11 +48,14 @@

(defn on-submit-upload [event]
(.preventDefault event)
(.setItem js/localStorage "name" (get @input-values :name))
(.setItem js/localStorage "content" (get @input-values :file))
(validate current-hash-atom input-values input-errors)
(when (empty? @input-errors)
(set! (.-href (.-location js/window)) "/contribute/upload")))
(if-not (local-storage-enabled)
(reset! error (local-storage-error))
(do
(.setItem js/localStorage "name" (get @input-values :name))
(.setItem js/localStorage "content" (get @input-values :file))
(validate current-hash-atom input-values input-errors)
(when (empty? @input-errors)
(set! (.-href (.-location js/window)) "/contribute/upload")))))

(defn on-tab-click [href]
(reset! current-hash-atom href)
Expand Down Expand Up @@ -230,7 +237,9 @@
:else (render-copr-card)))

(defn homepage []
[:div {:class "card text-center"}
(render-stats)
(render-navigation)
(render-cards)])
(if @error
(render-error (:title @error) (:description @error))
[:div {:class "card text-center"}
(render-stats)
(render-navigation)
(render-cards)]))
Loading