♻️ Refactor flags handling on frontend.

This commit is contained in:
Andrey Antukh 2021-08-27 13:19:36 +02:00
parent c9985121c4
commit e587179359
9 changed files with 78 additions and 41 deletions

View file

@ -0,0 +1,32 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) UXBOX Labs SL
(ns app.common.flags
"Flags parsing algorithm."
(:require
[cuerdas.core :as str]))
(defn parse
[default flags]
(loop [flags (seq flags)
result default]
(let [item (first flags)]
(if (nil? item)
result
(let [sname (name item)]
(cond
(str/starts-with? sname "enable-")
(recur (rest flags)
(conj result (keyword (subs sname 7))))
(str/starts-with? sname "disable-")
(recur (rest flags)
(disj result (keyword (subs sname 8))))
:else
(recur (rest flags) result)))))))

View file

@ -9,6 +9,7 @@
[app.common.spec :as us] [app.common.spec :as us]
[app.common.uri :as u] [app.common.uri :as u]
[app.common.version :as v] [app.common.version :as v]
[app.common.flags :as flags]
[app.util.avatars :as avatars] [app.util.avatars :as avatars]
[app.util.dom :as dom] [app.util.dom :as dom]
[app.util.globals :refer [global location]] [app.util.globals :refer [global location]]
@ -53,10 +54,14 @@
:browser :browser
:webworker)) :webworker))
(def default-flags
#{:registration})
(defn- parse-flags (defn- parse-flags
[global] [global]
(let [flags (obj/get global "penpotFlags" "")] (let [flags (obj/get global "penpotFlags" "")
(into #{} (map keyword) (str/words flags)))) flags (into #{} (map keyword) (str/words flags))]
(flags/parse default-flags flags)))
(defn- parse-version (defn- parse-version
[global] [global]
@ -68,26 +73,27 @@
(def default-theme "default") (def default-theme "default")
(def default-language "en") (def default-language "en")
(def demo-warning (obj/get global "penpotDemoWarning" false))
(def feedback-enabled (obj/get global "penpotFeedbackEnabled" false))
(def allow-demo-users (obj/get global "penpotAllowDemoUsers" true))
(def google-client-id (obj/get global "penpotGoogleClientID" nil)) (def google-client-id (obj/get global "penpotGoogleClientID" nil))
(def gitlab-client-id (obj/get global "penpotGitlabClientID" nil)) (def gitlab-client-id (obj/get global "penpotGitlabClientID" nil))
(def github-client-id (obj/get global "penpotGithubClientID" nil)) (def github-client-id (obj/get global "penpotGithubClientID" nil))
(def oidc-client-id (obj/get global "penpotOIDCClientID" nil)) (def oidc-client-id (obj/get global "penpotOIDCClientID" nil))
(def login-with-ldap (obj/get global "penpotLoginWithLDAP" false))
(def registration-enabled (obj/get global "penpotRegistrationEnabled" true))
(def worker-uri (obj/get global "penpotWorkerURI" "/js/worker.js")) (def worker-uri (obj/get global "penpotWorkerURI" "/js/worker.js"))
(def translations (obj/get global "penpotTranslations")) (def translations (obj/get global "penpotTranslations"))
(def themes (obj/get global "penpotThemes")) (def themes (obj/get global "penpotThemes"))
(def analytics (obj/get global "penpotAnalyticsEnabled" false))
(def flags (delay (parse-flags global))) (def flags (atom (parse-flags global)))
(def version (atom (parse-version global)))
(def target (atom (parse-target global)))
(def browser (atom (parse-browser)))
(def platform (atom (parse-platform)))
(def version (delay (parse-version global))) ;; mantain for backward compatibility
(def target (delay (parse-target global))) (let [login-with-ldap (obj/get global "penpotLoginWithLDAP" false)
(def browser (delay (parse-browser))) registration (obj/get global "penpotRegistrationEnabled" true)]
(def platform (delay (parse-platform))) (when login-with-ldap
(swap! flags conj :login-with-ldap))
(when (false? registration)
(swap! flags disj :registration)))
(def public-uri (def public-uri
(let [uri (u/uri (or (obj/get global "penpotPublicURI") (let [uri (u/uri (or (obj/get global "penpotPublicURI")

View file

@ -242,6 +242,6 @@
(defmethod ptk/resolve ::initialize (defmethod ptk/resolve ::initialize
[_ params] [_ params]
(if cf/analytics (if (contains? @cf/flags :audit-log)
(initialize) (initialize)
(ptk/data-event ::initialize params))) (ptk/data-event ::initialize params)))

View file

@ -55,11 +55,11 @@
(def routes (def routes
[["/auth" [["/auth"
["/login" :auth-login] ["/login" :auth-login]
(when cf/registration-enabled (when (contains? @cf/flags :registration)
["/register" :auth-register]) ["/register" :auth-register])
(when cf/registration-enabled (when (contains? @cf/flags :registration)
["/register/validate" :auth-register-validate]) ["/register/validate" :auth-register-validate])
(when cf/registration-enabled (when (contains? @cf/flags :registration)
["/register/success" :auth-register-success]) ["/register/success" :auth-register-success])
["/recovery/request" :auth-recovery-request] ["/recovery/request" :auth-recovery-request]
["/recovery" :auth-recovery] ["/recovery" :auth-recovery]

View file

@ -7,7 +7,7 @@
(ns app.main.ui.auth.login (ns app.main.ui.auth.login
(:require (:require
[app.common.spec :as us] [app.common.spec :as us]
[app.config :as cfg] [app.config :as cf]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.data.users :as du] [app.main.data.users :as du]
[app.main.repo :as rp] [app.main.repo :as rp]
@ -23,10 +23,10 @@
[rumext.alpha :as mf])) [rumext.alpha :as mf]))
(def show-alt-login-buttons? (def show-alt-login-buttons?
(or cfg/google-client-id (or cf/google-client-id
cfg/gitlab-client-id cf/gitlab-client-id
cfg/github-client-id cf/github-client-id
cfg/oidc-client-id)) cf/oidc-client-id))
(s/def ::email ::us/email) (s/def ::email ::us/email)
(s/def ::password ::us/not-empty-string) (s/def ::password ::us/not-empty-string)
@ -113,7 +113,7 @@
[:& fm/submit-button [:& fm/submit-button
{:label (tr "auth.login-submit")}] {:label (tr "auth.login-submit")}]
(when cfg/login-with-ldap (when (contains? @cf/flags :login-with-ldap)
[:& fm/submit-button [:& fm/submit-button
{:label (tr "auth.login-with-ldap-submit") {:label (tr "auth.login-with-ldap-submit")
:on-click on-submit-ldap}])]]])) :on-click on-submit-ldap}])]]]))
@ -121,26 +121,26 @@
(mf/defc login-buttons (mf/defc login-buttons
[{:keys [params] :as props}] [{:keys [params] :as props}]
[:div.auth-buttons [:div.auth-buttons
(when cfg/google-client-id (when cf/google-client-id
[:a.btn-ocean.btn-large.btn-google-auth [:a.btn-ocean.btn-large.btn-google-auth
{:on-click #(login-with-oauth % :google params)} {:on-click #(login-with-oauth % :google params)}
(tr "auth.login-with-google-submit")]) (tr "auth.login-with-google-submit")])
(when cfg/gitlab-client-id (when cf/gitlab-client-id
[:a.btn-ocean.btn-large.btn-gitlab-auth [:a.btn-ocean.btn-large.btn-gitlab-auth
{:on-click #(login-with-oauth % :gitlab params)} {:on-click #(login-with-oauth % :gitlab params)}
[:img.logo [:img.logo
{:src "/images/icons/brand-gitlab.svg"}] {:src "/images/icons/brand-gitlab.svg"}]
(tr "auth.login-with-gitlab-submit")]) (tr "auth.login-with-gitlab-submit")])
(when cfg/github-client-id (when cf/github-client-id
[:a.btn-ocean.btn-large.btn-github-auth [:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :github params)} {:on-click #(login-with-oauth % :github params)}
[:img.logo [:img.logo
{:src "/images/icons/brand-github.svg"}] {:src "/images/icons/brand-github.svg"}]
(tr "auth.login-with-github-submit")]) (tr "auth.login-with-github-submit")])
(when cfg/oidc-client-id (when cf/oidc-client-id
[:a.btn-ocean.btn-large.btn-github-auth [:a.btn-ocean.btn-large.btn-github-auth
{:on-click #(login-with-oauth % :oidc params)} {:on-click #(login-with-oauth % :oidc params)}
(tr "auth.login-with-oidc-submit")])]) (tr "auth.login-with-oidc-submit")])])
@ -166,14 +166,13 @@
[:a {:on-click #(st/emit! (rt/nav :auth-recovery-request))} [:a {:on-click #(st/emit! (rt/nav :auth-recovery-request))}
(tr "auth.forgot-password")]] (tr "auth.forgot-password")]]
(when cfg/registration-enabled (when (contains? @cf/flags :registration)
[:div.link-entry [:div.link-entry
[:span (tr "auth.register") " "] [:span (tr "auth.register") " "]
[:a {:on-click #(st/emit! (rt/nav :auth-register {} params))} [:a {:on-click #(st/emit! (rt/nav :auth-register {} params))}
(tr "auth.register-submit")]])] (tr "auth.register-submit")]])]
(when (contains? @cf/flags :demo-users)
(when cfg/allow-demo-users
[:div.links.demo [:div.links.demo
[:div.link-entry [:div.link-entry
[:span (tr "auth.create-demo-profile") " "] [:span (tr "auth.create-demo-profile") " "]

View file

@ -116,7 +116,7 @@
[:h1 (tr "auth.register-title")] [:h1 (tr "auth.register-title")]
[:div.subtitle (tr "auth.register-subtitle")] [:div.subtitle (tr "auth.register-subtitle")]
(when cf/demo-warning (when (contains? @cf/flags :demo-warning)
[:& demo-warning]) [:& demo-warning])
[:& register-form {:params params}] [:& register-form {:params params}]
@ -135,7 +135,7 @@
:tab-index "4"} :tab-index "4"}
(tr "auth.login-here")]] (tr "auth.login-here")]]
(when cf/allow-demo-users (when (contains? @cf/flags :demo-users)
[:div.link-entry [:div.link-entry
[:span (tr "auth.create-demo-profile") " "] [:span (tr "auth.create-demo-profile") " "]
[:a {:on-click #(st/emit! (du/create-demo-profile)) [:a {:on-click #(st/emit! (du/create-demo-profile))
@ -216,7 +216,7 @@
:label (tr "auth.terms-privacy-agreement") :label (tr "auth.terms-privacy-agreement")
:type "checkbox"}]] :type "checkbox"}]]
(when (contains? @cf/flags :show-newsletter-check-on-register-validation) (when (contains? @cf/flags :newsletter-registration-check)
[:div.fields-row [:div.fields-row
[:& fm/input {:name :accept-newsletter-subscription [:& fm/input {:name :accept-newsletter-subscription
:class "check-primary" :class "check-primary"

View file

@ -8,7 +8,7 @@
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.spec :as us] [app.common.spec :as us]
[app.config :as cfg] [app.config :as cf]
[app.main.data.dashboard :as dd] [app.main.data.dashboard :as dd]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.data.modal :as modal] [app.main.data.modal :as modal]
@ -214,7 +214,7 @@
[:* {:key (:id team)} [:* {:key (:id team)}
[:li.team-name {:on-click (partial team-selected (:id team))} [:li.team-name {:on-click (partial team-selected (:id team))}
[:span.team-icon [:span.team-icon
[:img {:src (cfg/resolve-team-photo-url team)}]] [:img {:src (cf/resolve-team-photo-url team)}]]
[:span.team-text {:title (:name team)} (:name team)]]]) [:span.team-text {:title (:name team)} (:name team)]]])
[:hr] [:hr]
@ -358,7 +358,7 @@
[:span.team-text (tr "dashboard.default-team-name")]] [:span.team-text (tr "dashboard.default-team-name")]]
[:div.team-name [:div.team-name
[:span.team-icon [:span.team-icon
[:img {:src (cfg/resolve-team-photo-url team)}]] [:img {:src (cf/resolve-team-photo-url team)}]]
[:span.team-text {:title (:name team)} (:name team)]]) [:span.team-text {:title (:name team)} (:name team)]])
[:span.switch-icon [:span.switch-icon
@ -468,7 +468,7 @@
(mf/defc profile-section (mf/defc profile-section
[{:keys [profile team] :as props}] [{:keys [profile team] :as props}]
(let [show (mf/use-state false) (let [show (mf/use-state false)
photo (cfg/resolve-profile-photo-url profile) photo (cf/resolve-profile-photo-url profile)
on-click on-click
(mf/use-callback (mf/use-callback
@ -496,7 +496,7 @@
[:span.icon i/exit] [:span.icon i/exit]
[:span.text (tr "labels.logout")]] [:span.text (tr "labels.logout")]]
(when cfg/feedback-enabled (when (contains? @cf/flags :user-feedback)
[:li.feedback {:on-click (partial on-click :settings-feedback)} [:li.feedback {:on-click (partial on-click :settings-feedback)}
[:span.icon i/msg-info] [:span.icon i/msg-info]
[:span.text (tr "labels.give-feedback")] [:span.text (tr "labels.give-feedback")]

View file

@ -87,7 +87,7 @@
i/pencil i/pencil
[:span.element-title (tr "labels.release-notes")]] [:span.element-title (tr "labels.release-notes")]]
(when cf/feedback-enabled (when (contains? @cf/flags :user-feedback)
[:li {:class (when feedback? "current") [:li {:class (when feedback? "current")
:on-click go-settings-feedback} :on-click go-settings-feedback}
i/msg-info i/msg-info

View file

@ -8,7 +8,7 @@
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.math :as mth] [app.common.math :as mth]
[app.config :as cfg] [app.config :as cf]
[app.main.data.modal :as modal] [app.main.data.modal :as modal]
[app.main.data.workspace :as dw] [app.main.data.workspace :as dw]
[app.main.data.workspace.shortcuts :as sc] [app.main.data.workspace.shortcuts :as sc]
@ -259,7 +259,7 @@
[:li.export-file {:on-click on-export-files} [:li.export-file {:on-click on-export-files}
[:span (tr "dashboard.export-single")]] [:span (tr "dashboard.export-single")]]
(when cfg/feedback-enabled (when (contains? @cf/flags :user-feedback)
[:li.feedback {:on-click (st/emitf (rt/nav :settings-feedback))} [:li.feedback {:on-click (st/emitf (rt/nav :settings-feedback))}
[:span (tr "labels.give-feedback")] [:span (tr "labels.give-feedback")]
[:span.primary-badge "ALPHA"]]) [:span.primary-badge "ALPHA"]])