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

Compress URL params #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@uiw/react-codemirror": "^4.22.1",
"framer-motion": "^11.5.4",
"highlight.js": "^11.10.0",
"lz-string": "^1.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "^5.0.1",
Expand Down
25 changes: 17 additions & 8 deletions src/cljs/xt_play/app.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[xt-play.model.query :as query]
[xt-play.model.query-params :as query-params]
[xt-play.model.tx-batch :as tx-batch]
[xt-play.view :as view]))
[xt-play.view :as view]
["lz-string" :as lz-string]))

(glogi-console/install!)

Expand All @@ -17,26 +18,34 @@

;; 'my.app.thing :trace ;; Some namespaces you might want detailed logging

(defn- decompress-params [{:keys [uri-version] :as query-params}]
(let [decompress (case uri-version
Copy link
Contributor

Choose a reason for hiding this comment

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

It could be more intuitive to be an if if there's only one case with a default (I know, I'm a hypocrite)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I left it like this because I’ve changed the uri format enough times to know I’ll change it again 😅

"1" lz-string/decompressFromEncodedURIComponent
;; default
js/atob)
maybe-decompress #(when % (decompress %))]
(-> query-params
(update :txs maybe-decompress)
(update :query maybe-decompress))))

;; TODO: Special case existing txs
(defn- param-decode [s]
(let [txs (-> s js/atob js/JSON.parse (js->clj :keywordize-keys true))]
(defn- decode-txs [s]
(let [txs (-> s js/JSON.parse (js->clj :keywordize-keys true))]
(->> txs
(map #(update % :system-time (fn [d] (when d (js/Date. d))))))))

(rf/reg-event-fx
::init
[(rf/inject-cofx ::query-params/get)]
(fn [{:keys [query-params]} [_ xt-version]]
(let [{:keys [type txs query]} query-params
(let [{:keys [type txs query]} (decompress-params query-params)
type (if type (keyword type) :sql)]
{:db {:version xt-version
:type type
:query (if query
(js/atob query)
(query/default type))}
:query (or query (query/default type))}
:dispatch [::tx-batch/init
(if txs
(param-decode txs)
(decode-txs txs)
[(tx-batch/default type)])]})))

(defn ^:dev/after-load start! []
Expand Down
10 changes: 6 additions & 4 deletions src/cljs/xt_play/model/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[xt-play.model.href :as href]
[xt-play.model.query :as query]
[xt-play.model.query-params :as query-params]
[xt-play.model.tx-batch :as tx-batch]))
[xt-play.model.tx-batch :as tx-batch]
["lz-string" :as lz-string]))

(rf/reg-event-db
:hide-copy-tick
Expand All @@ -25,15 +26,16 @@
:-> :copy-tick)

(defn- param-encode [tx-batches]
(-> tx-batches clj->js js/JSON.stringify js/btoa))
(-> tx-batches clj->js js/JSON.stringify lz-string/compressToEncodedURIComponent))

(rf/reg-event-fx
:update-url
(fn [{:keys [db]} _]
{::query-params/set {:version (:version db)
{::query-params/set {:uri-version "1"
:version (:version db)
:type (name (:type db))
:txs (param-encode (tx-batch/batch-list db))
:query (js/btoa (:query db))}}))
:query (lz-string/compressToEncodedURIComponent (:query db))}}))

(rf/reg-event-fx
:dropdown-selection
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9527,6 +9527,15 @@ __metadata:
languageName: node
linkType: hard

"lz-string@npm:^1.5.0":
version: 1.5.0
resolution: "lz-string@npm:1.5.0"
bin:
lz-string: bin/bin.js
checksum: 1ee98b4580246fd90dd54da6e346fb1caefcf05f677c686d9af237a157fdea3fd7c83a4bc58f858cd5b10a34d27afe0fdcbd0505a47e0590726a873dc8b8f65d
languageName: node
linkType: hard

"magic-string@npm:^0.25.0, magic-string@npm:^0.25.7":
version: 0.25.9
resolution: "magic-string@npm:0.25.9"
Expand Down Expand Up @@ -14666,6 +14675,7 @@ __metadata:
"@uiw/react-codemirror": ^4.22.1
framer-motion: ^11.5.4
highlight.js: ^11.10.0
lz-string: ^1.5.0
react: ^18.3.1
react-dom: ^18.3.1
react-scripts: ^5.0.1
Expand Down
Loading