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)
  (require 'org-roam)
   :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))))
               '(("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
                                           (org-roam-node-id it)
                                           (org-roam-node-title it))))))
                 ("Follow backlinks" . (lambda (x)
                                         (let ((candidates
                                                (--> x
                                                       (org-roam-backlink-source-node it))
                                           (helm-org-roam nil (or candidates (list x))))))))
                 "Create note"
               :action '(("Capture note" . (lambda (candidate)
                                              :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.