From fe94847fd908a6a3e8590b46c0953178ae6b2f6c Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 13 Apr 2020 10:05:35 +0800 Subject: [PATCH] Change latest commit hash after push successfully --- web/src/main/frontend/handler.cljs | 41 +++++++++++-- web/src/main/frontend/state.cljs | 2 +- web/src/main/frontend/ui.cljs | 99 +++++++++++++++--------------- 3 files changed, 86 insertions(+), 56 deletions(-) diff --git a/web/src/main/frontend/handler.cljs b/web/src/main/frontend/handler.cljs index 30eb4ef16..434496f2d 100644 --- a/web/src/main/frontend/handler.cljs +++ b/web/src/main/frontend/handler.cljs @@ -24,7 +24,7 @@ (:import [goog.events EventHandler])) ;; TODO: replace all util/p-handle with p/let - +;; TODO: separate git status for push-failed, pull-failed, etc (defn set-state-kv! [key value] (swap! state/state assoc key value)) @@ -184,18 +184,24 @@ (set-latest-journals!))))) (defn show-notification! - [text status] + [content status] (swap! state/state assoc :notification/show? true - :notification/text text + :notification/content content :notification/status status) (when-not (= status :error) (js/setTimeout #(swap! state/state assoc :notification/show? false - :notification/text nil + :notification/content nil :notification/status nil) 5000))) +(defn- clear-storage + [] + (p/let [_idb-clear (js/window.pfs._idb.wipe)] + (js/localStorage.clear) + (set! (.-href js/window.location) "/"))) + (defn pull [repo-url token] (when (and @@ -215,7 +221,17 @@ (p/then (fn [] (load-db-and-journals! repo-url @remote-changed? false))) (p/catch (fn [_error] - (show-notification! "Merges with conflicts are not supported yet, please save your local changes, logout and login again." :error)))))))) + (show-notification! + [:p.content + "Merges with conflicts are not supported yet, please " + [:span.text-gray-700.font-bold + "make sure saving your local changes first"] + ". After that, click " + [:a.font-bold {:href "" + :on-click clear-storage} + "Pull again"] + " to pull the latest changes."] + :error)))))))) (defn periodically-pull [repo-url pull-now?] @@ -224,6 +240,15 @@ (js/setInterval #(pull repo-url token) (* 60 1000)))) +(defn get-latest-commit + [handler] + (-> (p/let [commits (git/log (db/get-current-repo) + (get-github-token) + 1)] + (handler (first commits))) + (p/catch (fn [error] + (prn "get latest commit failed: " error))))) + ;; TODO: update latest commit (defn push [repo-url] @@ -238,7 +263,11 @@ (fn [] (prn "Push successfully!") (set-git-status! nil) - (set-git-error! nil)) + (set-git-error! nil) + (get-latest-commit + (fn [commit] + (when-let [hash (gobj/get commit "oid")] + (set-latest-commit! hash))))) (fn [error] (prn "Failed to push, error: " error) (set-git-status! :push-failed) diff --git a/web/src/main/frontend/state.cljs b/web/src/main/frontend/state.cljs index 1c69eb779..89c58eeb2 100644 --- a/web/src/main/frontend/state.cljs +++ b/web/src/main/frontend/state.cljs @@ -5,7 +5,7 @@ (def state (atom {:route-match nil :notification/show? false - :notification/text nil + :notification/content nil :root-component nil :edit? false :latest-journals [] diff --git a/web/src/main/frontend/ui.cljs b/web/src/main/frontend/ui.cljs index 547667d01..bd3e8d10e 100644 --- a/web/src/main/frontend/ui.cljs +++ b/web/src/main/frontend/ui.cljs @@ -59,61 +59,62 @@ text]) (rum/defc notification-content - [state text status] - (when (and text status) - (let [[color-class svg] (case status - :success - ["text-gray-900" - [:svg.h-6.w-6.text-green-400 - {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"} - [:path - {:d "M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z", - :stroke-width "2", - :stroke-linejoin "round", - :stroke-linecap "round"}]]] - ["text-red-500" - [:svg.h-6.w-6.text-red-500 - {:viewBox "0 0 20 20", :fill "currentColor"} - [:path - {:clip-rule "evenodd", - :d - "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z", - :fill-rule "evenodd"}]]])] - [:div.fixed.inset-0.flex.items-end.justify-center.px-4.py-6.pointer-events-none.sm:p-6.sm:items-start.sm:justify-end {:style {:top "3.2em"}} - [:div.max-w-sm.w-full.bg-white.shadow-lg.rounded-lg.pointer-events-auto - {:class (case state - "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0" - "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0" - "exiting" "transition ease-in duration-100 opacity-100" - "exited" "transition ease-in duration-100 opacity-0")} - [:div.rounded-lg.shadow-xs.overflow-hidden - [:div.p-4 - [:div.flex.items-start - [:div.flex-shrink-0 - svg] - [:div.ml-3.w-0.flex-1.pt-0.5 - [:p.text-sm.leading-5.font-medium {:style {:margin 0} - :class color-class} - text]] - [:div.ml-4.flex-shrink-0.flex - [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150 - {:on-click (fn [] - (swap! state/state assoc :notification/show? false))} - [:svg.h-5.w-5 - {:fill "currentColor", :viewBox "0 0 20 20"} - [:path - {:clip-rule "evenodd", - :d - "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z", - :fill-rule "evenodd"}]]]]]]]]]))) + [state content status] + (when (and content status) + (let [[color-class svg] + (case status + :success + ["text-gray-900" + [:svg.h-6.w-6.text-green-400 + {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"} + [:path + {:d "M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z", + :stroke-width "2", + :stroke-linejoin "round", + :stroke-linecap "round"}]]] + ["text-red-500" + [:svg.h-6.w-6.text-red-500 + {:viewBox "0 0 20 20", :fill "currentColor"} + [:path + {:clip-rule "evenodd", + :d + "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z", + :fill-rule "evenodd"}]]])] + [:div.fixed.inset-0.flex.items-end.justify-center.px-4.py-6.pointer-events-none.sm:p-6.sm:items-start.sm:justify-end {:style {:top "3.2em"}} + [:div.max-w-sm.w-full.bg-white.shadow-lg.rounded-lg.pointer-events-auto + {:class (case state + "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0" + "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0" + "exiting" "transition ease-in duration-100 opacity-100" + "exited" "transition ease-in duration-100 opacity-0")} + [:div.rounded-lg.shadow-xs.overflow-hidden + [:div.p-4 + [:div.flex.items-start + [:div.flex-shrink-0 + svg] + [:div.ml-3.w-0.flex-1.pt-0.5 + [:div.text-sm.leading-5.font-medium {:style {:margin 0} + :class color-class} + content]] + [:div.ml-4.flex-shrink-0.flex + [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150 + {:on-click (fn [] + (swap! state/state assoc :notification/show? false))} + [:svg.h-5.w-5 + {:fill "currentColor", :viewBox "0 0 20 20"} + [:path + {:clip-rule "evenodd", + :d + "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z", + :fill-rule "evenodd"}]]]]]]]]]))) (rum/defc notification < rum/reactive [] - (let [{:keys [:notification/show? :notification/text :notification/status]} (rum/react state/state)] + (let [{:keys [:notification/show? :notification/content :notification/status]} (rum/react state/state)] (css-transition {:in show? :timeout 100} (fn [state] - (notification-content state text status))))) + (notification-content state content status))))) (rum/defc checkbox [option]