@sanity/sdk-react 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@ import {FrameMessage} from '@sanity/sdk'
17
17
  import {GetUsersOptions} from '@sanity/sdk'
18
18
  import {JsonMatch} from '@sanity/sdk'
19
19
  import {MediaResource} from '@sanity/message-protocol'
20
+ import {PathChangeMessage} from '@sanity/message-protocol'
20
21
  import {PerspectiveHandle} from '@sanity/sdk'
21
22
  import {PreviewValue} from '@sanity/sdk'
22
23
  import {ProjectHandle} from '@sanity/sdk'
@@ -784,6 +785,53 @@ declare type UseCurrentUser = {
784
785
  */
785
786
  export declare const useCurrentUser: UseCurrentUser
786
787
 
788
+ /**
789
+ * @public
790
+ *
791
+ * A helper hook designed to be injected into routing components for apps within the Dashboard.
792
+ * While the Dashboard can usually handle navigation, there are special cases when you
793
+ * are already within a target app, and need to navigate to another route inside of that app.
794
+ *
795
+ * For example, your user might "favorite" a document inside of your application.
796
+ * If they click on the Dashboard favorites item in the sidebar, and are already within your application,
797
+ * there needs to be some way for the dashboard to signal to your application to reroute to where that document was favorited.
798
+ *
799
+ * This hook is intended to receive those messages, and takes a function to route to the correct path.
800
+ *
801
+ * @param navigateFn - Function to handle navigation; should accept:
802
+ * - `path`: a string, which will be a relative path (for example, 'my-route')
803
+ * - `type`: 'push', 'replace', or 'pop', which will be the type of navigation to perform
804
+ *
805
+ * @example
806
+ * ```tsx
807
+ * import {useDashboardNavigate} from '@sanity/sdk-react'
808
+ * import {BrowserRouter, useNavigate} from 'react-router'
809
+ * import {Suspense} from 'react'
810
+ *
811
+ * function DashboardNavigationHandler() {
812
+ * const navigate = useNavigate()
813
+ * useDashboardNavigate(({path, type}) => {
814
+ * navigate(path, {replace: type === 'replace'})
815
+ * })
816
+ * return null
817
+ * }
818
+ *
819
+ * // Wrap the component with Suspense since the hook may suspend
820
+ * function MyApp() {
821
+ * return (
822
+ * <BrowserRouter>
823
+ * <Suspense>
824
+ * <DashboardNavigationHandler />
825
+ * </Suspense>
826
+ * </BrowserRouter>
827
+ * )
828
+ * }
829
+ * ```
830
+ */
831
+ export declare function useDashboardNavigate(
832
+ navigateFn: (options: PathChangeMessage['data']) => void,
833
+ ): void
834
+
787
835
  /**
788
836
  * @public
789
837
  *
@@ -1754,20 +1802,18 @@ export declare const useLogOut: () => () => Promise<void>
1754
1802
  * - `favorite` - Function to add document to favorites
1755
1803
  * - `unfavorite` - Function to remove document from favorites
1756
1804
  * - `isFavorited` - Boolean indicating if document is currently favorited
1757
- * - `isConnected` - Boolean indicating if connection to Dashboard UI is established
1758
1805
  *
1759
1806
  * @example
1760
1807
  * ```tsx
1761
1808
  * function FavoriteButton(props: DocumentActionProps) {
1762
1809
  * const {documentId, documentType} = props
1763
- * const {favorite, unfavorite, isFavorited, isConnected} = useManageFavorite({
1810
+ * const {favorite, unfavorite, isFavorited} = useManageFavorite({
1764
1811
  * documentId,
1765
1812
  * documentType
1766
1813
  * })
1767
1814
  *
1768
1815
  * return (
1769
1816
  * <Button
1770
- * disabled={!isConnected}
1771
1817
  * onClick={() => isFavorited ? unfavorite() : favorite()}
1772
1818
  * text={isFavorited ? 'Remove from favorites' : 'Add to favorites'}
1773
1819
  * />
@@ -1828,7 +1874,6 @@ declare interface UseManageFavoriteProps extends DocumentHandle {
1828
1874
  * studios with the same projectId and dataset
1829
1875
  * @returns An object containing:
1830
1876
  * - `navigateToStudioDocument` - Function that when called will navigate to the studio document
1831
- * - `isConnected` - Boolean indicating if connection to Dashboard is established
1832
1877
  *
1833
1878
  * @example
1834
1879
  * ```tsx
@@ -1837,10 +1882,9 @@ declare interface UseManageFavoriteProps extends DocumentHandle {
1837
1882
  * import {Suspense} from 'react'
1838
1883
  *
1839
1884
  * function NavigateButton({documentHandle}: {documentHandle: DocumentHandle}) {
1840
- * const {navigateToStudioDocument, isConnected} = useNavigateToStudioDocument(documentHandle)
1885
+ * const {navigateToStudioDocument} = useNavigateToStudioDocument(documentHandle)
1841
1886
  * return (
1842
1887
  * <Button
1843
- * disabled={!isConnected}
1844
1888
  * onClick={navigateToStudioDocument}
1845
1889
  * text="Navigate to Studio Document"
1846
1890
  * />
@@ -2176,7 +2220,6 @@ export declare function useQuery<TData>(options: QueryOptions): {
2176
2220
  * @param documentHandle - The document handle containing document ID and type, like `{_id: '123', _type: 'book'}`
2177
2221
  * @returns An object containing:
2178
2222
  * - `recordEvent` - Function to record document interactions
2179
- * - `isConnected` - Boolean indicating if connection to Studio is established
2180
2223
  *
2181
2224
  * @example
2182
2225
  * ```tsx
@@ -2186,7 +2229,7 @@ export declare function useQuery<TData>(options: QueryOptions): {
2186
2229
  *
2187
2230
  * function RecordEventButton(props: DocumentActionProps) {
2188
2231
  * const {documentId, documentType, resourceType, resourceId} = props
2189
- * const {recordEvent, isConnected} = useRecordDocumentHistoryEvent({
2232
+ * const {recordEvent} = useRecordDocumentHistoryEvent({
2190
2233
  * documentId,
2191
2234
  * documentType,
2192
2235
  * resourceType,
@@ -2194,7 +2237,6 @@ export declare function useQuery<TData>(options: QueryOptions): {
2194
2237
  * })
2195
2238
  * return (
2196
2239
  * <Button
2197
- * disabled={!isConnected}
2198
2240
  * onClick={() => recordEvent('viewed')}
2199
2241
  * text="Viewed"
2200
2242
  * />
package/dist/index.js CHANGED
@@ -377,6 +377,19 @@ function useWindowConnection(t0) {
377
377
  function _temp$3(unsubscribe) {
378
378
  return unsubscribe();
379
379
  }
380
+ function useDashboardNavigate(navigateFn) {
381
+ const $ = c(2);
382
+ let t0;
383
+ $[0] !== navigateFn ? (t0 = {
384
+ name: SDK_NODE_NAME,
385
+ connectTo: SDK_CHANNEL_NAME,
386
+ onMessage: {
387
+ "dashboard/v1/history/change-path": (data) => {
388
+ navigateFn(data);
389
+ }
390
+ }
391
+ }, $[0] = navigateFn, $[1] = t0) : t0 = $[1], useWindowConnection(t0);
392
+ }
380
393
  function useManageFavorite({
381
394
  documentId,
382
395
  documentType,
@@ -1046,7 +1059,7 @@ function useUsers(options) {
1046
1059
  loadMore
1047
1060
  };
1048
1061
  }
1049
- var version = "1.0.0";
1062
+ var version = "2.0.0";
1050
1063
  function getEnv(key) {
1051
1064
  if (typeof import.meta < "u" && import.meta.env)
1052
1065
  return import.meta.env[key];
@@ -1068,6 +1081,7 @@ export {
1068
1081
  useAuthToken,
1069
1082
  useClient,
1070
1083
  useCurrentUser,
1084
+ useDashboardNavigate,
1071
1085
  useDashboardOrganizationId,
1072
1086
  useDatasets,
1073
1087
  useDocument,