-
Notifications
You must be signed in to change notification settings - Fork 7
ENG-1869 Review and accept imported relation instances in Roam #1384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sid597
wants to merge
7
commits into
main
Choose a base branch
from
eng-1869-review-and-accept-imported-relation-instances-in-roam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eec212b
ENG-1869 Review and accept imported relation instances in Roam
sid597 c811292
ENG-1869 Address pre-PR review findings on tentative relation acceptance
sid597 3903104
ENG-1869 Address second review pass findings
sid597 3a1438a
ENG-1869 Reuse relation schemas with multiple query patterns
sid597 85f7e0c
Merge main and preserve relation import regression coverage
sid597 70f8aca
Handle pending relation load failures and defer the empty state
sid597 e46676c
Fix ENG-1869 accepted relation deletion with pending twins
sid597 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
apps/roam/src/components/TentativeRelationInstances.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import React, { useCallback, useEffect, useState } from "react"; | ||
| import { Button, Classes, Tag } from "@blueprintjs/core"; | ||
| import { render as renderToast } from "roamjs-components/components/Toast"; | ||
| import deleteBlock from "roamjs-components/writes/deleteBlock"; | ||
| import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid"; | ||
| import posthog from "posthog-js"; | ||
| import { isRid, ridToSpaceUriAndLocalId } from "@repo/database/lib/rid"; | ||
| import getDiscourseRelations from "~/utils/getDiscourseRelations"; | ||
| import internalError from "~/utils/internalError"; | ||
| import { getErrorMessage } from "~/utils/materializeSharedNode"; | ||
| import { getStoredRelationsEnabled } from "~/utils/storedRelations"; | ||
| import { | ||
| refreshDiscourseContextsForMutatedUids, | ||
| useDiscourseContextMutationRefresh, | ||
| } from "~/utils/discourseContextMutationRefresh"; | ||
| import { acceptTentativeRelationInstance } from "~/utils/createReifiedBlock"; | ||
| import { | ||
| getTentativeRelationInstances, | ||
| type TentativeRelationInstance, | ||
| } from "~/utils/tentativeRelations"; | ||
| import type { ImportedSourceIdentity } from "~/utils/importedSourceIdentity"; | ||
|
|
||
| type TentativeRelationRow = TentativeRelationInstance & { | ||
| label: string; | ||
| otherText: string; | ||
| provenance: string; | ||
| }; | ||
|
|
||
| const buildProvenance = (importedFrom?: ImportedSourceIdentity): string => { | ||
| if (!importedFrom || !isRid(importedFrom.sourceNodeRid)) return ""; | ||
| const { spaceUri, sourceLocalId } = ridToSpaceUriAndLocalId( | ||
| importedFrom.sourceNodeRid, | ||
| ); | ||
| const modifiedAt = new Date(importedFrom.sourceModifiedAt); | ||
| const modified = Number.isNaN(modifiedAt.getTime()) | ||
| ? undefined | ||
| : modifiedAt.toLocaleString(); | ||
| return `from ${[spaceUri, sourceLocalId, modified].filter(Boolean).join(" · ")}`; | ||
| }; | ||
|
|
||
| const TentativeRelationInstances = ({ | ||
| uid, | ||
| onCountChange, | ||
| }: { | ||
| uid: string; | ||
| onCountChange: (count: number | undefined) => void; | ||
| }): React.JSX.Element | null => { | ||
| const [rows, setRows] = useState<TentativeRelationRow[]>([]); | ||
| const [pending, setPending] = useState<{ | ||
| uid: string; | ||
| action: "accept" | "remove"; | ||
| } | null>(null); | ||
|
|
||
| const loadRows = useCallback(async () => { | ||
| if (!getStoredRelationsEnabled()) { | ||
| onCountChange(0); | ||
| return; | ||
| } | ||
| onCountChange(undefined); | ||
| try { | ||
| const instances = await getTentativeRelationInstances(); | ||
| const relevant = instances.filter( | ||
| (instance) => | ||
| instance.sourceUid === uid || instance.destinationUid === uid, | ||
| ); | ||
| const relationById = new Map( | ||
| getDiscourseRelations().map((r) => [r.id, r]), | ||
| ); | ||
| const nextRows = relevant.map((instance) => { | ||
| const isOutgoing = instance.sourceUid === uid; | ||
| const otherUid = isOutgoing | ||
| ? instance.destinationUid | ||
| : instance.sourceUid; | ||
| const schema = relationById.get(instance.schemaUid); | ||
| const label = | ||
| (isOutgoing ? schema?.label : schema?.complement || schema?.label) || | ||
| "Unknown relation"; | ||
| return { | ||
| ...instance, | ||
| label, | ||
| otherText: getPageTitleByPageUid(otherUid) || otherUid, | ||
| provenance: buildProvenance(instance.importedFrom), | ||
| }; | ||
| }); | ||
| setRows(nextRows); | ||
| onCountChange(nextRows.length); | ||
| } catch (error) { | ||
| internalError({ | ||
| error, | ||
| type: "Load Tentative Relations Failed", | ||
| context: { uid }, | ||
| userMessage: | ||
| "Could not load imported relations. Refresh and try again.", | ||
| sendEmail: false, | ||
| }); | ||
| } | ||
| }, [uid, onCountChange]); | ||
|
|
||
| useEffect(() => { | ||
| void loadRows(); | ||
| }, [loadRows]); | ||
|
|
||
| const onMutationRefresh = useCallback(() => void loadRows(), [loadRows]); | ||
| useDiscourseContextMutationRefresh({ uid, onMutationRefresh }); | ||
|
|
||
| const onAccept = async (row: TentativeRelationRow): Promise<void> => { | ||
| posthog.capture("Discourse Context: Accept Tentative Relation Triggered", { | ||
| instanceUid: row.instanceUid, | ||
| uid, | ||
| }); | ||
| setPending({ uid: row.instanceUid, action: "accept" }); | ||
| try { | ||
| await acceptTentativeRelationInstance({ instanceUid: row.instanceUid }); | ||
| renderToast({ | ||
| id: "accept-relation-success", | ||
| content: "Relation accepted", | ||
| intent: "success", | ||
| }); | ||
| refreshDiscourseContextsForMutatedUids({ | ||
| uids: [row.sourceUid, row.destinationUid], | ||
| }); | ||
| } catch (error) { | ||
| internalError({ | ||
| error, | ||
| type: "Accept Tentative Relation Failed", | ||
| context: { instanceUid: row.instanceUid }, | ||
| userMessage: `Could not accept relation: ${getErrorMessage(error)}`, | ||
| sendEmail: false, | ||
| }); | ||
| } finally { | ||
| setPending(null); | ||
| } | ||
| }; | ||
|
|
||
| const onRemove = async (row: TentativeRelationRow): Promise<void> => { | ||
| posthog.capture("Discourse Context: Remove Tentative Relation Triggered", { | ||
| instanceUid: row.instanceUid, | ||
| uid, | ||
| }); | ||
| setPending({ uid: row.instanceUid, action: "remove" }); | ||
| try { | ||
| await deleteBlock(row.instanceUid); | ||
| renderToast({ | ||
| id: "remove-relation-success", | ||
| content: "Relation removed", | ||
| intent: "success", | ||
| }); | ||
| refreshDiscourseContextsForMutatedUids({ | ||
| uids: [row.sourceUid, row.destinationUid], | ||
| }); | ||
| } catch (error) { | ||
| internalError({ | ||
| error, | ||
| type: "Remove Tentative Relation Failed", | ||
| context: { instanceUid: row.instanceUid }, | ||
| userMessage: `Could not remove relation: ${getErrorMessage(error)}`, | ||
| sendEmail: false, | ||
| }); | ||
| } finally { | ||
| setPending(null); | ||
| } | ||
| }; | ||
|
|
||
| if (!rows.length) return null; | ||
|
|
||
| return ( | ||
| <div className="roamjs-discourse-tentative-relations mt-2 px-2"> | ||
| <div className={`${Classes.TEXT_MUTED} text-xs font-semibold`}> | ||
| Imported relations pending review ({rows.length}) | ||
| </div> | ||
| {rows.map((row) => ( | ||
| <div key={row.instanceUid} className="flex items-center gap-2 py-1"> | ||
| <div className="min-w-0 flex-1"> | ||
| <div className="truncate" title={row.otherText}> | ||
| <Tag minimal>{row.label}</Tag> {row.otherText} | ||
| </div> | ||
| {row.provenance && ( | ||
| <div | ||
| className={`${Classes.TEXT_MUTED} truncate text-xs`} | ||
| title={row.importedFrom?.sourceNodeRid} | ||
| > | ||
| {row.provenance} | ||
| </div> | ||
| )} | ||
| </div> | ||
| <Button | ||
| minimal | ||
| icon="tick" | ||
| title="Accept relation" | ||
| disabled={pending !== null} | ||
| loading={ | ||
| pending?.uid === row.instanceUid && pending.action === "accept" | ||
| } | ||
| onClick={() => void onAccept(row)} | ||
| /> | ||
| <Button | ||
| minimal | ||
| icon="delete" | ||
| title="Remove relation" | ||
| disabled={pending !== null} | ||
| loading={ | ||
| pending?.uid === row.instanceUid && pending.action === "remove" | ||
| } | ||
| onClick={() => void onRemove(row)} | ||
| /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default TentativeRelationInstances; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unhandled promise rejections in async operations. The
loadRowsfunction is called withvoid(lines 83, 86), which prevents error handling. IfgetTentativeRelationInstances()or any other async operation fails, it will cause an unhandled promise rejection that could crash the application or leave the UI in a broken state.Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.