refactor: modularize repos-dropdown

This commit is contained in:
Peng Xiao
2022-03-03 14:01:12 +08:00
committed by Tienson Qin
parent 417950cdea
commit 421a45c447
3 changed files with 105 additions and 95 deletions

View File

@@ -195,16 +195,8 @@
:else :else
(db/get-repo-path repo))) (db/get-repo-path repo)))
(rum/defcs ^:large-vars/cleanup-todo repos-dropdown < rum/reactive (defn- repos-dropdown-links [repos current-repo *multiple-windows?]
(rum/local false ::electron-multiple-windows?) (let [switch-repos (remove (fn [repo] (= current-repo (:url repo))) repos) ; exclude current repo
[state]
(let [multiple-windows? (::electron-multiple-windows? state)]
(when-let [current-repo (state/sub :git/current-repo)]
(let [repos (state/sub [:me :repos])
repos (remove (fn [r] (= config/local-repo (:url r))) repos)
switch-repos (remove (fn [repo]
(= current-repo (:url repo)))
repos)
repo-links (mapv repo-links (mapv
(fn [{:keys [url]}] (fn [{:keys [url]}]
(let [repo-path (get-repo-name url) (let [repo-path (get-repo-name url)
@@ -217,15 +209,7 @@
(ui-handler/open-new-window! url) (ui-handler/open-new-window! url)
(state/pub-event! [:graph/switch url])))}})) (state/pub-event! [:graph/switch url])))}}))
switch-repos) switch-repos)
links (->> refresh-link (let [nfs-repo? (config/local-db? current-repo)]
(concat repo-links
[(when (seq switch-repos)
{:hr true})
{:title (t :new-graph)
:options {:href (rfe/href :repo-add)}}
{:title (t :all-graphs)
:options {:href (rfe/href :repos)}}
(let [nfs-repo? (config/local-db? current-repo)]
(when (and nfs-repo? (when (and nfs-repo?
(not= current-repo config/local-repo) (not= current-repo config/local-repo)
(or (nfs-handler/supported?) (or (nfs-handler/supported?)
@@ -237,30 +221,30 @@
(state/pub-event! (state/pub-event!
[:modal/show [:modal/show
[:div {:style {:max-width 700}} [:div {:style {:max-width 700}}
[:p "Refresh detects and processes files modified on your disk and diverged from the actual Logseq page content. Continue?"] [:p (t :sync-from-local-changes-detected)]
(ui/button (ui/button
"Yes" (t :yes)
:autoFocus "on" :autoFocus "on"
:large? true :large? true
:on-click (fn [] :on-click (fn []
(state/close-modal!) (state/close-modal!)
(nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))}})) (nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))}}))
{:title (t :re-index) reindex-link {:title (t :re-index)
:hover-detail (t :re-index-detail) :hover-detail (t :re-index-detail)
:options (cond-> :options (cond->
{:on-click {:on-click
(fn [] (fn []
(if @multiple-windows? (if @*multiple-windows?
(state/pub-event! (state/pub-event!
[:modal/show [:modal/show
[:div [:div
[:p "You need to close the other windows before re-index this graph."]]]) [:p (t :re-index-multiple-windows-warning)]]])
(state/pub-event! (state/pub-event!
[:modal/show [:modal/show
[:div {:style {:max-width 700}} [:div {:style {:max-width 700}}
[:p "Re-index will discard the current graph, and then processes all the files again as they are currently stored on disk. You will lose unsaved changes and it might take a while. Continue?"] [:p (t :re-index-discard-unsaved-changes-warning)]
(ui/button (ui/button
"Yes" (t :yes)
:autoFocus "on" :autoFocus "on"
:large? true :large? true
:on-click (fn [] :on-click (fn []
@@ -268,13 +252,28 @@
(repo-handler/re-index! (repo-handler/re-index!
nfs-handler/rebuild-index! nfs-handler/rebuild-index!
page-handler/create-today-journal!)))]])))})} page-handler/create-today-journal!)))]])))})}
(when (util/electron?) new-window-link (when (util/electron?)
{:title (t :open-new-window) {:title (t :open-new-window)
:options {:on-click ui-handler/open-new-window!}})]) :options {:on-click ui-handler/open-new-window!}})]
(remove nil?))] (->>
(when (seq repos) (concat repo-links
(ui/dropdown-with-links [(when (seq repos) {:hr true})
(fn [{:keys [toggle-fn]}] {:title (t :new-graph) :options {:href (rfe/href :repo-add)}}
{:title (t :all-graphs) :options {:href (rfe/href :repos)}}
refresh-link
reindex-link
new-window-link])
(remove nil?))))
(rum/defcs repos-dropdown < rum/reactive
(rum/local false ::electron-multiple-windows?)
[state]
(let [multiple-windows? (::electron-multiple-windows? state)]
(when-let [current-repo (state/sub :git/current-repo)]
(let [repos (state/sub [:me :repos])
repos (remove (fn [r] (= config/local-repo (:url r))) repos)
links (repos-dropdown-links repos current-repo multiple-windows?)
render-content (fn [{:keys [toggle-fn]}]
(let [repo-path (get-repo-name current-repo) (let [repo-path (get-repo-name current-repo)
short-repo-name (if (or (util/electron?) short-repo-name (if (or (util/electron?)
(mobile-util/is-native-platform?)) (mobile-util/is-native-platform?))
@@ -290,10 +289,11 @@
[:span#repo-switch.block.pr-2.whitespace-nowrap [:span#repo-switch.block.pr-2.whitespace-nowrap
[:span [:span#repo-name.font-medium short-repo-name]] [:span [:span#repo-name.font-medium short-repo-name]]
[:span.dropdown-caret.ml-2 {:style {:border-top-color "#6b7280"}}]]]])) [:span.dropdown-caret.ml-2 {:style {:border-top-color "#6b7280"}}]]]]))
links links-header (cond->
(cond->
{:modal-class (util/hiccup->class {:modal-class (util/hiccup->class
"origin-top-right.absolute.left-0.mt-2.rounded-md.shadow-lg")} "origin-top-right.absolute.left-0.mt-2.rounded-md.shadow-lg")}
(seq switch-repos) (> (count repos) 1) ; show switch to if there are multiple repos
(assoc :links-header [:div.font-medium.text-sm.opacity-60.px-4.pt-2 (assoc :links-header [:div.font-medium.text-sm.opacity-60.px-4.pt-2
"Switch to:"])))))))) "Switch to:"]))]
(when (seq repos)
(ui/dropdown-with-links render-content links links-header))))))

View File

@@ -286,9 +286,13 @@
:port "Port" :port "Port"
:re-index "Re-index" :re-index "Re-index"
:re-index-detail "Rebuild the graph" :re-index-detail "Rebuild the graph"
:re-index-multiple-windows-warning "You need to close the other windows before re-index this graph."
:re-index-discard-unsaved-changes-warning "Re-index will discard the current graph, and then processes all the files again as they are currently stored on disk. You will lose unsaved changes and it might take a while. Continue?"
:open-new-window "New window" :open-new-window "New window"
:sync-from-local-files "Refresh" :sync-from-local-files "Refresh"
:sync-from-local-files-detail "Import changes from local files" :sync-from-local-files-detail "Import changes from local files"
:sync-from-local-changes-detected "Refresh detects and processes files modified on your disk and diverged from the actual Logseq page content. Continue?"
:unlink "unlink" :unlink "unlink"
:search (if config/publishing? :search (if config/publishing?
"Search" "Search"
@@ -1170,8 +1174,13 @@
:cancel "取消" :cancel "取消"
:new-graph "添加图谱" :new-graph "添加图谱"
:re-index "重新建立索引" :re-index "重新建立索引"
:re-index-detail "重新建立索引"
:re-index-multiple-windows-warning "在重建当前图谱索引前,你需要先关闭其它窗口"
:re-index-discard-unsaved-changes-warning "重建索引将丢弃当前图谱,之后重新导入保存在磁盘上的所有文件。此操作将丢弃未保存的更改,同时可能需要一段时间。是否继续?"
:open-new-window "打开新窗口" :open-new-window "打开新窗口"
:sync-from-local-files "刷新(读取本地最新文件)" :sync-from-local-files "刷新(读取本地最新文件)"
:sync-from-local-files-detail "读取本地最新文件"
:sync-from-local-changes-detected "执行刷新操作将会导入磁盘上修改过的、或是与实际Logseq页面内容不同的文件。是否继续"
:export-graph "导出图谱" :export-graph "导出图谱"
:export-page "导出当前页面" :export-page "导出当前页面"
:export-json "以 JSON 格式导出" :export-json "以 JSON 格式导出"

View File

@@ -302,5 +302,6 @@
(defn open-new-window! (defn open-new-window!
[repo] [repo]
; TODO: find out a better way to open a new window with a different repo path
(when repo (storage/set :git/current-repo repo)) (when repo (storage/set :git/current-repo repo))
(ipc/ipc "openNewWindow")) (ipc/ipc "openNewWindow"))