Skip to content

Commit 1fef3d7

Browse files
authored
docs(agent-tars): showcase build-time optimization (#1714)
1 parent e95134d commit 1fef3d7

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

multimodal/websites/docs/env.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
/// <reference types="@rspress/theme-default" />
2+
3+
// Virtual module for build-time injected showcase data
4+
declare module 'showcase-data' {
5+
import type { ApiShareItem } from './src/services/api';
6+
export const showcaseData: ApiShareItem[];
7+
export const lastUpdated: string;
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { RspressPlugin } from '@rspress/core';
2+
3+
/**
4+
* Rspress plugin to fetch showcase data at build time
5+
*/
6+
export function showcaseDataPlugin(): RspressPlugin {
7+
return {
8+
name: 'showcase-data-plugin',
9+
async addRuntimeModules() {
10+
try {
11+
const response = await fetch('https://agent-tars.toxichl1994.workers.dev/shares/public?page=1&limit=100');
12+
const data = await response.json();
13+
14+
return {
15+
'showcase-data': `export const showcaseData = ${JSON.stringify(data.success ? data.data : [])};`,
16+
};
17+
} catch {
18+
return {
19+
'showcase-data': 'export const showcaseData = [];',
20+
};
21+
}
22+
},
23+
};
24+
}

multimodal/websites/docs/rspress.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from '@rspress/core';
33
import mermaid from 'rspress-plugin-mermaid';
44

55
import { SEO_CONFIG } from './src/shared/seoConfig';
6+
import { showcaseDataPlugin } from './plugins/showcase-data-plugin';
67

78
const isProd = process.env.NODE_ENV === 'production';
89

@@ -81,6 +82,7 @@ export default defineConfig({
8182
fontSize: 16,
8283
},
8384
}),
85+
showcaseDataPlugin(),
8486
],
8587
themeConfig: {
8688
darkMode: false,

multimodal/websites/docs/src/hooks/useShowcaseData.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { useState, useEffect, useMemo } from 'react';
22
import { shareAPI, ApiShareItem } from '../services/api';
3-
import { processShowcaseData, ProcessedShowcaseData, ShowcaseItem } from '../services/dataProcessor';
3+
import {
4+
processShowcaseData,
5+
ProcessedShowcaseData,
6+
ShowcaseItem,
7+
} from '../services/dataProcessor';
8+
import { showcaseData } from 'showcase-data';
49

510
interface UseShowcaseDataResult {
611
items: ShowcaseItem[];
@@ -15,6 +20,9 @@ interface UseShowcaseDataProps {
1520
slug?: string | null;
1621
}
1722

23+
/**
24+
* Showcase data hook using build-time data for public shares
25+
*/
1826
export function useShowcaseData({
1927
sessionId,
2028
slug,
@@ -23,49 +31,30 @@ export function useShowcaseData({
2331
const [isLoading, setIsLoading] = useState(true);
2432
const [error, setError] = useState<string | null>(null);
2533

26-
// Process data only when apiItems change (performance optimization)
2734
const processedData = useMemo(() => {
2835
if (apiItems.length === 0) return null;
2936
return processShowcaseData(apiItems);
3037
}, [apiItems]);
3138

32-
// Extract items for backward compatibility
3339
const items = processedData?.items || [];
3440

3541
const fetchData = async () => {
3642
try {
3743
setIsLoading(true);
3844
setError(null);
3945

40-
if (sessionId) {
46+
if (!sessionId && !slug) {
47+
// Use build-time data for public shares
48+
setApiItems(showcaseData.length > 0 ? showcaseData : await shareAPI.getPublicShares(1, 100).then(r => r.data));
49+
} else if (sessionId) {
4150
const response = await shareAPI.getShare(sessionId);
42-
43-
if (response.success) {
44-
setApiItems([response.data]);
45-
} else {
46-
throw new Error(response.error || 'Failed to fetch share data');
47-
}
51+
setApiItems(response.success ? [response.data] : []);
4852
} else if (slug) {
4953
const response = await shareAPI.getShareBySlug(slug);
50-
51-
if (response.success) {
52-
setApiItems([response.data]);
53-
} else {
54-
throw new Error(response.error || `No share found with slug: ${slug}`);
55-
}
56-
} else {
57-
const response = await shareAPI.getPublicShares(1, 100);
58-
59-
if (response.success) {
60-
setApiItems(response.data);
61-
} else {
62-
throw new Error(response.error || 'Failed to fetch showcase data');
63-
}
54+
setApiItems(response.success ? [response.data] : []);
6455
}
6556
} catch (err) {
66-
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
67-
setError(errorMessage);
68-
console.error('Failed to fetch showcase data:', err);
57+
setError(err instanceof Error ? err.message : 'Unknown error');
6958
} finally {
7059
setIsLoading(false);
7160
}

0 commit comments

Comments
 (0)