class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
class PostsController < ApplicationController
def index
@posts = Post.published
.order(created_at: :desc)
.page(params[:page])
.per(20)
import { useInfiniteQuery } from '@tanstack/react-query'
import api from '@/services/api'
import { Post, PaginatedResponse } from '@/types'
export function useInfinitePosts() {
return useInfiniteQuery({
import { useCallback, useEffect, useState } from "react";
interface Options {
rootMargin?: string;
threshold?: number;
}
import { useCallback, useEffect, useRef, useState } from 'react';
export function usePaginatedFetch(fetchPage, { pageSize = 20 } = {}) {
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
export interface Page<T> {
items: T[];
nextCursor: string | null;
}
export interface Post {
function encodeCursor(row) {
if (!row) return null;
const payload = JSON.stringify({ t: row.created_at, id: row.id });
return Buffer.from(payload, 'utf8').toString('base64url');
}