Skip to content
Open
Changes from all 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
55 changes: 47 additions & 8 deletions apps/roam/src/components/settings/DiscourseNodeConfigPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
Alert,
Button,
ControlGroup,
Icon,
InputGroup,
Intent,
HTMLTable,
Tooltip,
} from "@blueprintjs/core";
import React, { useState } from "react";
import React, { useMemo, useState } from "react";
import getDiscourseNodes from "~/utils/getDiscourseNodes";
import refreshConfigTree from "~/utils/refreshConfigTree";
import createPage from "roamjs-components/writes/createPage";
Expand All @@ -31,6 +32,18 @@ type DiscourseNodeConfigPanelProps = React.ComponentProps<
setSelectedTabId: (id: string) => void;
};

const getFirstAvailableShortcut = (
label: string,
takenShortcuts: Set<string>,
): string => {
for (const char of label.toUpperCase()) {
if (/[A-Z0-9]/.test(char) && !takenShortcuts.has(char)) {
return char;
}
}
return "";
};

const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
isPopup,
setSelectedTabId,
Expand All @@ -49,6 +62,19 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
DiscourseRelation[]
>([]);
const [nodeTypeIdToDelete, setNodeTypeIdToDelete] = useState<string>("");
const duplicateShortcuts = useMemo(() => {
const counts = new Map<string, number>();
nodes.forEach((n) => {
const shortcut = n.shortcut.toUpperCase();
if (!shortcut) return;
counts.set(shortcut, (counts.get(shortcut) ?? 0) + 1);
});
return new Set(
Array.from(counts.entries())
.filter(([, count]) => count > 1)
.map(([shortcut]) => shortcut),
);
}, [nodes]);
const navigateToNode = (uid: string) => {
if (isPopup) {
setSelectedTabId(uid);
Expand Down Expand Up @@ -82,15 +108,12 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
className="select-none"
disabled={!label}
onClick={() => {
const candidateShortcut = label.slice(0, 1).toUpperCase();
const existingShortcuts = new Set(
const takenShortcuts = new Set(
getDiscourseNodes()
.map((n) => n.shortcut.toUpperCase())
.filter(Boolean),
);
const shortcut = existingShortcuts.has(candidateShortcut)
? ""
: candidateShortcut;
const shortcut = getFirstAvailableShortcut(label, takenShortcuts);
const format = `[[${label.slice(0, 3).toUpperCase()}]] - {content}`;
posthog.capture("Discourse Node: Type Created", { label: label });
void createPage({
Expand Down Expand Up @@ -123,10 +146,10 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
setNodes([
...nodes,
{
format: "",
format,
type: valueUid,
text: label,
shortcut: "",
shortcut,
tag: "",
specification: [],
backedBy: "user",
Expand All @@ -144,6 +167,7 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
<thead>
<tr>
<th>Node</th>
<th>Shortcut</th>
<th>Actions</th>
</tr>
</thead>
Expand All @@ -165,6 +189,21 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
<span>{n.text}</span>
</div>
</td>
<td
onClick={() => navigateToNode(n.type)}
style={{ verticalAlign: "middle" }}
>
<div className="flex items-center gap-2">
<span className="font-mono">{n.shortcut}</span>
{duplicateShortcuts.has(n.shortcut.toUpperCase()) && (
<Tooltip
content={`Multiple nodes share the shortcut "${n.shortcut.toUpperCase()}". Only one node can respond to it in the node menu.`}
>
<Icon icon="warning-sign" intent={Intent.WARNING} />
</Tooltip>
)}
</div>
</td>
<td>
<Tooltip content="Edit" hoverOpenDelay={500}>
<Button
Expand Down