π₯ Make videos programmatically with React
full prompt gallery ssr
Igor Samokhovets106bff6de2f7
diff --git a/packages/docs/route-plugin/index.js b/packages/docs/route-plugin/index.jsindex 25b4d2c7a6..3ddfbfbf7b 100644--- a/packages/docs/route-plugin/index.js+++ b/packages/docs/route-plugin/index.js@@ -30,12 +30,12 @@ module.exports = function () {}).filter(Boolean);- let promptSlugs = [];const promptsPath = path.join(__dirname, '../static/_raw/prompts.json');- if (fs.existsSync(promptsPath)) {- const prompts = JSON.parse(fs.readFileSync(promptsPath, 'utf-8'));- promptSlugs = prompts.map((p) => p.slug);- }+ const prompts = fs.existsSync(promptsPath)+ ? JSON.parse(fs.readFileSync(promptsPath, 'utf-8'))+ : [];+ const promptSlugs = prompts.map((p) => p.slug);+ const promptPageCount = Math.ceil(prompts.length / 12);if (templateSlugs.length === 0) {throw new Error('expected templates');@@ -45,10 +45,10 @@ module.exports = function () {throw new Error('expected experts');}- return {expertSlugs, templateSlugs, promptSlugs};+ return {expertSlugs, templateSlugs, promptSlugs, promptPageCount};},contentLoaded({- content: {expertSlugs, templateSlugs, promptSlugs},+ content: {expertSlugs, templateSlugs, promptSlugs, promptPageCount},actions,}) {expertSlugs.forEach((c) => {@@ -75,6 +75,16 @@ module.exports = function () {exact: true,});});+ Array.from({length: promptPageCount - 1}, (_, i) => i + 2).forEach(+ (pageNum) => {+ actions.addRoute({+ path: '/prompts/' + pageNum,+ component: '@site/src/components/PromptsGalleryPage.tsx',+ modules: {},+ exact: true,+ });+ },+ );},};};diff --git a/packages/docs/src/components/PromptsGalleryPage.tsx b/packages/docs/src/components/PromptsGalleryPage.tsxnew file mode 100644index 0000000000..041b2eab57--- /dev/null+++ b/packages/docs/src/components/PromptsGalleryPage.tsx@@ -0,0 +1,51 @@+import Head from '@docusaurus/Head';+import {useLocation} from '@docusaurus/router';+import useDocusaurusContext from '@docusaurus/useDocusaurusContext';+import type {Submission} from '@remotion/promo-pages/dist/prompts/prompt-types';+import {PromptsGalleryPage} from '@remotion/promo-pages/dist/prompts/PromptsGallery.js';+import Layout from '@theme/Layout';+import React from 'react';+import allPrompts from '../../static/_raw/prompts.json';+import {Seo} from './Seo';++const PROMPTS_PER_PAGE = 12;++export default () => {+ const location = useLocation();+ const context = useDocusaurusContext();++ const pageMatch = location.pathname.match(/\/prompts\/(\d+)$/);+ const currentPage = pageMatch ? parseInt(pageMatch[1], 10) : 1;+ const totalPages = Math.ceil(allPrompts.length / PROMPTS_PER_PAGE);++ const startIndex = (currentPage - 1) * PROMPTS_PER_PAGE;+ const prompts = (allPrompts as Submission[]).slice(+ startIndex,+ startIndex + PROMPTS_PER_PAGE,+ );++ const title =+ currentPage === 1+ ? 'Prompt Gallery | Remotion'+ : `Prompt Gallery - Page ${currentPage} | Remotion`;++ return (+ <Layout>+ <Head>+ {Seo.renderTitle(title)}+ {Seo.renderDescription(+ 'Browse AI-generated video prompts for Remotion Skills.',+ )}+ {Seo.renderImage(+ '/generated/articles-prompts-gallery.png',+ context.siteConfig.url,+ )}+ </Head>+ <PromptsGalleryPage+ prompts={prompts}+ currentPage={currentPage}+ totalPages={totalPages}+ />+ </Layout>+ );+};diff --git a/packages/docs/src/pages/prompts/index.tsx b/packages/docs/src/pages/prompts/index.tsxindex 18297e4cf0..49bd30ab23 100644--- a/packages/docs/src/pages/prompts/index.tsx+++ b/packages/docs/src/pages/prompts/index.tsx@@ -1,13 +1,18 @@import Head from '@docusaurus/Head';import useDocusaurusContext from '@docusaurus/useDocusaurusContext';+import type {Submission} from '@remotion/promo-pages/dist/prompts/prompt-types';import {PromptsGalleryPage} from '@remotion/promo-pages/dist/prompts/PromptsGallery.js';import Layout from '@theme/Layout';import React from 'react';-import prompts from '../../../static/_raw/prompts.json';+import allPrompts from '../../../static/_raw/prompts.json';import {Seo} from '../../components/Seo';+const PROMPTS_PER_PAGE = 12;+export default () => {const context = useDocusaurusContext();+ const totalPages = Math.ceil(allPrompts.length / PROMPTS_PER_PAGE);+ const prompts = (allPrompts as Submission[]).slice(0, PROMPTS_PER_PAGE);return (<Layout>@@ -21,7 +26,11 @@ export default () => {context.siteConfig.url,)}</Head>- <PromptsGalleryPage prompts={prompts} />+ <PromptsGalleryPage+ prompts={prompts}+ currentPage={1}+ totalPages={totalPages}+ /></Layout>);};diff --git a/packages/promo-pages/src/components/prompts/PromptsGallery.tsx b/packages/promo-pages/src/components/prompts/PromptsGallery.tsxindex 96c51d883e..2065670afc 100644--- a/packages/promo-pages/src/components/prompts/PromptsGallery.tsx+++ b/packages/promo-pages/src/components/prompts/PromptsGallery.tsx@@ -1,7 +1,6 @@import {Button, Card} from '@remotion/design';-import React, {useCallback, useEffect, useRef, useState} from 'react';+import React, {useEffect, useRef, useState} from 'react';import {CardLikeButton} from './CardLikeButton';-import {REMOTION_PRO_ORIGIN} from './config';import {Page} from './Page';import {getAuthorName, getAvatarUrl} from './prompt-helpers';import type {Submission} from './prompt-types';@@ -82,39 +81,102 @@ const SubmissionCard: React.FC<{readonly submission: Submission}> = ({);};-export const PromptsGalleryPage: React.FC<{- readonly prompts?: Submission[];-}> = ({prompts: promptsProp}) => {- const [submissions, setSubmissions] = useState<Submission[]>(- promptsProp ?? [],- );- const [nextCursor, setNextCursor] = useState<string | null>(null);- const [loading, setLoading] = useState(false);-- const fetchPage = useCallback((cursor?: string) => {- setLoading(true);- const url = cursor- ? `${REMOTION_PRO_ORIGIN}/api/prompts?cursor=${cursor}`- : `${REMOTION_PRO_ORIGIN}/api/prompts`;-- fetch(url)- .then((res) => res.json())- .then((data) => {- setSubmissions((prev) =>- cursor ? [...prev, ...data.items] : data.items,- );- setNextCursor(data.nextCursor);- })- .catch(() => {})- .finally(() => setLoading(false));- }, []);+const Pagination: React.FC<{+ readonly currentPage: number;+ readonly totalPages: number;+}> = ({currentPage, totalPages}) => {+ if (totalPages <= 1) {+ return null;+ }- useEffect(() => {- if (!promptsProp) {- fetchPage();+ const getPageUrl = (page: number) => {+ return page === 1 ? '/prompts' : `/prompts/${page}`;+ };++ const getVisiblePages = () => {+ const pages: (number | 'ellipsis-start' | 'ellipsis-end')[] = [];++ pages.push(1);++ if (currentPage > 3) {+ pages.push('ellipsis-start');}- }, [fetchPage, promptsProp]);++ for (+ let i = Math.max(2, currentPage - 1);+ i <= Math.min(totalPages - 1, currentPage + 1);+ i+++ ) {+ if (!pages.includes(i)) {+ pages.push(i);+ }+ }++ if (currentPage < totalPages - 2) {+ pages.push('ellipsis-end');+ }++ if (totalPages > 1 && !pages.includes(totalPages)) {+ pages.push(totalPages);+ }++ return pages;+ };+ return (+ <div className="flex items-center justify-center gap-4 mt-12 font-brand text-sm">+ {currentPage > 1 ? (+ <a+ href={getPageUrl(currentPage - 1)}+ className="text-muted-foreground hover:text-text no-underline hover:no-underline"+ >+ β Previous+ </a>+ ) : (+ <span className="text-gray-300 cursor-not-allowed">β Previous</span>+ )}++ <span className="flex items-center gap-1">+ {getVisiblePages().map((page) =>+ typeof page === 'string' ? (+ <span key={page} className="px-1 text-muted-foreground">+ ...+ </span>+ ) : (+ <a+ key={page}+ href={getPageUrl(page)}+ className={`px-2 py-1 no-underline hover:no-underline ${+ page === currentPage+ ? 'text-text font-bold'+ : 'text-muted-foreground hover:text-text'+ }`}+ >+ {page}+ </a>+ ),+ )}+ </span>++ {currentPage < totalPages ? (+ <a+ href={getPageUrl(currentPage + 1)}+ className="text-muted-foreground hover:text-text no-underline hover:no-underline"+ >+ Next β+ </a>+ ) : (+ <span className="text-gray-300 cursor-not-allowed">Next β</span>+ )}+ </div>+ );+};++export const PromptsGalleryPage: React.FC<{+ readonly prompts: Submission[];+ readonly currentPage: number;+ readonly totalPages: number;+}> = ({prompts, currentPage, totalPages}) => {return (<Page className="flex-col"><div className="m-auto max-w-[1200px] w-full px-4 py-12">@@ -166,18 +228,12 @@ export const PromptsGalleryPage: React.FC<{<div className="h-12" /><div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">- {submissions.map((s) => (+ {prompts.map((s) => (<SubmissionCard key={s.id} submission={s} />))}</div>- {loading && <div className="text-center text-muted-foreground mt-8" />}-- {nextCursor && !loading && (- <div className="text-center mt-8">- <Button onClick={() => fetchPage(nextCursor)}>Load more</Button>- </div>- )}+ <Pagination currentPage={currentPage} totalPages={totalPages} /></div></Page>);diff --git a/packages/promo-pages/src/prompts.tsx b/packages/promo-pages/src/prompts.tsxindex 63f97fddaa..75834d38a1 100644--- a/packages/promo-pages/src/prompts.tsx+++ b/packages/promo-pages/src/prompts.tsx@@ -6,7 +6,7 @@ import './index.css';createRoot(document.getElementById('root')!).render(<StrictMode><div data-theme="light">- <PromptsGalleryPage />+ <PromptsGalleryPage prompts={[]} currentPage={1} totalPages={1} /></div></StrictMode>,);