Skip to content

Commit

Permalink
Merge pull request #2187 from filipesilva/permalink-name-pw
Browse files Browse the repository at this point in the history
feat: permalink includes graph name and password
  • Loading branch information
filipesilva authored May 9, 2022
2 parents 2be292c + 6351d1d commit 9308b12
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/cljs/athens/electron/boot.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
[(rf/inject-cofx :local-storage :athens/persist)]
(fn [{:keys [local-storage]} [_ first-boot?]]
(let [boot-tx (sentry/transaction-start "boot-sequence")
id-param (router/consume-graph-param)
param-db (when-let [graph-params (router/consume-graph-params)]
(apply utils/self-hosted-db graph-params))
init-app-db (cond->
;; Init it from local storage.
(wrap-span-no-new-tx "db/init-app-db" (db/init-app-db local-storage))
;; Select the db in id-param if there.
id-param (db-picker/add-and-select (utils/self-hosted-db id-param id-param "")))
param-db (db-picker/add-and-select param-db))
all-dbs (db-picker/all-dbs init-app-db)
selected-db (db-picker/selected-db init-app-db)
default-db (utils/get-default-db)
Expand Down
50 changes: 29 additions & 21 deletions src/cljs/athens/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -253,30 +253,38 @@

;; Permalink param processing

(def graph-param-key "graph")
(def graph-name-param-key "graph-name")
(def graph-url-param-key "graph-url")
(def graph-password-param-key "graph-password")


(defn consume-graph-param
"Removes and returns the graph-id in the current URL, if any."
(defn consume-graph-params
"Removes and returns the graph params in the current URL, if any."
[]
;; Note: don't use the reitit.frontend functions here, as the router
;; it not yet initialized during boot.
(let [url (js/URL. js/window.location)
graph-id (.. url -searchParams (get graph-param-key))]
;; Replace history with a version without the graph param.
(.. url -searchParams (delete graph-param-key))
(js/history.replaceState js/history.state nil url)
graph-id))


(defn create-url-with-graph-param
(let [window-url (js/URL. js/window.location)
name (.. window-url -searchParams (get graph-name-param-key))
url (.. window-url -searchParams (get graph-url-param-key))
password (js/atob (.. window-url -searchParams (get graph-password-param-key)))]
(when url
;; Replace history with a version without the graph params.
(.. window-url -searchParams (delete graph-name-param-key))
(.. window-url -searchParams (delete graph-url-param-key))
(.. window-url -searchParams (delete graph-password-param-key))
(js/history.replaceState js/history.state nil window-url)
[(or name url) url password])))


(defn create-url-with-graph-params
"Create a URL containing graph-id."
[graph-id]
(let [url (js/URL. (if electron.utils/electron?
;; Use live web client + page route on electron.
(str "https://web.athensresearch.org/"
js/window.location.hash)
js/window.location))]
(.. url -searchParams (set graph-param-key graph-id))
(.toString url)))

[name url password]
(let [created-url (js/URL. (if electron.utils/electron?
;; Use live web client + page route on electron.
(str "https://web.athensresearch.org/"
js/window.location.hash)
js/window.location))]
(.. created-url -searchParams (set graph-name-param-key name))
(.. created-url -searchParams (set graph-url-param-key url))
(.. created-url -searchParams (set graph-password-param-key (js/btoa password)))
(.toString created-url)))
6 changes: 3 additions & 3 deletions src/cljs/athens/self_hosted/presence/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

(defn copy-permalink
[]
(let [selected-db @(rf/subscribe [:db-picker/selected-db])
url (router/create-url-with-graph-param (:id selected-db))]
(.. js/navigator -clipboard (writeText url))
(let [{:keys [name url password]} @(rf/subscribe [:db-picker/selected-db])
created-url (router/create-url-with-graph-params name url password)]
(.. js/navigator -clipboard (writeText created-url))
(util/toast (clj->js {:status "info"
:position "top-right"
:title "Copied permalink to clipboard"}))))
Expand Down

1 comment on commit 9308b12

@vercel
Copy link

@vercel vercel bot commented on 9308b12 May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.