-
Notifications
You must be signed in to change notification settings - Fork 432
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
fix: ensure created comments create URLs with inspect and comment params #5012
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import {useCallback, useMemo} from 'react' | ||
import {useCallback} from 'react' | ||
import {useMemoObservable} from 'react-rx' | ||
import {of} from 'rxjs' | ||
import {useRouterState} from 'sanity/router' | ||
import {useSchema, useWorkspace, useDocumentPreviewStore, getPreviewStateObservable} from 'sanity' | ||
import {usePaneRouter} from '../../../components' | ||
import {COMMENTS_INSPECTOR_NAME} from '../../../panes/document/constants' | ||
import {CommentContext} from '../types' | ||
import {getPreviewStateObservable, useDocumentPreviewStore, useSchema, useWorkspace} from 'sanity' | ||
|
||
interface NotificationTargetHookOptions { | ||
documentId: string | ||
documentType: string | ||
} | ||
|
||
interface NotificationTargetHookValue { | ||
documentTitle: string | ||
toolName: string | ||
url: string | ||
workspaceTitle: string | ||
/** | ||
* Returns an object with notification-specific values for the selected comment, such as | ||
* the current workspace + document title and full URL to the comment. | ||
* These values are currently used in notification emails. | ||
* | ||
* **Please note:** this will generate a URL for the comment based on the current _active_ pane. | ||
* The current active pane may not necessarily be the right-most desk pane and in these cases, | ||
* the selected comment may not be visible on initial load when visiting these URLs. | ||
*/ | ||
getNotificationValue: ({commentId}: {commentId: string}) => CommentContext['notification'] | ||
} | ||
|
||
/** @internal */ | ||
|
@@ -22,18 +30,8 @@ export function useNotificationTarget( | |
): NotificationTargetHookValue { | ||
const {documentId, documentType} = opts || {} | ||
const schemaType = useSchema().get(documentType) | ||
const {basePath, title: workspaceTitle, tools} = useWorkspace() | ||
|
||
const activeToolName = useRouterState( | ||
useCallback( | ||
(routerState) => (typeof routerState.tool === 'string' ? routerState.tool : undefined), | ||
[], | ||
), | ||
) | ||
const activeTool = useMemo( | ||
() => tools.find((tool) => tool.name === activeToolName), | ||
[activeToolName, tools], | ||
) | ||
const {title: workspaceTitle} = useWorkspace() | ||
const {createPathWithParams, params} = usePaneRouter() | ||
|
||
const documentPreviewStore = useDocumentPreviewStore() | ||
|
||
|
@@ -45,15 +43,25 @@ export function useNotificationTarget( | |
const {published, draft} = previewState || {} | ||
const documentTitle = (draft?.title || published?.title || 'Sanity document') as string | ||
|
||
const currentUrl = new URL(window.location.href) | ||
const deskToolSegment = currentUrl.pathname.split('/').slice(2, 3).join('') | ||
currentUrl.pathname = `${basePath}/${deskToolSegment}/__edit__${documentId},type=${documentType}` | ||
const notificationUrl = currentUrl.toString() | ||
const handleGetNotificationValue = useCallback( | ||
({commentId}: {commentId: string}) => { | ||
// Generate a path based on the current pane params. | ||
// We force a value for `inspect` to ensure that this is included in URLs when comments | ||
// are created outside of the inspector context (i.e. directly on the field) | ||
// @todo: consider filtering pane router params and culling all non-active RHS panes prior to generating this link | ||
const path = createPathWithParams({ | ||
...params, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that this function was useful here as well! 👍 We don't do the "force thing" when copying the comment URL, we should maybe add it there as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably unnecessary since you can only (currently) copy from the document inspector, but also probably wouldn't hurt to include! |
||
comment: commentId, | ||
inspect: COMMENTS_INSPECTOR_NAME, | ||
}) | ||
const url = `${window.location.origin}${path}` | ||
|
||
return {documentTitle, url, workspaceTitle} | ||
}, | ||
[createPathWithParams, documentTitle, params, workspaceTitle], | ||
) | ||
|
||
return { | ||
documentTitle, | ||
toolName: activeTool?.name || '', | ||
url: notificationUrl, | ||
workspaceTitle, | ||
getNotificationValue: handleGetNotificationValue, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,11 @@ export interface CommentPath { | |
field: string | ||
} | ||
|
||
interface CommentContext { | ||
/** | ||
* @beta | ||
* @hidden | ||
*/ | ||
export interface CommentContext { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exporting this since I wanted to avoid something like |
||
tool: string | ||
payload?: Record<string, unknown> | ||
notification?: { | ||
|
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.
This was moved out of
useNotificationTarget
since the tool name isn't part of the notification object