Skip to content

Commit

Permalink
feat: add delete to toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyeh committed Oct 21, 2023
1 parent 0e304b7 commit d876bdc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/web/topic/components/Surface/TopicToolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Build, Download, Error, Group, Redo, Undo, Upload } from "@mui/icons-material";
import { Build, Delete, Download, Error, Group, Redo, Undo, Upload } from "@mui/icons-material";
import { AppBar, Divider, IconButton, ToggleButton, Toolbar, Tooltip } from "@mui/material";
import fileDownload from "js-file-download";
import { useEffect, useState } from "react";
Expand All @@ -12,6 +12,8 @@ import {
resetPerspectives,
useIsComparingPerspectives,
} from "../../../view/store/store";
import { deleteGraphPart } from "../../store/createDeleteActions";
import { useSelectedGraphPart } from "../../store/graphPartHooks";
import { migrate } from "../../store/migrate";
import { TopicStoreState, useOnPlayground } from "../../store/store";
import { useUserCanEditTopicData } from "../../store/userHooks";
Expand Down Expand Up @@ -64,6 +66,8 @@ export const TopicToolbar = () => {
const isComparingPerspectives = useIsComparingPerspectives();
const [hasErrored, setHasErrored] = useState(false);

const selectedGraphPart = useSelectedGraphPart();

const [isMoreActionsDrawerOpen, setIsMoreActionsDrawerOpen] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -117,6 +121,22 @@ export const TopicToolbar = () => {
>
<Redo />
</IconButton>

<Divider orientation="vertical" />

<IconButton
color="inherit"
title="Delete"
aria-label="Delete"
onClick={() => {
if (selectedGraphPart) {
void deleteGraphPart(selectedGraphPart);
}
}}
disabled={!selectedGraphPart}
>
<Delete />
</IconButton>
</>
)}

Expand Down
11 changes: 10 additions & 1 deletion src/web/topic/store/createDeleteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { createDraft, finishDraft } from "immer";
import { errorWithData } from "../../../common/errorHandling";
import { emitter } from "../../common/event";
import {
type Node,
type GraphPart,
Node,
RelationDirection,
buildEdge,
buildNode,
Expand Down Expand Up @@ -257,3 +258,11 @@ export const deleteEdge = async (edgeId: string) => {

useTopicStore.setState(finishDraft(state), false, "deleteEdge");
};

export const deleteGraphPart = async (graphPart: GraphPart) => {
if (graphPart.type === "ScoreEdge") {
await deleteEdge(graphPart.id);
} else {
await deleteNode(graphPart.id);
}
};
10 changes: 10 additions & 0 deletions src/web/topic/store/graphPartHooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useTopicStore } from "./store";
import { getActiveDiagram } from "./utils";

export const useExplicitClaimCount = (graphPartId: string) => {
return useTopicStore((state) => {
Expand All @@ -10,3 +11,12 @@ export const useExplicitClaimCount = (graphPartId: string) => {
return claimTree.nodes.length - 1;
});
};

export const useSelectedGraphPart = () => {
return useTopicStore((state) => {
const diagram = getActiveDiagram(state);
return (
diagram.edges.find((edge) => edge.selected) ?? diagram.nodes.find((node) => node.selected)
);
});
};
1 change: 1 addition & 0 deletions src/web/topic/utils/diagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const buildEdge = ({
};
};

export type GraphPart = Node | Edge;
export type GraphPartType = "node" | "edge";

export const possibleScores = ["-", "1", "2", "3", "4", "5", "6", "7", "8", "9"] as const;
Expand Down

0 comments on commit d876bdc

Please sign in to comment.