@postrun/react 0.2.0 → 1.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.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use client";
2
2
  import { createContext, memo, useMemo, useState, useEffect, Fragment as Fragment$1, useRef, createElement, useContext, useCallback } from 'react';
3
3
  import { useInfiniteQuery, useQuery, useMutation, QueryClient } from '@tanstack/react-query';
4
- import { createPostrunClient, profilesList, profilesGet, profilesCreate, profilesUpdate, profilesDelete, connectionsConnect, connectionsListByProfile, connectionsGet, connectionsListAccounts, connectionsSelect, connectionsDelete, mediaCreate, mediaGet, mediaList, mediaUpdate, mediaDelete, postsList, postsGet, postsCreate, buildCreatePost, isPostPlatform, postsUpdate, postsDelete } from '@postrun/js';
4
+ import { createPostrunClient, profilesList, profilesGet, profilesCreate, profilesUpdate, profilesDelete, connectionsConnect, connectionsListByProfile, connectionsGet, connectionsListAccounts, connectionsSelect, connectionsDelete, mediaGet, mediaList, mediaUpdate, mediaDelete, postsList, postsGet, postsCreate, buildCreatePost, isPostPlatform, postsUpdate, postsDelete, mediaCreate, PostrunError } from '@postrun/js';
5
+ import Nango, { AuthError } from '@nangohq/frontend';
6
+ import pWaitFor, { TimeoutError } from 'p-wait-for';
7
+ import pLimit from 'p-limit';
5
8
  import pRetry, { AbortError } from 'p-retry';
6
- import pWaitFor from 'p-wait-for';
7
9
  import axios, { isAxiosError } from 'axios';
8
10
  import { enrichTweet, TweetContainer, TweetHeader, TweetInReplyTo, TweetBody, TweetMedia, QuotedTweet } from 'react-tweet';
9
11
  import { FiMessageCircle, FiRepeat, FiHeart, FiBarChart2, FiShare, FiGlobe, FiUsers, FiThumbsUp, FiMessageSquare, FiSend } from 'react-icons/fi';
@@ -194,29 +196,245 @@ function useDeleteProfile() {
194
196
  queryClient
195
197
  );
196
198
  }
