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

feat: permalink includes graph name and password #2187

Merged
merged 3 commits into from
May 9, 2022
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
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