@xhub-short/sdk 0.1.0-beta.0 → 0.1.0-beta.10

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.
Files changed (4) hide show
  1. package/README.md +148 -14
  2. package/dist/index.d.ts +2211 -902
  3. package/dist/index.js +3939 -1513
  4. package/package.json +9 -10
package/README.md CHANGED
@@ -257,9 +257,24 @@ SDK đảm bảo hoạt động với Server-Side Rendering:
257
257
  ├── @xhub-short/contracts (types, interfaces)
258
258
  ├── @xhub-short/core (domain logic)
259
259
  ├── @xhub-short/adapters (mock + preset adapters)
260
- └── react (peer dependency)
260
+ ├── @xhub-short/ui (headless components)
261
+ ├── react (peer dependency)
262
+ └── zustand (peer dependency)
261
263
  ```
262
264
 
265
+ ### Host App Installation
266
+
267
+ ```bash
268
+ npm install @xhub-short/sdk
269
+ ```
270
+
271
+ **That's it!** No other dependencies needed.
272
+
273
+ **Bundled internally:**
274
+ - `zustand` - state management (bundled in @xhub-short/core)
275
+ - `clsx` - class names utility (bundled in @xhub-short/ui)
276
+ - Custom `useTouchDrag` hook - gesture handling (bundled in @xhub-short/sdk)
277
+
263
278
  ---
264
279
 
265
280
  ## ✨ Chức năng chính
@@ -483,38 +498,88 @@ export type { FeedState, PlayerState, ResourceState, ... } from '@xhub-short/cor
483
498
 
484
499
  ## 🚀 Quick Start
485
500
 
486
- ### Basic Usage
501
+ ### Simplest Usage (Recommended)
487
502
 
488
503
  ```tsx
489
- import { ShortVideoProvider, useFeed, usePlayer } from '@xhub-short/sdk';
504
+ import { ShortVideoRoot, VideoFeed, DefaultSlot } from '@xhub-short/sdk';
490
505
 
491
506
  function App() {
492
507
  return (
493
- <ShortVideoProvider
508
+ <ShortVideoRoot
494
509
  config={{
495
510
  preset: {
496
511
  baseUrl: 'https://api.example.com/v1',
497
512
  auth: { getAccessToken: () => localStorage.getItem('token') },
498
513
  endpoints: {
499
514
  feed: { list: '/videos', detail: '/videos/:id' },
500
- interaction: { like: '/videos/:id/like', ... },
515
+ interaction: { like: '/videos/:id/like' },
501
516
  },
502
517
  },
503
518
  }}
519
+ errorMode="auto-skip-with-toast"
504
520
  >
505
- <VideoFeed />
506
- </ShortVideoProvider>
521
+ <VideoFeed renderSlot={(video, index) => (
522
+ <DefaultSlot
523
+ video={video}
524
+ index={index}
525
+ showBookmark={true}
526
+ onOpenComments={(videoId) => openSheet(videoId)}
527
+ onShare={(video) => shareVideo(video)}
528
+ />
529
+ )} />
530
+ </ShortVideoRoot>
507
531
  );
508
532
  }
509
-
510
- function VideoFeed() {
511
- const { videos, loadInitial, loadMore, hasMore } = useFeed();
512
- const { load, play, pause, isPlaying } = usePlayer();
513
-
514
- // Your UI here...
515
- }
516
533
  ```
517
534
 
535
+ **That's it!** The SDK handles everything: video loading, HLS playback, swipe navigation, like/follow actions, error handling, and more.
536
+
537
+ ### DefaultSlot Props
538
+
539
+ | Prop | Type | Default | Description |
540
+ |------|------|---------|-------------|
541
+ | `video` | `VideoItem` | required | Video data |
542
+ | `index` | `number` | required | Index in feed |
543
+ | `showPoster` | `boolean` | `true` | Show poster |
544
+ | `showLikeAnimation` | `boolean` | `true` | Double-tap hearts |
545
+ | `showAuthorInfo` | `boolean` | `true` | Avatar + follow |
546
+ | `showActionBar` | `boolean` | `true` | Like, comment, share |
547
+ | `showVideoInfo` | `boolean` | `true` | Caption, hashtags |
548
+ | `showProgressBar` | `boolean` | `true` | Progress bar |
549
+ | `showBookmark` | `boolean` | `false` | Bookmark button |
550
+ | `onOpenComments` | `(videoId) => void` | - | Comment click |
551
+ | `onShare` | `(video) => void` | - | Share click |
552
+ | `onOpenProfile` | `(author) => void` | - | Author click |
553
+ | `onHashtagClick` | `(tag) => void` | - | Hashtag click |
554
+
555
+ ### Custom Usage (Advanced)
556
+
557
+ For full control, use individual components:
558
+
559
+ ```tsx
560
+ import {
561
+ ShortVideoRoot, VideoFeed, VideoSlot, VideoPlayer,
562
+ ActionBar, AuthorInfo, VideoInfo, ProgressBar
563
+ } from '@xhub-short/sdk';
564
+ import { VideoSlotOverlay, VideoSlotPoster } from '@xhub-short/ui';
565
+
566
+ <VideoFeed renderSlot={(video, index) => (
567
+ <VideoSlot video={video} index={index}>
568
+ <VideoPlayer />
569
+ <VideoSlotPoster />
570
+ <VideoSlotOverlay>
571
+ <VideoSlotOverlay.Actions>
572
+ <AuthorInfo video={video} variant="avatar-badge" />
573
+ <ActionBar video={video} />
574
+ </VideoSlotOverlay.Actions>
575
+ <VideoSlotOverlay.Bottom>
576
+ <VideoInfo video={video} />
577
+ <ProgressBar showTime />
578
+ </VideoSlotOverlay.Bottom>
579
+ </VideoSlotOverlay>
580
+ </VideoSlot>
581
+ )} />
582
+
518
583
  ### With Custom Adapters
