import { createFileRoute, Link } from '@tanstack/react-router' import { z } from 'zod' import { strapiApi } from '@/data/loaders' import { StrapiImage } from '@/components/strapi-image' import { Search } from '@/components/search' import { Pagination } from '@/components/pagination' import type { TArticle } from '@/types/strapi' type LoaderResult = { status: 'success' | 'empty' | 'error' articles: TArticle[] meta?: { pagination?: { page: number; pageCount: number; total: number } } error?: string query?: string } const searchSchema = z.object({ query: z.string().optional(), page: z.number().default(1), }) export const Route = createFileRoute('/demo/strapi')({ component: RouteComponent, validateSearch: searchSchema, loaderDeps: ({ search }) => ({ search }), loader: async ({ deps }): Promise => { const { query, page } = deps.search try { const response = await strapiApi.articles.getArticlesData({ data: { query, page }, }) // Check if we got data if (!response || !response.data) { return { status: 'empty', articles: [], meta: response?.meta, query, } } // Check if data array is empty if (response.data.length === 0) { return { status: 'empty', articles: [], meta: response.meta, query, } } return { status: 'success', articles: response.data, meta: response.meta, query, } } catch (error) { console.error('Strapi fetch error:', error) return { status: 'error', articles: [], error: error instanceof Error ? error.message : 'Failed to connect to Strapi', query, } } }, }) function StrapiServerInstructions() { return (

Start the Strapi Server

$ cd ../server

$ npm install

$ npm run develop

Then create an admin at{' '} http://localhost:1337/admin

) } function ConnectionError({ error }: { error?: string }) { return (

Cannot Connect to Strapi

Make sure your Strapi server is running at{' '} http://localhost:1337

{error &&

Error: {error}

}
) } function NoArticlesFound({ query }: { query?: string }) { if (query) { return (

No Results Found

No articles match your search for "{query}". Try adjusting your search terms.

) } return (

No Articles Yet

Your Strapi server is running, but there are no published articles. Create and publish your first article to see it here.

How to add articles:

  1. 1. Open{' '} Strapi Admin Panel
  2. 2. Go to{' '} Content Manager {' '} → Article
  3. 3. Click{' '} Create new entry
  4. 4. Fill in the details and click{' '} Publish
) } function RouteComponent() { const { status, articles, meta, error, query } = Route.useLoaderData() return (

CMS

Strapi Articles

{status === 'error' && } {status === 'empty' && } {status === 'success' && ( <>
{articles.map((article: TArticle) => (

{article.title || 'Untitled'}

{article.description && (

{article.description}

)}
{article.author?.name && ( By {article.author.name} )} {article.createdAt && ( {new Date(article.createdAt).toLocaleDateString()} )}
{article.category?.name && (
{article.category.name}
)}
))}
{meta?.pagination && meta.pagination.pageCount > 1 && (
)} )}
) }