Skip to content

Commit 7ef9d88

Browse files
committed
ENG-1088: An affordance to delete relations.
Sending necessary data through the result.
1 parent 593818d commit 7ef9d88

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

apps/roam/src/components/DiscourseContext.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,7 @@ const ContextTab = ({
269269
// TODO - always save settings, but maybe separate from root `parentUid`?
270270
preventSavingSettings
271271
parentUid={parentUid}
272-
results={Object.values(results).map(
273-
({ target, complement, id, ...a }) => a as Result,
274-
)}
272+
results={Object.values(results).map(({ target, ...a }) => a as Result)}
275273
columns={columns}
276274
onRefresh={onRefresh}
277275
header={

apps/roam/src/components/results-view/ResultsTable.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
} from "react";
88
import { Button, HTMLTable, Icon, IconName } from "@blueprintjs/core";
99
import { IconNames } from "@blueprintjs/icons";
10+
import { render as renderToast } from "roamjs-components/components/Toast";
1011
import { Column, Result } from "~/utils/types";
1112
import type { FilterData, Sorts, Views } from "~/utils/parseResultSettings";
1213
import Filter, { Filters } from "roamjs-components/components/Filter";
@@ -21,6 +22,9 @@ import toCellValue from "~/utils/toCellValue";
2122
import { ContextContent } from "~/components/DiscourseContext";
2223
import DiscourseContextOverlay from "~/components/DiscourseContextOverlay";
2324
import { CONTEXT_OVERLAY_SUGGESTION } from "~/utils/predefinedSelections";
25+
import { getSetting } from "~/utils/extensionSettings";
26+
import { strictQueryForReifiedBlocks } from "~/utils/createReifiedBlock";
27+
import formatDistanceStrictWithOptions from "date-fns/esm/fp/formatDistanceStrictWithOptions/index.js";
2428

2529
const EXTRA_ROW_TYPES = ["context", "discourse"] as const;
2630
type ExtraRowType = (typeof EXTRA_ROW_TYPES)[number] | null;
@@ -202,6 +206,7 @@ const ResultRow = ({
202206
onDragEnd,
203207
onRefresh,
204208
}: ResultRowProps) => {
209+
const useReifiedRel = getSetting<boolean>("use-reified-relations");
205210
const cell = (key: string) => {
206211
const value = toCellValue({
207212
value: r[`${key}-display`] || r[key] || "",
@@ -264,6 +269,52 @@ const ResultRow = ({
264269
[views],
265270
);
266271
const trRef = useRef<HTMLTableRowElement>(null);
272+
const onDelete = () => {
273+
const data = {
274+
sourceUid: r["complement"] === 1 ? r["uid"] : r["ctxTargetUid"],
275+
destinationUid: r["complement"] === 1 ? r["ctxTargetUid"] : r["uid"],
276+
hasSchema: r["id"],
277+
} as Record<string, string>;
278+
strictQueryForReifiedBlocks(data)
279+
.then((blockUid) => {
280+
if (blockUid === null) {
281+
renderToast({
282+
id: "delete-relation-error",
283+
content: "Could not find relation",
284+
intent: "warning",
285+
});
286+
return;
287+
}
288+
deleteBlock(blockUid)
289+
.then(() => {
290+
renderToast({
291+
id: "delete-relation-success",
292+
content: "Relation deleted",
293+
intent: "success",
294+
});
295+
onRefresh();
296+
})
297+
.catch((e) => {
298+
// this one should be an internalError
299+
console.error(e);
300+
renderToast({
301+
id: "delete-relation-error",
302+
content: "Could not delete relation",
303+
intent: "danger",
304+
});
305+
});
306+
})
307+
.catch((e) => {
308+
// this one should be an internalError
309+
console.error(e);
310+
renderToast({
311+
id: "delete-relation-error",
312+
content: "Error searching for relation",
313+
intent: "danger",
314+
});
315+
});
316+
};
317+
267318
return (
268319
<>
269320
<tr ref={trRef} data-uid={r.uid}>
@@ -321,6 +372,15 @@ const ResultRow = ({
321372
) : (
322373
cell(key)
323374
)}
375+
{useReifiedRel && r["ctxTargetUid"] !== undefined && (
376+
<Button
377+
minimal
378+
icon="delete"
379+
className="float-right"
380+
title="Delete relation"
381+
onClick={onDelete}
382+
></Button>
383+
)}
324384
{i < columns.length - 1 && (
325385
<div
326386
style={{

apps/roam/src/utils/createReifiedBlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const DISCOURSE_GRAPH_PROP_NAME = "discourse-graph";
66

77
const SANE_ROLE_NAME_RE = new RegExp(/^[\w\-]*$/);
88

9-
const strictQueryForReifiedBlocks = async (
9+
export const strictQueryForReifiedBlocks = async (
1010
parameterUids: Record<string, string>,
1111
): Promise<string | null> => {
1212
const paramsAsSeq = Object.entries(parameterUids);

apps/roam/src/utils/getDiscourseContextResults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ const executeQueries = async (
7676
onResult?: onResult,
7777
) => {
7878
const promises = queryConfigs.map(async ({ relation, queryPromise }) => {
79-
const results = await queryPromise();
79+
let results = await queryPromise();
80+
results = results.map((r) => ({ ...r, ctxTargetUid: targetUid }));
8081
if (onResult) {
8182
const groupedResult = {
8283
label: relation.text,

0 commit comments

Comments
 (0)