Skip to content

Interactive Paginator

Paginator is an interactive multi-page navigation utility (Layer 4.3) for presenting long lists, catalogs, or search results in WhatsApp without spamming the chat.

It allows users to paginate through items interactively using text keywords (e.g., next, prev, 1, 2) or interactive button clicks.


Basic Paginator Example

javascript
import { Paginator } from 'leaves-guardian';

const items = [
  'Item 1: Premium Coffee',
  'Item 2: Green Tea Matcha',
  'Item 3: Dark Chocolate Croissant',
  'Item 4: Sourdough Bread',
  'Item 5: Vanilla Latte',
  'Item 6: Espresso Roast',
  'Item 7: Blueberry Muffin',
  'Item 8: Earl Grey Tea'
];

client.on('message', async (msg) => {
  if (msg.text === '!menu') {
    const paginator = new Paginator(client, {
      items,
      itemsPerPage: 3,
      timeout: 60000, // Inactivity timeout in ms
      pageRenderer: async (pageItems, pageInfo) => {
        return `
📜 *Product Catalog* (Page ${pageInfo.currentPage}/${pageInfo.totalPages})
---------------------------------------
${pageItems.map((item, idx) => `${pageInfo.startIndex + idx + 1}. ${item}`).join('\n')}
---------------------------------------
💡 *Navigasi*: Balas \`next\` / \`prev\` atau nomor halaman.
        `.trim();
      }
    });

    // Start pagination in this conversation
    await paginator.start(msg.chat.id, msg.sender.id);
  }
});

Paginator Options

OptionTypeDefaultDescription
itemsArray[]The full array of data items to paginate.
itemsPerPagenumber5Number of items displayed per page.
timeoutnumber60000Inactivity duration in ms before the paginator automatically closes.
pageRendererFunctionDefaultFunction async (pageItems, pageInfo, context) => string | Builder generating the page output.

The pageInfo Object

The pageRenderer callback receives pageInfo containing:

  • pageInfo.currentPage: Current 1-based page number.
  • pageInfo.totalPages: Total calculated pages.
  • pageInfo.totalItems: Total number of items.
  • pageInfo.startIndex: Zero-based index of the first item on this page.
  • pageInfo.endIndex: Zero-based index of the last item on this page.
  • pageInfo.hasNext: Boolean flag whether a next page exists.
  • pageInfo.hasPrev: Boolean flag whether a previous page exists.

The paginator automatically recognizes navigation triggers from both plain text responses and interactive button payloads:

ActionRecognized Text Commands & Button Payloads
PAGINATOR_ACTIONS.NEXTnext, n, >, , selanjutnya, lanjut, pag_next
PAGINATOR_ACTIONS.PREVprev, p, <, , sebelumnya, kembali, pag_prev
PAGINATOR_ACTIONS.FIRSTfirst, <<, , awal, pertama, pag_first
PAGINATOR_ACTIONS.LASTlast, >>, , akhir, terakhir, pag_last
PAGINATOR_ACTIONS.JUMPEntering any direct page number (e.g., 1, 2, 3)
PAGINATOR_ACTIONS.STOPstop, close, tutup, x, , batal, exit, selesai, pag_stop

Paginator Lifecycle States (PAGINATOR_STATES)

StateDescription
PAGINATOR_STATES.IDLECreated, not yet started.
PAGINATOR_STATES.RUNNINGActively listening for user page navigation.
PAGINATOR_STATES.STOPPEDStopped manually or via close keyword.
PAGINATOR_STATES.TIMEOUTClosed due to inactivity timeout.
PAGINATOR_STATES.SHUTDOWNClosed because client disconnected.
PAGINATOR_STATES.ERRORStopped due to runtime error.

Released under the MIT License.