|
| 1 | +import React from "react"; |
| 2 | +import ReactDOM from "react-dom"; |
| 3 | +import { OnloadArgs } from "roamjs-components/types"; |
| 4 | +import { |
| 5 | + Popover, |
| 6 | + Menu, |
| 7 | + MenuItem, |
| 8 | + Button, |
| 9 | + Intent, |
| 10 | + Position, |
| 11 | + PopoverInteractionKind, |
| 12 | +} from "@blueprintjs/core"; |
| 13 | +import { FeedbackWidget } from "./BirdEatsBugs"; |
| 14 | + |
| 15 | +type DiscourseFloatingMenuProps = { |
| 16 | + // CSS placement class |
| 17 | + position: "top-left" | "top-right" | "bottom-left" | "bottom-right"; |
| 18 | + theme: string; // e.g., "bp3-light" | "bp3-dark" |
| 19 | + buttonTheme?: string; // e.g., "bp3-light" | "bp3-dark" |
| 20 | +}; |
| 21 | + |
| 22 | +const ANCHOR_ID = "dg-floating-menu-anchor"; |
| 23 | + |
| 24 | +export const DiscourseFloatingMenu = (props: DiscourseFloatingMenuProps) => ( |
| 25 | + <div |
| 26 | + id="discourse-floating-menu" |
| 27 | + className={`${props.position} ${props.theme}`} |
| 28 | + > |
| 29 | + <Popover |
| 30 | + autoFocus={false} |
| 31 | + content={ |
| 32 | + <Menu> |
| 33 | + <MenuItem |
| 34 | + text="Send feedback" |
| 35 | + icon="send-message" |
| 36 | + onClick={() => { |
| 37 | + try { |
| 38 | + (window.birdeatsbug as FeedbackWidget | undefined)?.trigger?.(); |
| 39 | + } catch (error) { |
| 40 | + console.error("Failed to trigger feedback widget:", error); |
| 41 | + } |
| 42 | + }} |
| 43 | + /> |
| 44 | + <MenuItem |
| 45 | + text="Docs" |
| 46 | + icon="document-open" |
| 47 | + href="https://discoursegraphs.com/docs/roam" |
| 48 | + rel="noopener noreferrer" |
| 49 | + target="_blank" |
| 50 | + /> |
| 51 | + <MenuItem |
| 52 | + text="Community" |
| 53 | + icon="people" |
| 54 | + href="https://join.slack.com/t/discoursegraphs/shared_invite/zt-37xklatti-cpEjgPQC0YyKYQWPNgAkEg" |
| 55 | + rel="noopener noreferrer" |
| 56 | + target="_blank" |
| 57 | + /> |
| 58 | + </Menu> |
| 59 | + } |
| 60 | + position={Position.TOP} |
| 61 | + className="bp3-popover-content-sizing" |
| 62 | + interactionKind={PopoverInteractionKind.HOVER} |
| 63 | + boundary="viewport" |
| 64 | + modifiers={{ |
| 65 | + arrow: { |
| 66 | + enabled: false, |
| 67 | + }, |
| 68 | + }} |
| 69 | + > |
| 70 | + <Button |
| 71 | + intent={Intent.PRIMARY} |
| 72 | + text="Discourse Graphs" |
| 73 | + id="dg-floating-menu-button" |
| 74 | + aria-label="Open Discourse Graphs menu" |
| 75 | + className={props.buttonTheme} |
| 76 | + /> |
| 77 | + </Popover> |
| 78 | + </div> |
| 79 | +); |
| 80 | + |
| 81 | +export const hideDiscourseFloatingMenu = () => { |
| 82 | + const anchor = document.getElementById(ANCHOR_ID); |
| 83 | + anchor?.classList.add("hidden"); |
| 84 | +}; |
| 85 | + |
| 86 | +export const showDiscourseFloatingMenu = () => { |
| 87 | + const anchor = document.getElementById(ANCHOR_ID); |
| 88 | + anchor?.classList.remove("hidden"); |
| 89 | +}; |
| 90 | + |
| 91 | +export const installDiscourseFloatingMenu = ( |
| 92 | + extensionAPI: OnloadArgs["extensionAPI"], |
| 93 | + props: DiscourseFloatingMenuProps = { |
| 94 | + position: "bottom-right", |
| 95 | + theme: "bp3-light", |
| 96 | + buttonTheme: "bp3-dark", |
| 97 | + }, |
| 98 | +) => { |
| 99 | + let floatingMenuAnchor = document.getElementById(ANCHOR_ID); |
| 100 | + if (!floatingMenuAnchor) { |
| 101 | + floatingMenuAnchor = document.createElement("div"); |
| 102 | + floatingMenuAnchor.id = ANCHOR_ID; |
| 103 | + document.getElementById("app")?.appendChild(floatingMenuAnchor); |
| 104 | + } |
| 105 | + if (extensionAPI.settings.get("hide-feedback-button") as boolean) { |
| 106 | + floatingMenuAnchor.classList.add("hidden"); |
| 107 | + } |
| 108 | + ReactDOM.render( |
| 109 | + <DiscourseFloatingMenu |
| 110 | + position={props.position} |
| 111 | + theme={props.theme} |
| 112 | + buttonTheme={props.buttonTheme} |
| 113 | + />, |
| 114 | + floatingMenuAnchor, |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export const removeDiscourseFloatingMenu = () => { |
| 119 | + const anchor = document.getElementById(ANCHOR_ID); |
| 120 | + if (anchor) { |
| 121 | + try { |
| 122 | + ReactDOM.unmountComponentAtNode(anchor); |
| 123 | + } catch (e) { |
| 124 | + // no-op: unmount best-effort |
| 125 | + } |
| 126 | + anchor.remove(); |
| 127 | + } |
| 128 | +}; |
0 commit comments