197
-
198
- // src/navigate.ts
199
- function navigate(url) {
200
- window.location.assign(url);
199
+ var PENDING = { status: "connected_pending" };
200
+ var CANCELLED = { status: "cancelled" };
201
+ var active = (connection) => ({
202
+ status: "active",
203
+ connection
204
+ });
205
+ var failed = (reason) => ({
206
+ status: "error",
207
+ reason
208
+ });
209
+ function outcomeForAuthError(error) {
210
+ switch (error.type) {
211
+ case "window_closed":
212
+ return CANCELLED;
213
+ case "blocked_by_browser":
214
+ return failed("popup_blocked");
215
+ case "missing_auth_token":
216
+ case "invalid_host_url":
217
+ case "missing_credentials":
218
+ case "connection_test_failed":
219
+ case "missing_connect_session_token":
220
+ case "connection_validation_failed":
221
+ case "resource_capped":
222
+ case "unknown_error":
223
+ return failed("auth_failed");
224
+ }
225
+ }
226
+ async function grant(authorize) {
227
+ try {
228
+ return { ok: true, connectionId: await authorize() };
229
+ } catch (error) {
230
+ if (error instanceof AuthError) {
231
+ return { ok: false, outcome: outcomeForAuthError(error) };
232
+ }
233
+ return { ok: false, outcome: failed("auth_failed") };
234
+ }
235
+ }
236
+ async function awaitGrantedConnection(deps, nangoConnectionId) {
237
+ try {
238
+ return await pWaitFor(
239
+ async () => {
240
+ const rows = await deps.listByNangoConnectionId(nangoConnectionId);
241
+ const match = rows[0];
242
+ return match ? pWaitFor.resolveWith(match) : false;
243
+ },
244
+ { interval: deps.pollIntervalMs, timeout: deps.pollTimeoutMs }
245
+ );
246
+ } catch (error) {
247
+ if (error instanceof TimeoutError) {
248
+ return null;
249
+ }
250
+ throw error;
251
+ }
252
+ }
253
+ async function bindPendingConnection(deps, connection) {
254
+ let accounts;
255
+ try {
256
+ accounts = await deps.discoverAccounts(connection.id);
257
+ } catch (error) {
258
+ if (error instanceof PostrunError && error.code === "not_implemented") {
259
+ return PENDING;
260
+ }
261
+ throw error;
262
+ }
263
+ if (accounts.length === 0) {
264
+ return PENDING;
265
+ }
266
+ const chosen = await deps.chooseAccount(accounts);
267
+ try {
268
+ return active(await deps.selectAccount(connection.id, chosen));
269
+ } catch (error) {
270
+ if (error instanceof PostrunError) {
271
+ return error.code === "connection_reauth_required" ? failed("reauth_required") : failed("select_failed");
272
+ }
273
+ throw error;
274
+ }
275
+ }
276
+ async function runEmbeddedConnect(deps) {
277
+ const granted = await grant(deps.authorize);
278
+ if (!granted.ok) {
279
+ return granted.outcome;
280
+ }
281
+ let connection;
282
+ try {
283
+ connection = await awaitGrantedConnection(deps, granted.connectionId);
284
+ } catch {
285
+ return failed("connection_not_found");
286
+ }
287
+ if (connection === null) {
288
+ return PENDING;
289
+ }
290
+ if (connection.external_account_id !== null) {
291
+ return active(connection);
292
+ }
293
+ try {
294
+ return await bindPendingConnection(deps, connection);
295
+ } catch {
296
+ return failed("select_failed");
297
+ }
201
298
  }
202
299
 
203
300
  // src/connections.ts
204
- function useConnect() {
205
- const { client, queryClient } = usePostrun();
206
- return useMutation(
207
- {
208
- mutationFn: async ({ profileId, platform }) => {
209
- const session = (await connectionsConnect({
301
+ var POLL_INTERVAL_MS = 1500;
302
+ var POLL_TIMEOUT_MS = 15e3;
303
+ function useConnect({
304
+ profileId,
305
+ platform,
306
+ onConnected
307
+ }) {
308
+ const { client } = usePostrun();
309
+ const [state, setState] = useState({ phase: "preparing" });
310
+ const [remintNonce, setRemintNonce] = useState(0);
311
+ const sessionRef = useRef(null);
312
+ const pickRef = useRef(null);
313
+ const inFlightRef = useRef(false);
314
+ const flowGenRef = useRef(0);
315
+ const onConnectedRef = useRef(onConnected);
316
+ useEffect(() => {
317
+ onConnectedRef.current = onConnected;
318
+ }, [onConnected]);
319
+ const abandonFlow = useCallback(() => {
320
+ flowGenRef.current += 1;
321
+ inFlightRef.current = false;
322
+ const pick = pickRef.current;
323
+ pickRef.current = null;
324
+ pick?.reject(new Error("connect flow abandoned"));
325
+ }, []);
326
+ useEffect(() => {
327
+ let abandoned = false;
328
+ setState({ phase: "preparing" });
329
+ sessionRef.current = null;
330
+ connectionsConnect({ client, path: { id: profileId }, body: { platform } }).then(({ data }) => {
331
+ if (abandoned || !data) return;
332
+ sessionRef.current = {
333
+ token: data.connect_session_token,
334
+ providerConfigKey: data.provider_config_key,
335
+ host: data.nango_host
336
+ };
337
+ setState({ phase: "idle" });
338
+ }).catch(() => {
339
+ if (!abandoned) setState({ phase: "error", reason: "auth_failed" });
340
+ });
341
+ return () => {
342
+ abandoned = true;
343
+ abandonFlow();
344
+ };
345
+ }, [client, profileId, platform, remintNonce, abandonFlow]);
346
+ const start = useCallback(() => {
347
+ const session = sessionRef.current;
348
+ if (!session || inFlightRef.current) return;
349
+ inFlightRef.current = true;
350
+ const gen = flowGenRef.current;
351
+ const isCurrent = () => flowGenRef.current === gen;
352
+ setState({ phase: "connecting" });
353
+ void runEmbeddedConnect({
354
+ // Nango lives INSIDE `authorize` so a SYNCHRONOUS throw (invalid host /
355
+ // missing token — the Nango SDK throws `AuthError` synchronously) becomes a
356
+ // promise rejection that `grant()` maps to `auth_failed`, never an uncaught
357
+ // throw escaping the click and wedging `inFlightRef`. Gesture timing still
358
+ // holds: `authorize()` is invoked SYNCHRONOUSLY down the
359
+ // start → runEmbeddedConnect → grant chain (each `await`'s operand is
360
+ // evaluated before it suspends), so `nango.auth()`'s `window.open` fires
361
+ // inside the user gesture, with no `await` before it.
362
+ authorize: async () => {
363
+ const nango = new Nango({
364
+ host: session.host,
365
+ connectSessionToken: session.token
366
+ });
367
+ const result = await nango.auth(session.providerConfigKey, {
368
+ detectClosedAuthWindow: true
369
+ });
370
+ return result.connectionId;
371
+ },
372
+ chooseAccount: (accounts) => new Promise((resolve, reject) => {
373
+ if (!isCurrent()) {
374
+ reject(new Error("connect flow abandoned"));
375
+ return;
376
+ }
377
+ pickRef.current = { resolve, reject };
378
+ setState({ phase: "picking", accounts });
379
+ }),
380
+ listByNangoConnectionId: async (nangoConnectionId) => {
381
+ const { data } = await connectionsListByProfile({
210
382
  client,
211
383
  path: { id: profileId },
212
- body: { platform }
213
- })).data;
214
- navigate(session.hosted_connect_url);
215
- return session;
384
+ query: { nango_connection_id: nangoConnectionId }
385
+ });
386
+ return data?.data ?? [];
387
+ },
388
+ discoverAccounts: async (connectionId) => {
389
+ const { data } = await connectionsListAccounts({
390
+ client,
391
+ path: { id: connectionId }
392
+ });
393
+ return data?.data ?? [];
394
+ },
395
+ selectAccount: async (connectionId, externalAccountId) => {
396
+ const { data } = await connectionsSelect({
397
+ client,
398
+ path: { id: connectionId },
399
+ body: { external_account_id: externalAccountId }
400
+ });
401
+ if (!data) throw new Error("select returned no connection");
402
+ return data;
403
+ },
404
+ pollIntervalMs: POLL_INTERVAL_MS,
405
+ pollTimeoutMs: POLL_TIMEOUT_MS
406
+ }).then((outcome) => {
407
+ if (!isCurrent()) return;
408
+ inFlightRef.current = false;
409
+ pickRef.current = null;
410
+ switch (outcome.status) {
411
+ case "active":
412
+ setState({ phase: "active", connection: outcome.connection });
413
+ onConnectedRef.current?.(outcome.connection);
414
+ return;
415
+ case "connected_pending":
416
+ setState({ phase: "connected_pending" });
417
+ return;
418
+ case "cancelled":
419
+ setState({ phase: "cancelled" });
420
+ return;
421
+ case "error":
422
+ setState({ phase: "error", reason: outcome.reason });
423
+ return;
216
424
  }
217
- },
218
- queryClient
219
- );
425
+ });
426
+ }, [client, profileId]);
427
+ const select = useCallback((externalAccountId) => {
428
+ const pick = pickRef.current;
429
+ if (!pick) return;
430
+ pickRef.current = null;
431
+ setState({ phase: "connecting" });
432
+ pick.resolve(externalAccountId);
433
+ }, []);
434
+ const reset = useCallback(() => {
435
+ setRemintNonce((n) => n + 1);
436
+ }, []);
437
+ return { state, start, select, reset };
220
438
  }
221
439
  function useConnections(profileId, filter) {
222
440
  const { client, queryClient } = usePostrun();
@@ -282,6 +500,17 @@ function useDisconnect() {
282
500
  queryClient
283
501
  );
284
502
  }
503
+
504
+ // src/Connect.tsx
505
+ function Connect({
506
+ profileId,
507
+ platform,
508
+ onConnected,
509
+ children
510
+ }) {
511
+ const api = useConnect({ profileId, platform, onConnected });
512
+ return children(api);
513
+ }
285
514
  var UploadError = class extends Error {
286
515
  status;
287
516
  constructor(status, message) {
@@ -322,17 +551,7 @@ async function uploadBytes(target, file, options = {}) {
322
551
  }
323
552
 
324
553
  // src/media.ts
325
- var DOCUMENT_MIME = /^application\/(pdf|msword|vnd\.(openxmlformats-officedocument\.(wordprocessingml\.document|presentationml\.presentation)|ms-powerpoint))$/;
326
- function inferKind(contentType) {
327
- if (contentType === "image/gif") return "gif";
328
- if (contentType.startsWith("image/")) return "image";
329
- if (contentType.startsWith("video/")) return "video";
330
- if (DOCUMENT_MIME.test(contentType)) return "document";
331
- throw new Error(
332
- `Could not infer media kind from "${contentType}". Pass { kind } explicitly.`
333
- );
334
- }
335
- async function pollUntilSettled(client, id, signal) {
554
+ async function pollUntilSettled(client, id, signal, onTick) {
336
555
  let latest;
337
556
  await pWaitFor(
338
557
  async () => {
@@ -340,6 +559,7 @@ async function pollUntilSettled(client, id, signal) {
340
559
  throw new DOMException("Upload aborted", "AbortError");
341
560
  }
342
561
  latest = (await mediaGet({ client, path: { id } })).data;
562
+ onTick?.(latest);
343
563
  return latest.status === "ready" || latest.status === "failed";
344
564
  },
345
565
  { interval: 1500, timeout: 3e5 }
@@ -349,93 +569,134 @@ async function pollUntilSettled(client, id, signal) {
349
569
  }
350
570
  return latest;
351
571
  }
352
- function useMediaUpload() {
353
- const { client, queryClient } = usePostrun();
354
- const [status, setStatus] = useState("idle");
355
- const [progress, setProgress] = useState(0);
356
- const [media, setMedia] = useState(null);
357
- const [error, setError] = useState(null);
358
- const abortRef = useRef(null);
359
- const upload = useCallback(
360
- async (file, options) => {
361
- const contentType = options.contentType || file.type;
362
- if (!contentType) {
363
- throw new Error(
364
- "Could not determine the file's content type. Pass { contentType } explicitly."
365
- );
366
- }
367
- const kind = options.kind ?? inferKind(contentType);
368
- abortRef.current?.abort();
369
- const controller = new AbortController();
370
- abortRef.current = controller;
371
- setStatus("uploading");
372
- setProgress(0);
373
- setMedia(null);
374
- setError(null);
375
- try {
376
- const created = (await mediaCreate({
377
- client,
378
- body: {
379
- profile_id: options.profileId,
380
- kind,
381
- content_type: contentType,
382
- targets: options.targets,
383
- raw: options.raw,
384
- alt_text: options.altText,
385
- external_id: options.externalId,
386
- metadata: options.metadata
572
+ async function runUpload(client, file, options, signal, callbacks) {
573
+ const created = (await mediaCreate({
574
+ client,
575
+ body: {
576
+ profile_id: options.profileId,
577
+ kind: options.kind,
578
+ content_type: options.contentType,
579
+ targets: options.targets,
580
+ raw: options.raw,
581
+ alt_text: options.altText,
582
+ external_id: options.externalId,
583
+ metadata: options.metadata
584
+ }
585
+ })).data;
586
+ if (created.upload) {
587
+ const target = created.upload;
588
+ await pRetry(
589
+ async () => {
590
+ try {
591
+ await uploadBytes(target, file, {
592
+ onProgress: callbacks.onProgress,
593
+ signal
594
+ });
595
+ } catch (uploadError) {
596
+ if (uploadError instanceof UploadError && uploadError.status >= 400 && uploadError.status < 500) {
597
+ throw new AbortError(uploadError);
387
598
  }
388
- })).data;
389
- if (created.upload) {
390
- const target = created.upload;
391
- await pRetry(
392
- async () => {
393
- try {
394
- await uploadBytes(target, file, {
395
- onProgress: setProgress,
396
- signal: controller.signal
397
- });
398
- } catch (uploadError) {
399
- if (uploadError instanceof UploadError && uploadError.status >= 400 && uploadError.status < 500) {
400
- throw new AbortError(uploadError);
401
- }
402
- throw uploadError;
403
- }
404
- },
405
- { retries: 3, signal: controller.signal }
406
- );
407
- }
408
- setStatus("processing");
409
- const settled = await pollUntilSettled(
410
- client,
411
- created.id,
412
- controller.signal
413
- );
414
- queryClient.setQueryData(mediaKeys.detail(created.id), settled);
415
- void queryClient.invalidateQueries({ queryKey: mediaKeys.lists() });
416
- setMedia(settled);
417
- setStatus(settled.status === "failed" ? "failed" : "ready");
418
- return settled;
419
- } catch (caught) {
420
- setError(caught);
421
- setStatus("failed");
422
- throw caught;
423
- } finally {
424
- if (abortRef.current === controller) {
425
- abortRef.current = null;
599
+ throw uploadError;
426
600
  }
601
+ },
602
+ { retries: 3, signal }
603
+ );
604
+ }
605
+ callbacks.onProcessing();
606
+ return pollUntilSettled(client, created.id, signal, callbacks.onPoll);
607
+ }
608
+ function toFileArray(files) {
609
+ if (files instanceof File) return [files];
610
+ return Array.from(files);
611
+ }
612
+ function useMediaUpload(options) {
613
+ const { client, queryClient } = usePostrun();
614
+ const [items, setItems] = useState([]);
615
+ const controllers = useRef(/* @__PURE__ */ new Map());
616
+ const limitRef = useRef(null);
617
+ if (!limitRef.current) {
618
+ limitRef.current = pLimit(options?.concurrency ?? 3);
619
+ }
620
+ const patch = useCallback(
621
+ (id, changes) => {
622
+ setItems(
623
+ (current) => current.map(
624
+ (item) => item.id === id ? { ...item, ...changes } : item
625
+ )
626
+ );
627
+ },
628
+ []
629
+ );
630
+ const add = useCallback(
631
+ (files, uploadOptions) => {
632
+ const queued = toFileArray(files).map((file) => ({
633
+ id: crypto.randomUUID(),
634
+ file,
635
+ status: "uploading",
636
+ progress: 0,
637
+ media: null,
638
+ error: null
639
+ }));
640
+ setItems((current) => [...current, ...queued]);
641
+ const limit = limitRef.current;
642
+ if (!limit) {
643
+ return Promise.resolve([]);
427
644
  }
645
+ const settlements = queued.map((item) => {
646
+ const controller = new AbortController();
647
+ controllers.current.set(item.id, controller);
648
+ return limit(() => {
649
+ if (controller.signal.aborted) {
650
+ throw new DOMException("Upload aborted", "AbortError");
651
+ }
652
+ return runUpload(client, item.file, uploadOptions, controller.signal, {
653
+ onProgress: (progress) => patch(item.id, { progress }),
654
+ onProcessing: () => patch(item.id, { status: "processing" }),
655
+ // Live server progress (stage + percent) each poll tick.
656
+ onPoll: (media) => patch(item.id, { media })
657
+ });
658
+ }).then((settled) => {
659
+ patch(item.id, {
660
+ status: settled.status === "failed" ? "failed" : "ready",
661
+ media: settled,
662
+ progress: 1
663
+ });
664
+ queryClient.setQueryData(mediaKeys.detail(settled.id), settled);
665
+ void queryClient.invalidateQueries({ queryKey: mediaKeys.lists() });
666
+ return settled;
667
+ }).catch((error) => {
668
+ if (controller.signal.aborted) {
669
+ return null;
670
+ }
671
+ patch(item.id, { status: "failed", error });
672
+ return null;
673
+ }).finally(() => {
674
+ controllers.current.delete(item.id);
675
+ });
676
+ });
677
+ return Promise.all(settlements).then(
678
+ (results) => results.filter((result) => result !== null)
679
+ );
428
680
  },
429
- [client, queryClient]
681
+ [client, queryClient, patch]
430
682
  );
431
- const cancel = useCallback(() => abortRef.current?.abort(), []);
683
+ const remove = useCallback((id) => {
684
+ controllers.current.get(id)?.abort();
685
+ controllers.current.delete(id);
686
+ setItems((current) => current.filter((item) => item.id !== id));
687
+ }, []);
432
688
  const reset = useCallback(() => {
433
- setStatus("idle");
434
- setProgress(0);
435
- setMedia(null);
436
- setError(null);
689
+ controllers.current.forEach((controller) => controller.abort());
690
+ controllers.current.clear();
691
+ setItems([]);
437
692
  }, []);
438
- return { upload, cancel, reset, status, progress, media, error };
693
+ const ready = items.flatMap(
694
+ (item) => item.status === "ready" && item.media ? [item.media] : []
695
+ );
696
+ const isUploading = items.some(
697
+ (item) => item.status === "uploading" || item.status === "processing"
698
+ );
699
+ return { items, ready, isUploading, add, remove, reset };
439
700
  }
440
701
  function useMedia(id) {
441
702
  const { client, queryClient } = usePostrun();
@@ -1317,6 +1578,6 @@ function LinkedInPostPreviewImpl({
1317
1578
  }
1318
1579
  var LinkedInPostPreview = memo(LinkedInPostPreviewImpl);
1319
1580
 
1320
- export { LinkedInPostPreview, PostrunProvider, UploadError, XPostPreview, connectionKeys, mediaKeys, postKeys, profileKeys, useCalendar, useConnect, useConnection, useConnections, useCreatePost, useCreateProfile, useDeleteMedia, useDeletePost, useDeleteProfile, useDisconnect, useDiscoverableAccounts, useInfiniteList, useMedia, useMediaInfinite, useMediaList, useMediaUpload, usePost, usePostrun, usePosts, usePostsInfinite, useProfile, useProfiles, useProfilesInfinite, useSelectAccount, useUpdateMedia, useUpdatePost, useUpdateProfile };
1581
+ export { Connect, LinkedInPostPreview, PostrunProvider, UploadError, XPostPreview, connectionKeys, mediaKeys, postKeys, profileKeys, useCalendar, useConnect, useConnection, useConnections, useCreatePost, useCreateProfile, useDeleteMedia, useDeletePost, useDeleteProfile, useDisconnect, useDiscoverableAccounts, useInfiniteList, useMedia, useMediaInfinite, useMediaList, useMediaUpload, usePost, usePostrun, usePosts, usePostsInfinite, useProfile, useProfiles, useProfilesInfinite, useSelectAccount, useUpdateMedia, useUpdatePost, useUpdateProfile };
1321
1582
  //# sourceMappingURL=index.js.map
1322
1583
  //# sourceMappingURL=index.js.map