Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions apps/roam/src/components/SuggestionsBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import type { PageGroup } from "~/components/settings/utils/zodSchema";
import { createReifiedRelation } from "~/utils/createReifiedBlock";
import { getStoredRelationsEnabled } from "~/utils/storedRelations";
import posthog from "posthog-js";
import {
notifyBlockSuggestionAdded,
notifyRelationSuggestionAdded,
} from "~/utils/notifySuggestiveModeAdoption";

export type DiscourseData = {
results: Awaited<ReturnType<typeof getDiscourseContextResults>>;
Expand Down Expand Up @@ -360,6 +364,7 @@ const SuggestionsBody = ({
});
return;
}
notifyRelationSuggestionAdded(tag, node.text);
} else {
renderToast({
id: "suggestions-create-block-error",
Expand All @@ -373,7 +378,9 @@ const SuggestionsBody = ({
parentUid: blockUid,
node: { text: `[[${node.text}]]` },
});
await notifyBlockSuggestionAdded(blockUid);
}

posthog.capture("Suggestive Mode: Suggestion Adopted", {
tag,
nodeType: node.type,
Expand Down
88 changes: 88 additions & 0 deletions apps/roam/src/utils/notifySuggestiveModeAdoption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { render as renderToast } from "roamjs-components/components/Toast";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
import getPageUidByBlockUid from "roamjs-components/queries/getPageUidByBlockUid";
import getCurrentPageUid from "roamjs-components/dom/getCurrentPageUid";
import openBlockInSidebar from "roamjs-components/writes/openBlockInSidebar";
import { getPersonalSetting } from "~/components/settings/utils/accessors";
import { PERSONAL_KEYS } from "~/components/settings/utils/settingKeys";
import { isPageUid } from "~/utils/isPageUid";

type RoamSidebarWindow = {
type: string;
"window-id": string;
"block-uid"?: string;
};

const showSuggestionToast = (content: string): void => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefit is this function bringing?

renderToast({
id: "suggestive-mode-added",
content,
intent: "success",
timeout: 4000,
});
};

// When opening a new window, omit windowId and pass a delay to let Roam finish rendering.
const focusSidebarOutline = (windowId?: string, delayMs = 0): void => {
const focus = () => {
const container = windowId
? document.querySelector<HTMLElement>(
`[data-sidebar-window-id="${windowId}"]`,
)
: document.querySelector<HTMLElement>(".rm-sidebar-outline");
container
?.querySelector<HTMLElement>(".rm-title-display")
?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
};
delayMs > 0 ? setTimeout(focus, delayMs) : focus();
};

export const notifyBlockSuggestionAdded = async (
targetBlockUid: string,
): Promise<void> => {
const pageUid = isPageUid(targetBlockUid)
? targetBlockUid
: getPageUidByBlockUid(targetBlockUid) || targetBlockUid;

const mainUid = getCurrentPageUid();
const isOpenInMain = mainUid === pageUid || mainUid === targetBlockUid;

if (
isOpenInMain ||
getPersonalSetting<boolean>([PERSONAL_KEYS.disableSidebarOpen])
) {
const pageTitle = getPageTitleByPageUid(pageUid);
showSuggestionToast(
pageTitle ? `Added to [[${pageTitle}]]` : "Added to outline",
);
return;
}

let sidebarWindows: RoamSidebarWindow[] = [];
try {
sidebarWindows = window.roamAlphaAPI.ui.rightSidebar.getWindows() ?? [];
} catch {}

Check warning on line 64 in apps/roam/src/utils/notifySuggestiveModeAdoption.ts

View workflow job for this annotation

GitHub Actions / eslint (apps/roam)

[eslint (apps/roam)] apps/roam/src/utils/notifySuggestiveModeAdoption.ts#L64

Empty block statement no-empty
Raw output
  64:11  warning  Empty block statement  no-empty

const existingWindow = sidebarWindows.find(
(w) =>
w.type === "outline" &&
(w["block-uid"] === targetBlockUid || w["block-uid"] === pageUid),
);

if (existingWindow) {
focusSidebarOutline(existingWindow["window-id"]);
return;
}

await openBlockInSidebar(targetBlockUid);
focusSidebarOutline(undefined, 100);
};

export const notifyRelationSuggestionAdded = (
sourceTitle: string,
destinationTitle: string,
): void => {
showSuggestionToast(
`Added relation between [[${sourceTitle}]] and [[${destinationTitle}]]`,
);
};
Loading