Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add react error boundaries for root and inspect-presented #698

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions notebooks/errors/boundaries.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns boundaries
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.viewer :as v]))

(clerk/add-viewers! [(assoc v/code-block-viewer :render-fn '(fn [] borked))])
14 changes: 8 additions & 6 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,15 @@
([opts x]
(if (valid-react-element? x)
x
(let [{:nextjournal/keys [value viewer] :keys [path]} x]
(let [{:nextjournal/keys [value viewer] :keys [path]} x
hash (str (:hash viewer) "@" (peek (:path opts)))]
#_(prn :inspect-presented value :valid-element? (react/isValidElement value) :viewer viewer)
;; each view function must be called in its own 'functional component' so that it gets its own hook state.
^{:key (str (:hash viewer) "@" (peek (:path opts)))}
[(:render-fn viewer) value (merge opts
(:nextjournal/render-opts x)
{:viewer viewer :path path})]))))
^{:key hash}
[:> ErrorBoundary {:hash hash}
[(:render-fn viewer) value (merge opts
(:nextjournal/render-opts x)
{:viewer viewer :path path})]]))))

(defn inspect [value]
(r/with-let [!state (r/atom nil)]
Expand Down Expand Up @@ -532,7 +534,7 @@
[body-fn* @!presented-value]]))

(defn root []
[:<>
[:> ErrorBoundary {:hash @!doc}
[:div.fixed.w-full.z-20.top-0.left-0.w-full
(when-let [status (:nextjournal.clerk.sci-env/connection-status @!doc)]
[connection-status status])
Expand Down
Loading