mirror of
https://github.com/penpot/penpot.git
synced 2025-08-06 08:58:34 +02:00
♻️ Refactor routes
This commit is contained in:
parent
21cdf8b0ae
commit
f08894629d
9 changed files with 153 additions and 138 deletions
|
@ -7,7 +7,6 @@
|
|||
(ns app.main
|
||||
(:require
|
||||
[app.common.logging :as log]
|
||||
[app.common.spec :as us]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cf]
|
||||
[app.main.data.events :as ev]
|
||||
|
@ -18,14 +17,12 @@
|
|||
[app.main.ui :as ui]
|
||||
[app.main.ui.confirm]
|
||||
[app.main.ui.modal :refer [modal]]
|
||||
[app.main.ui.routes :as rt]
|
||||
[app.main.worker]
|
||||
[app.util.dom :as dom]
|
||||
[app.util.i18n :as i18n]
|
||||
[app.util.router :as rt]
|
||||
[app.util.storage :refer [storage]]
|
||||
[app.util.theme :as theme]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
[potok.core :as ptk]
|
||||
[rumext.alpha :as mf]))
|
||||
|
||||
|
@ -33,79 +30,32 @@
|
|||
(log/set-level! :root :warn)
|
||||
(log/set-level! :app :info)
|
||||
|
||||
|
||||
(when (= :browser @cf/target)
|
||||
(log/info :message "wecome to penpot" :version (:full @cf/version) :public-uri (str cf/public-uri)))
|
||||
|
||||
(log/info :message "Welcome to penpot" :version (:full @cf/version) :public-uri (str cf/public-uri)))
|
||||
|
||||
(declare reinit)
|
||||
|
||||
(s/def ::any any?)
|
||||
|
||||
(defn match-path
|
||||
[router path]
|
||||
(when-let [match (rt/match router path)]
|
||||
(if-let [conform (get-in match [:data :conform])]
|
||||
(let [spath (get conform :path-params ::any)
|
||||
squery (get conform :query-params ::any)]
|
||||
(try
|
||||
(-> (dissoc match :params)
|
||||
(assoc :path-params (us/conform spath (get match :path-params))
|
||||
:query-params (us/conform squery (get match :query-params))))
|
||||
(catch :default _
|
||||
nil)))
|
||||
match)))
|
||||
|
||||
(defn on-navigate
|
||||
[router path]
|
||||
(let [match (match-path router path)
|
||||
profile (:profile @storage)
|
||||
nopath? (or (= path "") (= path "/"))
|
||||
authed? (and (not (nil? profile))
|
||||
(not= (:id profile) uuid/zero))]
|
||||
|
||||
(cond
|
||||
(and nopath? authed? (nil? match))
|
||||
(if (not= uuid/zero profile)
|
||||
(st/emit! (rt/nav :dashboard-projects {:team-id (du/get-current-team-id profile)}))
|
||||
(st/emit! (rt/nav :auth-login)))
|
||||
|
||||
(and (not authed?) (nil? match))
|
||||
(st/emit! (rt/nav :auth-login))
|
||||
|
||||
(nil? match)
|
||||
(st/emit! (rt/assign-exception {:type :not-found}))
|
||||
|
||||
:else
|
||||
(st/emit! (rt/navigated match)))))
|
||||
|
||||
(defn init-ui
|
||||
[]
|
||||
(mf/mount (mf/element ui/app) (dom/get-element "app"))
|
||||
(mf/mount (mf/element modal) (dom/get-element "modal")))
|
||||
|
||||
|
||||
(defn initialize
|
||||
[]
|
||||
(letfn [(on-profile [_profile]
|
||||
(rx/of (rt/initialize-router ui/routes)
|
||||
(rt/initialize-history on-navigate)))]
|
||||
(ptk/reify ::initialize
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(assoc state :session-id (uuid/next)))
|
||||
(ptk/reify ::initialize
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(assoc state :session-id (uuid/next)))
|
||||
|
||||
ptk/WatchEvent
|
||||
(watch [_ _ stream]
|
||||
(rx/merge
|
||||
(rx/of
|
||||
(ptk/event ::ev/initialize)
|
||||
(du/initialize-profile))
|
||||
(->> stream
|
||||
(rx/filter (ptk/type? ::du/profile-fetched))
|
||||
(rx/take 1)
|
||||
(rx/map deref)
|
||||
(rx/mapcat on-profile)))))))
|
||||
ptk/WatchEvent
|
||||
(watch [_ _ stream]
|
||||
(rx/merge
|
||||
(rx/of (ptk/event ::ev/initialize)
|
||||
(du/initialize-profile))
|
||||
(->> stream
|
||||
(rx/filter du/profile-fetched?)
|
||||
(rx/take 1)
|
||||
(rx/map #(rt/init-routes)))))))
|
||||
|
||||
(defn ^:export init
|
||||
[]
|
||||
|
@ -121,11 +71,14 @@
|
|||
(mf/unmount (dom/get-element "modal"))
|
||||
(init-ui))
|
||||
|
||||
(add-watch i18n/locale "locale" (fn [_ _ o v]
|
||||
(when (not= o v)
|
||||
(reinit))))
|
||||
|
||||
(defn ^:dev/after-load after-load
|
||||
[]
|
||||
(reinit))
|
||||
|
||||
;; Reload the UI when the language changes
|
||||
(add-watch
|
||||
i18n/locale "locale"
|
||||
(fn [_ _ old-value current-value]
|
||||
(when (not= old-value current-value)
|
||||
(reinit))))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue