             [cljs-time.core :as time]
             [datascript :as d]
             [frontend.async :refer [put!]]
+            [frontend.auth :as auth]
             [frontend.components.common :as common]
             [frontend.datascript :as ds]
             [frontend.state :as state]

           [:a.menu-close {:on-click #(cast! :overlay-closed)
                           :role "button"}
            (common/icon :times)]]
          [:div.menu-prompt-body
           (for [[time-bucket bucket-docs]
                 (reverse (sort-by #(:last-updated-instant (first (last %)))
                                   (group-by #(date->bucket (:last-updated-instant %)) docs)))]
             (list*
              [:div.recent-time-group
               [:h2 time-bucket]]

              (for [doc bucket-docs]
                [:div.recent-doc
                 [:a.recent-doc-thumb {:href (str "/document/" (:db/id doc))}
                  [:img {:src (str "/document/" (:db/id doc) ".svg")}]]
                 [:a.recent-doc-title {:href (str "/document/" (:db/id doc))}
                  (str (:db/id doc))]])))]])))))

     (time/after? time (time/minus start-of-day (time/days 14))) "last week"
     :else "a while ago")))

(defn signup-prompt [app owner]
  (reify
    om/IRender
    (render [_]
      (let [cast! (om/get-shared owner :cast!)]
        (html
         [:div.menu-prompt {:class (str "menu-prompt-" "username")}
          [:div.menu-header
           [:a.menu-close {:on-click #(cast! :overlay-closed)
                           :role "button"}
            (common/icon :times)]]
          [:div.menu-prompt-body
           [:h2 "Don't lose your creations"]
           [:p
            "Log in or sign up to view all of the docs you created."]
           [:a.prompt-button {:href (auth/auth-url)
                              :role "button"
                              :on-click #(do
                                           (.preventDefault %)
                                           (cast! :track-external-link-clicked
                                                  {:path (auth/auth-url)
                                                   :event "Signup Clicked"
                                                   :properties {:source "your-docs-overlay"}}))}
            "Sign Up"]]
          [:div.menu-footer
           [:a.menu-footer-link {:on-click #(do
                                              (.preventDefault %)
                                              (cast! :overlay-closed))
                                 :role "button"}
            "No thanks."]]])))))

(defn docs-list [docs owner]
  (reify
    om/IRender
    (render [_]
      (html
       [:div
        (for [[time-bucket bucket-docs]
              (reverse (sort-by #(:last-updated-instant (first (last %)))
                                (group-by #(date->bucket (:last-updated-instant %)) docs)))]
          (list*
           (html [:div.recent-time-group
                  [:h2 time-bucket]])

           (for [doc bucket-docs]
             (html
              [:div.recent-doc
               [:a.recent-doc-thumb {:href (str "/document/" (:db/id doc))}
                [:img {:src (str "/document/" (:db/id doc) ".svg")}]]
               [:a.recent-doc-title {:href (str "/document/" (:db/id doc))}
                (str (:db/id doc))]]))))]))))

(defn dummy-docs [current-doc-id doc-count]
  (repeat doc-count {:db/id current-doc-id
                     :last-update-instant (js/Date.)}))

(defn doc-viewer* [app owner]
  (reify
    om/IRender
    (render [_]
