 
 ; UI
 
-(defn code-view [{:keys [selected posts] :as cursor} owner]
+(defn code-view [{:keys [selected-post posts] :as cursor} owner]
   (reify
     om/IRender
     (render [_]


(enable-console-print!)

(defonce app-state (atom {:seleced nil
                          :posts []}))

; WebSockets


          [:ul.list-unstyled
           (map (fn [i]
                  [:li
                   {:on-click (fn [_] (om/update! cursor [:selected-post] i))}
                   ((if (= i selected-post) (fn [text] [:strong text]) identity) (str "Code " (inc i)))])
                (reverse (range (count posts))))]]
         (when (seq posts)
           [:div.col-xs-10
            [:pre#codeview
             [:code (get posts selected-post)]]])]))
    om/IDidUpdate
    (did-update [_ _ _]
      (let [codeview (-> js/document
                         (.getElementById "codeview"))]
        (.highlightBlock js/hljs codeview)))))

(defn submit-code [cursor e]
  (let [code (-> js/document
                 (.getElementById "code"))]
    #_(print "Sent: " [:post-to-screen/code code])
    (chsk-send! [:post-to-screen/code (.-value code)])
    (set! (.-value code) "")
    (om/update! cursor [:selected-tab] "Show")
    (.preventDefault e)))

(defn post-form [cursor owner]
  (reify
    om/IRender
    (render [_]
      (html
        [:form.form-horizontal {:role      "form"
                                :on-submit (partial submit-code cursor)}
         [:div.form-group
          [:label.control-label.col-xs-1 {:for "code"} "Code:"]
          [:div.col-xs-10
           [:textarea#code.form-control {:rows "15"}]]]
         [:div.form-group
          [:div.col-xs-offset-1.col-xs-10
           [:button.btn {:type "submit"} "Post code"]]]]))
    om/IDidMount
    (did-mount [_]
      (-> js/document
          (.getElementById "code")
          .focus))))

(defn tab-view [{:keys [selected-tab] :as cursor} owner]
  (reify
    om/IRender
    (render [_]
      (let [tabs ["Post" "Show"]]
        (html
          [:div
           [:div.btn-group {:role "toolbar"}
            (map-indexed (fn [i tab]
                           [(str "button.btn.btn-default" (if (= tab selected-tab) ".active" ""))
                            {:type     "button"
                             :on-click (fn [_] (om/update! cursor [:selected-tab] tab))}
                            tab])
                         tabs)]
           [:hr]
           (case selected-tab
             "Post" (om/build post-form cursor)
             "Show" (om/build code-view cursor))])))))

(defn application [cursor owner]
  (reify
