From f04f9dc262d389f466e3f1029c5cecb943d5b600 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 22 Aug 2025 11:57:45 +0300 Subject: [PATCH] feat(react/ribbon): port shared switch --- .../src/widgets/ribbon/BasicPropertiesTab.tsx | 48 +++++++++++ .../ribbon_widgets/basic_properties.ts | 3 - apps/client/src/widgets/shared_switch.ts | 81 ------------------- 3 files changed, 48 insertions(+), 84 deletions(-) delete mode 100644 apps/client/src/widgets/shared_switch.ts diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx index 2b10599bd..33f28a277 100644 --- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx +++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx @@ -13,6 +13,8 @@ import FNote from "../../entities/fnote"; import protected_session from "../../services/protected_session"; import FormDropdownList from "../react/FormDropdownList"; import toast from "../../services/toast"; +import branches from "../../services/branches"; +import sync from "../../services/sync"; export default function BasicPropertiesTab() { const { note } = useNoteContext(); @@ -23,6 +25,7 @@ export default function BasicPropertiesTab() { + ); } @@ -199,6 +202,51 @@ function BookmarkSwitch({ note }: { note?: FNote | null }) { ) } +function SharedSwitch({ note }: { note?: FNote | null }) { + const [ isShared, setIsShared ] = useState(false); + const refreshState = useCallback(() => { + setIsShared(!!note?.hasAncestor("_share")); + }, [ note ]); + + useEffect(() => refreshState(), [ note ]); + useTriliumEventBeta("entitiesReloaded", ({ loadResults }) => { + if (note && loadResults.getBranchRows().find((b) => b.noteId === note.noteId)) { + refreshState(); + } + }); + + const switchShareState = useCallback(async (shouldShare: boolean) => { + if (!note) return; + + if (shouldShare) { + await branches.cloneNoteToParentNote(note.noteId, "_share"); + } else { + if (note?.getParentBranches().length === 1 && !(await dialog.confirm(t("shared_switch.shared-branch")))) { + return; + } + + const shareBranch = note?.getParentBranches().find((b) => b.parentNoteId === "_share"); + if (!shareBranch?.branchId) return; + await server.remove(`branches/${shareBranch.branchId}?taskId=no-progress-reporting`); + } + + sync.syncNow(true); + }, [ note ]); + + return ( +
+ +
+ ) +} + function findTypeTitle(type?: NoteType, mime?: string | null) { if (type === "code") { const mimeTypes = mime_types.getMimeTypes(); diff --git a/apps/client/src/widgets/ribbon_widgets/basic_properties.ts b/apps/client/src/widgets/ribbon_widgets/basic_properties.ts index e4127e0bc..8a762f96c 100644 --- a/apps/client/src/widgets/ribbon_widgets/basic_properties.ts +++ b/apps/client/src/widgets/ribbon_widgets/basic_properties.ts @@ -10,8 +10,6 @@ import type FNote from "../../entities/fnote.js"; import NoteLanguageWidget from "../note_language.js"; const TPL = /*html*/` -
-
@@ -28,7 +26,6 @@ export default class BasicPropertiesWidget extends NoteContextAwareWidget { constructor() { super(); - this.sharedSwitchWidget = new SharedSwitchWidget().contentSized(); this.templateSwitchWidget = new TemplateSwitchWidget().contentSized(); this.noteLanguageWidget = new NoteLanguageWidget().contentSized(); diff --git a/apps/client/src/widgets/shared_switch.ts b/apps/client/src/widgets/shared_switch.ts deleted file mode 100644 index 20e954efa..000000000 --- a/apps/client/src/widgets/shared_switch.ts +++ /dev/null @@ -1,81 +0,0 @@ -import SwitchWidget from "./switch.js"; -import branchService from "../services/branches.js"; -import server from "../services/server.js"; -import utils from "../services/utils.js"; -import syncService from "../services/sync.js"; -import dialogService from "../services/dialog.js"; -import { t } from "../services/i18n.js"; -import type FNote from "../entities/fnote.js"; -import type { EventData } from "../components/app_context.js"; - -export default class SharedSwitchWidget extends SwitchWidget { - - isEnabled() { - return super.isEnabled() - && !["root", "_share", "_hidden"].includes(this.noteId ?? "") - && !this.noteId?.startsWith("_options"); - } - - doRender() { - super.doRender(); - - this.switchOnName = t("shared_switch.shared"); - this.switchOnTooltip = t("shared_switch.toggle-on-title"); - - this.switchOffName = t("shared_switch.shared"); - this.switchOffTooltip = t("shared_switch.toggle-off-title"); - - this.$helpButton.attr("data-help-page", "sharing.html").show(); - this.$helpButton.on("click", (e) => utils.openHelp($(e.target))); - } - - async switchOn() { - if (!this.noteId) { - return; - } - - await branchService.cloneNoteToParentNote(this.noteId, "_share"); - - syncService.syncNow(true); - } - - async switchOff() { - const shareBranch = this.note?.getParentBranches().find((b) => b.parentNoteId === "_share"); - - if (!shareBranch) { - return; - } - - if (this.note?.getParentBranches().length === 1) { - if (!(await dialogService.confirm(t("shared_switch.shared-branch")))) { - return; - } - } - - await server.remove(`branches/${shareBranch.branchId}?taskId=no-progress-reporting`); - - syncService.syncNow(true); - } - - async refreshWithNote(note: FNote) { - const isShared = note.hasAncestor("_share"); - const canBeUnshared = isShared && note.getParentBranches().find((b) => b.parentNoteId === "_share"); - const switchDisabled = isShared && !canBeUnshared; - - this.isToggled = isShared; - - if (switchDisabled) { - this.disabledTooltip = t("shared_switch.inherited"); - this.canToggle = false; - } else { - this.disabledTooltip = ""; - this.canToggle = true; - } - } - - entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { - if (loadResults.getBranchRows().find((b) => b.noteId === this.noteId)) { - this.refresh(); - } - } -}