@postrun/react 1.1.0 → 1.3.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/README.md CHANGED
@@ -104,9 +104,11 @@ returns the same `{ state, start, prepare, select, reset }` — `<Connect>` is j
104
104
  a thin render-prop wrapper over it. The hosted `/connect` page remains available
105
105
  as a no-SDK fallback (link to the `hosted_connect_url` from a `POST .../connect`).
106
106
 
107
- `onConnected` also auto-refetches your `useConnections` list, so the new account
108
- shows up with no manual refetch. Pass `onError(reason)` / `onCancelled()` if you'd
109
- rather react with callbacks than read `state.phase`.
107
+ A successful connect auto-refetches your `useConnections` list, so the new account
108
+ shows up with no manual refetch. Prefer callbacks to reading `state.phase`? Use
109
+ `onSuccess()` (fires on success `active` **or** `connected_pending`, ideal for
110
+ "close the dialog"), `onConnected(connection)` (only `active`, hands you the bound
111
+ connection), `onError(reason)`, and `onCancelled()`.
110
112
 
111
113
  **Multi-platform picker?** A session is pre-minted on mount, which is ideal for a
112
114
  dedicated button but would mint one per platform in a "pick a network" list. Set
package/dist/index.cjs CHANGED
@@ -315,6 +315,7 @@ function useConnect({
315
315
  profileId,
316
316
  platform,
317
317
  onConnected,
318
+ onSuccess,
318
319
  onError,
319
320
  onCancelled,
320
321
  prepareOnMount = true
@@ -329,10 +330,12 @@ function useConnect({
329
330
  const preparingRef = react.useRef(false);
330
331
  const prepareGenRef = react.useRef(0);
331
332
  const onConnectedRef = react.useRef(onConnected);
333
+ const onSuccessRef = react.useRef(onSuccess);
332
334
  const onErrorRef = react.useRef(onError);
333
335
  const onCancelledRef = react.useRef(onCancelled);
334
336
  react.useEffect(() => {
335
337
  onConnectedRef.current = onConnected;
338
+ onSuccessRef.current = onSuccess;
336
339
  onErrorRef.current = onError;
337
340
  onCancelledRef.current = onCancelled;
338
341
  });
@@ -455,10 +458,12 @@ function useConnect({
455
458
  setState({ phase: "active", connection: outcome.connection });
456
459
  void queryClient.invalidateQueries({ queryKey: connectionKeys.lists() });
457
460
  onConnectedRef.current?.(outcome.connection);
461
+ onSuccessRef.current?.();
458
462
  return;
459
463
  case "connected_pending":
460
464
  setState({ phase: "connected_pending" });
461
465
  void queryClient.invalidateQueries({ queryKey: connectionKeys.lists() });
466
+ onSuccessRef.current?.();
462
467
  return;
463
468
  case "cancelled":
464
469
  setState({ phase: "cancelled" });
@@ -553,6 +558,7 @@ function Connect({
553
558
  profileId,
554
559
  platform,
555
560
  onConnected,
561
+ onSuccess,
556
562
  onError,
557
563
  onCancelled,
558
564
  prepareOnMount,
@@ -562,6 +568,7 @@ function Connect({
562
568
  profileId,
563
569
  platform,
564
570
  onConnected,
571
+ onSuccess,
565
572
  onError,
566
573
  onCancelled,
567
574
  prepareOnMount
@@ -894,6 +901,37 @@ function useCreatePost(profileId) {
894
901
  connectedChannels: connected.map((connection) => connection.platform).filter(js.isPostPlatform)
895
902
  };
896
903
  }
904
+ function useValidatePost(profileId) {
905
+ const { client, queryClient } = usePostrun();
906
+ const connections = useConnections(profileId);
907
+ const connected = connections.data?.data ?? [];
908
+ const mutation = reactQuery.useMutation(
909
+ {
910
+ mutationFn: async (input) => (await js.postsValidate({
911
+ client,
912
+ body: js.buildCreatePost({ ...input, profileId }, connected)
913
+ })).data
914
+ },
915
+ queryClient
916
+ );
917
+ const { mutateAsync } = mutation;
918
+ const validate = react.useCallback(
919
+ (input) => mutateAsync(input),
920
+ [mutateAsync]
921
+ );
922
+ const issues = mutation.data?.issues ?? [];
923
+ return {
924
+ validate,
925
+ // The server's verdict (undefined until the first call resolves).
926
+ publishable: mutation.data?.publishable,
927
+ issues,
928
+ isPending: mutation.isPending,
929
+ error: mutation.error,
930
+ // Connections must load before `validate` can resolve a channel.
931
+ isReady: connections.isSuccess,
932
+ connectedChannels: connected.map((connection) => connection.platform).filter(js.isPostPlatform)
933
+ };
934
+ }
897
935
  function useUpdatePost(postId) {
898
936
  const { client, queryClient } = usePostrun();
899
937
  return reactQuery.useMutation(
@@ -1671,5 +1709,6 @@ exports.useSelectAccount = useSelectAccount;
1671
1709
  exports.useUpdateMedia = useUpdateMedia;
1672
1710
  exports.useUpdatePost = useUpdatePost;
1673
1711
  exports.useUpdateProfile = useUpdateProfile;
1712
+ exports.useValidatePost = useValidatePost;
1674
1713
  //# sourceMappingURL=index.cjs.map
1675
1714
  //# sourceMappingURL=index.cjs.map