Preview Note While Searching Org-Roam

When searching for an org-roam note, the title isn’t always enough to tell if it’s what I was looking for. I want to preview the note without leaving the search interface (using helm).

To do that, I adapted this helm source for org-roam.

(defun helm-org-roam (&optional input candidates)
  (interactive)
  (require 'org-roam)
  (helm
   :input input
   :sources (list
             (helm-build-sync-source "Find note: "
               :must-match nil
               :fuzzy-match t
               :candidates (or candidates (org-roam-node-read--completions))
               :persistent-action (lambda (x)
                                    (--> x
                                         (view-file (org-roam-node-file it))))
               :action
               '(("Find File" . (lambda (x)
                                  (--> x
                                       (org-roam-node-visit it t))))
                 ("Preview" . (lambda (x)
                                (--> x
                                     (view-file (org-roam-node-file it)))))
                 ("Insert link" . (lambda (x)
                                    (--> x
                                         (insert
                                          (format
                                           "[[id:%s][%s]]"
                                           (org-roam-node-id it)
                                           (org-roam-node-title it))))))
                 ("Follow backlinks" . (lambda (x)
                                         (let ((candidates
                                                (--> x
                                                     org-roam-backlinks-get
                                                     (--map
                                                      (org-roam-node-title
                                                       (org-roam-backlink-source-node it))
                                                      it))))
                                           (helm-org-roam nil (or candidates (list x))))))))
             (helm-build-dummy-source
                 "Create note"
               :action '(("Capture note" . (lambda (candidate)
                                             (org-roam-capture-
                                              :node (org-roam-node-create :title candidate)
                                              :props '(:finalize find-file)))))))))

  (global-set-key (kbd "C-c n f") 'helm-org-roam)

Now when I have a list of search results I can preview the file using the C-j without leaving the search session.