519
584
 
520
585
  ```tsx
@@ -532,6 +597,75 @@ function VideoFeed() {
532
597
 
533
598
  ---
534
599
 
600
+ ## 👤 Guest Mode (Chế độ khách)
601
+
602
+ SDK hỗ trợ chế độ khách cho phép người dùng chưa đăng nhập xem video và bình luận (read-only). Các tương tác (Like, Comment, Follow) sẽ bị chặn và trigger callback để Host App xử lý (ví dụ: hiện modal đăng nhập).
603
+
604
+ ### Cấu hình
605
+
606
+ ```tsx
607
+ <ShortVideoRoot
608
+ config={{
609
+ // ...
610
+ guest: {
611
+ /**
612
+ * 'auto': Tự động detect dựa trên auth token (default)
613
+ * true: Force guest mode
614
+ * false: Disable guest mode
615
+ */
616
+ mode: 'auto',
617
+
618
+ /**
619
+ * Callback khi guest thực hiện hành động cần auth (Like, Comment, Follow...)
620
+ */
621
+ onAction: (action, context) => {
622
+ console.log('Guest Action:', action, context);
623
+ // VD: showLoginModal();
624
+ }
625
+ }
626
+ }}
627
+ >
628
+ <VideoFeed />
629
+ </ShortVideoRoot>
630
+ ```
631
+
632
+ ### Các hành động được chặn
633
+ - **Like / Bookmark**: Trigger `onAction`
634
+ - **Follow**: Trigger `onAction`
635
+ - **Comment Input**: Hiển thị nút "Login to comment". Khi click triggers `onAction` (`action='comment'`).
636
+
637
+ ---
638
+
639
+ ## ⚡ Prefetch Data (Tải trước dữ liệu)
640
+
641
+ Cho phép Host App tải trước dữ liệu Feed (Reels) để trải nghiệm hiển thị tức thì (instant loading).
642
+
643
+ ### Cách sử dụng
644
+
645
+ Gọi hàm `prefetchFeed` trước khi mount SDK components (ví dụ: khi hover vào menu Reels hoặc trong route loader).
646
+
647
+ ```tsx
648
+ import { prefetchFeed } from '@xhub-short/sdk';
649
+
650
+ // 1. Prefetch data (lưu vào bộ nhớ tạm)
651
+ // Có thể gọi ở bất kỳ đâu trong Host App
652
+ await prefetchFeed(sdkConfig, { ttl: 5 * 60 * 1000 });
653
+
654
+ // ... Sau đó ...
655
+
656
+ // 2. Khi User vào trang Reels, SDK sẽ tự động dùng data đã fetch
657
+ <ShortVideoRoot config={sdkConfig}>
658
+ <VideoFeed />
659
+ </ShortVideoRoot>
660
+ ```
661
+
662
+ ### Đặc điểm
663
+ - **In-Memory Cache**: Dữ liệu chỉ lưu tạm trong RAM, mất khi refresh trang (đảm bảo fresh data).
664
+ - **Auto-Consume**: `ShortVideoRoot` tự động tiêu thụ cache và xóa sau khi dùng.
665
+ - **Performance**: Giúp giảm thời gian chờ đợi (LCP) xuống gần bằng 0 khi user chuyển trang.
666
+
667
+ ---
668
+
535
669
  ## 🔗 Liên kết với các Packages
536
670
 
537
671
  | Package | Quan hệ với SDK |