@umituz/react-native-google-stitch-sdk 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/skills/SKILL.md +674 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-google-stitch-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "React Native wrapper for Google Labs Stitch SDK with TypeScript support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
},
|
|
60
60
|
"files": [
|
|
61
61
|
"src",
|
|
62
|
+
"skills",
|
|
62
63
|
"README.md",
|
|
63
64
|
"LICENSE"
|
|
64
65
|
]
|
package/skills/SKILL.md
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: setup-react-native-google-stitch-sdk
|
|
3
|
+
description: Sets up React Native wrapper for Google Labs Stitch SDK - AI-powered UI screen generation from text prompts. Triggers on: Setup Stitch SDK, Google Stitch, UI generation, screen generation, AI screens, useStitch, generateScreen, @umituz/react-native-google-stitch-sdk.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Setup React Native Google Stitch SDK
|
|
7
|
+
|
|
8
|
+
Comprehensive setup for `@umituz/react-native-google-stitch-sdk` - React Native wrapper for Google Labs Stitch SDK with AI-powered UI screen generation.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
This package provides a React Native wrapper for Google Labs Stitch SDK:
|
|
13
|
+
- Generate UI screens from text prompts using AI
|
|
14
|
+
- Get HTML and screenshot URLs for generated screens
|
|
15
|
+
- Edit screens with natural language
|
|
16
|
+
- Generate design variants
|
|
17
|
+
- Project and screen management
|
|
18
|
+
- Full TypeScript support
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
Just say: **"Setup Google Stitch SDK in my app"** and this skill will handle everything.
|
|
23
|
+
|
|
24
|
+
**Features Included:**
|
|
25
|
+
- AI-powered screen generation from text
|
|
26
|
+
- HTML export for generated screens
|
|
27
|
+
- Screenshot/image URLs
|
|
28
|
+
- Screen editing and variants
|
|
29
|
+
- Project management
|
|
30
|
+
- MCP tool access
|
|
31
|
+
|
|
32
|
+
## When to Use
|
|
33
|
+
|
|
34
|
+
Invoke this skill when you need to:
|
|
35
|
+
- Add AI UI screen generation to your app
|
|
36
|
+
- Generate screens from text prompts
|
|
37
|
+
- Integrate Google Labs Stitch SDK
|
|
38
|
+
- Export HTML from AI-generated screens
|
|
39
|
+
- Create design variations
|
|
40
|
+
|
|
41
|
+
## Step 1: Analyze the Project
|
|
42
|
+
|
|
43
|
+
### Check package.json
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
cat package.json | grep "@umituz/react-native-google-stitch-sdk"
|
|
47
|
+
npm list @umituz/react-native-google-stitch-sdk
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Detect Project Type
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
cat app.json | grep -q "expo" && echo "Expo" || echo "Bare RN"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Step 2: Install Package
|
|
57
|
+
|
|
58
|
+
### Install Core Package
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm install @umituz/react-native-google-stitch-sdk@latest
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Verify Installation
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm list @umituz/react-native-google-stitch-sdk
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Step 3: Environment Setup
|
|
71
|
+
|
|
72
|
+
### CRITICAL: Environment Variables Required
|
|
73
|
+
|
|
74
|
+
The underlying `@google/stitch-sdk` requires `process.env.STITCH_API_KEY`. You MUST set up environment variable support in your React Native app.
|
|
75
|
+
|
|
76
|
+
#### For Expo Apps
|
|
77
|
+
|
|
78
|
+
**Option 1: react-native-dotenv (Recommended)**
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm install --save-dev react-native-dotenv
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Update `babel.config.js`:
|
|
85
|
+
```javascript
|
|
86
|
+
module.exports = {
|
|
87
|
+
presets: ['module:metro-react-native-babel-preset'],
|
|
88
|
+
plugins: [
|
|
89
|
+
['module:react-native-dotenv', {
|
|
90
|
+
moduleName: '@env',
|
|
91
|
+
path: '.env',
|
|
92
|
+
}]
|
|
93
|
+
]
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Create `.env` file in project root:
|
|
98
|
+
```env
|
|
99
|
+
STITCH_API_KEY=your-actual-api-key-here
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Option 2: Expo Constants**
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx expo install expo-constants
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Use `Constants.expoConfig.extra` in app.json:
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"expo": {
|
|
112
|
+
"extra": {
|
|
113
|
+
"stitchApiKey": "your-actual-api-key-here"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### For Bare React Native
|
|
120
|
+
|
|
121
|
+
**Option 1: react-native-dotenv (Recommended)**
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
npm install --save-dev react-native-dotenv
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Update `metro.config.js`:
|
|
128
|
+
```javascript
|
|
129
|
+
const { getDefaultConfig } = require('expo/metro-config');
|
|
130
|
+
|
|
131
|
+
const config = getDefaultConfig(__dirname);
|
|
132
|
+
|
|
133
|
+
module.exports = config;
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Update `babel.config.js`:
|
|
137
|
+
```javascript
|
|
138
|
+
module.exports = {
|
|
139
|
+
presets: ['module:metro-react-native-babel-preset'],
|
|
140
|
+
plugins: [
|
|
141
|
+
['module:react-native-dotenv', {
|
|
142
|
+
moduleName: '@env',
|
|
143
|
+
path: '.env',
|
|
144
|
+
}]
|
|
145
|
+
]
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Create `.env` file:
|
|
150
|
+
```env
|
|
151
|
+
STITCH_API_KEY=your-actual-api-key-here
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Option 2: Manual Polyfill**
|
|
155
|
+
|
|
156
|
+
Create `global.polyfills.js`:
|
|
157
|
+
```javascript
|
|
158
|
+
if (typeof process === 'undefined') {
|
|
159
|
+
global.process = require('process');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Set environment variables
|
|
163
|
+
process.env.STITCH_API_KEY = 'your-actual-api-key-here';
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Import in `index.js` or `App.tsx`:
|
|
167
|
+
```javascript
|
|
168
|
+
import './global.polyfills';
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Get API Key
|
|
172
|
+
|
|
173
|
+
1. Go to https://github.com/google-labs-code/stitch-sdk
|
|
174
|
+
2. Follow authentication instructions
|
|
175
|
+
3. Get your `STITCH_API_KEY`
|
|
176
|
+
|
|
177
|
+
## Step 4: Basic Usage
|
|
178
|
+
|
|
179
|
+
### Using the Hook
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { useStitch } from '@umituz/react-native-google-stitch-sdk/core';
|
|
183
|
+
|
|
184
|
+
function MyScreen() {
|
|
185
|
+
const {
|
|
186
|
+
isLoading,
|
|
187
|
+
error,
|
|
188
|
+
generateScreen,
|
|
189
|
+
getScreenOutput,
|
|
190
|
+
editScreen,
|
|
191
|
+
generateVariants,
|
|
192
|
+
} = useStitch({
|
|
193
|
+
apiKey: process.env.STITCH_API_KEY,
|
|
194
|
+
autoInitialize: true,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const handleGenerate = async () => {
|
|
198
|
+
try {
|
|
199
|
+
const screen = await generateScreen('project-id', {
|
|
200
|
+
prompt: 'A modern login page with email and password fields',
|
|
201
|
+
deviceType: 'MOBILE',
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const { htmlUrl, imageUrl } = await getScreenOutput('project-id', screen.screenId);
|
|
205
|
+
console.log('HTML URL:', htmlUrl);
|
|
206
|
+
console.log('Screenshot URL:', imageUrl);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
console.error('Generation failed:', err);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<View>
|
|
214
|
+
<Button
|
|
215
|
+
title="Generate Screen"
|
|
216
|
+
onPress={handleGenerate}
|
|
217
|
+
disabled={isLoading}
|
|
218
|
+
/>
|
|
219
|
+
{error && <Text>Error: {error.message}</Text>}
|
|
220
|
+
</View>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Using Service Directly
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { stitchService } from '@umituz/react-native-google-stitch-sdk/core';
|
|
229
|
+
import { initializeStitchSDK } from '@umituz/react-native-google-stitch-sdk/init';
|
|
230
|
+
|
|
231
|
+
// Initialize
|
|
232
|
+
initializeStitchSDK({
|
|
233
|
+
apiKey: process.env.STITCH_API_KEY,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Generate screen
|
|
237
|
+
const screen = await stitchService.generateScreen('project-id', {
|
|
238
|
+
prompt: 'A dashboard with charts',
|
|
239
|
+
deviceType: 'DESKTOP',
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Get HTML
|
|
243
|
+
const htmlUrl = await stitchService.getScreenHtml('project-id', screen.screenId);
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Step 5: Generate Screens
|
|
247
|
+
|
|
248
|
+
### Basic Screen Generation
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
const { generateScreen } = useStitch();
|
|
252
|
+
|
|
253
|
+
const screen = await generateScreen('project-id', {
|
|
254
|
+
prompt: 'A shopping cart screen with product list',
|
|
255
|
+
deviceType: 'MOBILE',
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Device Types
|
|
260
|
+
|
|
261
|
+
Available device types:
|
|
262
|
+
- `'MOBILE'` - Mobile phone layout
|
|
263
|
+
- `'DESKTOP'` - Desktop layout
|
|
264
|
+
- `'TABLET'` - Tablet layout
|
|
265
|
+
- `'AGNOSTIC'` - Responsive layout
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
const screen = await generateScreen('project-id', {
|
|
269
|
+
prompt: 'A profile page',
|
|
270
|
+
deviceType: 'TABLET',
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Step 6: Get Screen Output
|
|
275
|
+
|
|
276
|
+
### Get HTML URL
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
const htmlUrl = await getScreenHtml('project-id', screen.screenId);
|
|
280
|
+
// Returns: https://stitch.googleapis.com/...
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Get Screenshot URL
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
const imageUrl = await getScreenImage('project-id', screen.screenId);
|
|
287
|
+
// Returns: https://stitch.googleapis.com/...
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Get Both URLs
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
const { htmlUrl, imageUrl } = await getScreenOutput('project-id', screen.screenId);
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Display in React Native
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
import { WebView } from 'react-native-webview';
|
|
300
|
+
import { Image } from 'react-native';
|
|
301
|
+
|
|
302
|
+
function GeneratedScreen({ screen }) {
|
|
303
|
+
const { htmlUrl, imageUrl } = useScreenOutput(screen);
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<>
|
|
307
|
+
{/* Show screenshot */}
|
|
308
|
+
<Image source={{ uri: imageUrl }} style={{ width: '100%', height: 300 }} />
|
|
309
|
+
|
|
310
|
+
{/* Show HTML in WebView */}
|
|
311
|
+
<WebView source={{ uri: htmlUrl }} />
|
|
312
|
+
</>
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Step 7: Edit Screens
|
|
318
|
+
|
|
319
|
+
### Edit with Text Prompt
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
const editedScreen = await editScreen('project-id', screen.screenId, {
|
|
323
|
+
prompt: 'Make the background dark and add a sidebar',
|
|
324
|
+
deviceType: 'MOBILE',
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Edit with Model Selection
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
const editedScreen = await editScreen('project-id', screen.screenId, {
|
|
332
|
+
prompt: 'Use larger fonts and brighter colors',
|
|
333
|
+
deviceType: 'MOBILE',
|
|
334
|
+
modelId: 'GEMINI_3_PRO', // or 'GEMINI_3_FLASH'
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Step 8: Generate Variants
|
|
339
|
+
|
|
340
|
+
### Basic Variants
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
const variants = await generateVariants('project-id', screen.screenId, {
|
|
344
|
+
prompt: 'Try different color schemes',
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Returns array of variant screens
|
|
348
|
+
variants.forEach(variant => {
|
|
349
|
+
console.log('Variant:', variant.screenId);
|
|
350
|
+
});
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Advanced Variants
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
const variants = await generateVariants('project-id', screen.screenId, {
|
|
357
|
+
prompt: 'Create modern variations',
|
|
358
|
+
options: {
|
|
359
|
+
variantCount: 3,
|
|
360
|
+
creativeRange: 'EXPLORE', // 'REFINE' | 'EXPLORE' | 'REIMAGINE'
|
|
361
|
+
aspects: ['COLOR_SCHEME', 'LAYOUT'],
|
|
362
|
+
},
|
|
363
|
+
deviceType: 'MOBILE',
|
|
364
|
+
modelId: 'GEMINI_3_PRO',
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### Variant Aspects
|
|
369
|
+
|
|
370
|
+
Available aspects:
|
|
371
|
+
- `'LAYOUT'` - Layout variations
|
|
372
|
+
- `'COLOR_SCHEME'` - Color scheme variations
|
|
373
|
+
- `'IMAGES'` - Image variations
|
|
374
|
+
- `'TEXT_FONT'` - Font variations
|
|
375
|
+
- `'TEXT_CONTENT'` - Content variations
|
|
376
|
+
|
|
377
|
+
## Step 9: Project Management
|
|
378
|
+
|
|
379
|
+
### List Projects
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
const { listProjects } = useStitch();
|
|
383
|
+
|
|
384
|
+
const projects = await listProjects();
|
|
385
|
+
projects.forEach(project => {
|
|
386
|
+
console.log('Project:', project.projectId);
|
|
387
|
+
});
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Create New Project
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
const { createProject } = useStitch();
|
|
394
|
+
|
|
395
|
+
const { projectId } = await createProject('My App Project');
|
|
396
|
+
console.log('New project ID:', projectId);
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### List Screens in Project
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
const { listScreens } = useStitch();
|
|
403
|
+
|
|
404
|
+
const screens = await listScreens('project-id');
|
|
405
|
+
screens.forEach(screen => {
|
|
406
|
+
console.log('Screen:', screen.screenId);
|
|
407
|
+
});
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
## Step 10: Advanced Usage
|
|
411
|
+
|
|
412
|
+
### Direct MCP Tool Access
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
const { callTool } = useStitch();
|
|
416
|
+
|
|
417
|
+
// Call any Stitch MCP tool directly
|
|
418
|
+
const result = await callTool('create_project', {
|
|
419
|
+
title: 'Agent Project',
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
const tools = await callTool('list_tools', {});
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Display Generated HTML
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
import { WebView } from 'react-native-webview';
|
|
429
|
+
|
|
430
|
+
function ScreenViewer({ htmlUrl }) {
|
|
431
|
+
return (
|
|
432
|
+
<WebView
|
|
433
|
+
source={{ uri: htmlUrl }}
|
|
434
|
+
style={{ flex: 1 }}
|
|
435
|
+
javaScriptEnabled={true}
|
|
436
|
+
domStorageEnabled={true}
|
|
437
|
+
/>
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Download and Cache Screens
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
import { FileSystem } from 'react-native-file-system';
|
|
446
|
+
|
|
447
|
+
const downloadScreen = async (htmlUrl: string) => {
|
|
448
|
+
const localPath = `${FileSystem.documentDirectory}screen.html`;
|
|
449
|
+
await FileSystem.downloadAsync(htmlUrl, localPath);
|
|
450
|
+
return localPath;
|
|
451
|
+
};
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
## Step 11: Error Handling
|
|
455
|
+
|
|
456
|
+
### Handle Stitch Errors
|
|
457
|
+
|
|
458
|
+
```typescript
|
|
459
|
+
import { StitchError } from '@google/stitch-sdk';
|
|
460
|
+
|
|
461
|
+
const handleGenerate = async () => {
|
|
462
|
+
try {
|
|
463
|
+
const screen = await generateScreen('project-id', {
|
|
464
|
+
prompt: 'A login page',
|
|
465
|
+
});
|
|
466
|
+
} catch (error) {
|
|
467
|
+
if (error instanceof StitchError) {
|
|
468
|
+
console.error('Error code:', error.code);
|
|
469
|
+
console.error('Recoverable:', error.recoverable);
|
|
470
|
+
|
|
471
|
+
switch (error.code) {
|
|
472
|
+
case 'AUTH_FAILED':
|
|
473
|
+
console.error('Check your API key');
|
|
474
|
+
break;
|
|
475
|
+
case 'NOT_FOUND':
|
|
476
|
+
console.error('Project/screen not found');
|
|
477
|
+
break;
|
|
478
|
+
case 'RATE_LIMITED':
|
|
479
|
+
console.error('Rate limit exceeded, wait a bit');
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Handle Hook Errors
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
const { isLoading, error } = useStitch();
|
|
491
|
+
|
|
492
|
+
if (error) {
|
|
493
|
+
return (
|
|
494
|
+
<View>
|
|
495
|
+
<Text>Error: {error.message}</Text>
|
|
496
|
+
<Button title="Retry" onPress={retry} />
|
|
497
|
+
</View>
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (isLoading) {
|
|
502
|
+
return <ActivityIndicator />;
|
|
503
|
+
}
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
## Step 12: Best Practices
|
|
507
|
+
|
|
508
|
+
### Prompt Engineering
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
// Good prompts
|
|
512
|
+
const goodPrompts = [
|
|
513
|
+
'A modern e-commerce product detail page with large images',
|
|
514
|
+
'A minimalist login form with social login buttons',
|
|
515
|
+
'A dashboard with 4 stat cards and a line chart',
|
|
516
|
+
];
|
|
517
|
+
|
|
518
|
+
// Bad prompts
|
|
519
|
+
const badPrompts = [
|
|
520
|
+
'a page', // Too vague
|
|
521
|
+
'make it good', // Not specific
|
|
522
|
+
];
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### Cache Generated Screens
|
|
526
|
+
|
|
527
|
+
```typescript
|
|
528
|
+
import { useState, useCallback } from 'react';
|
|
529
|
+
|
|
530
|
+
function useScreenCache() {
|
|
531
|
+
const [cache, setCache] = useState(new Map());
|
|
532
|
+
|
|
533
|
+
const getCachedScreen = useCallback((screenId: string) => {
|
|
534
|
+
return cache.get(screenId);
|
|
535
|
+
}, [cache]);
|
|
536
|
+
|
|
537
|
+
const setCachedScreen = useCallback((screenId: string, data: any) => {
|
|
538
|
+
setCache(prev => new Map(prev).set(screenId, data));
|
|
539
|
+
}, []);
|
|
540
|
+
|
|
541
|
+
return { getCachedScreen, setCachedScreen };
|
|
542
|
+
}
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
### Loading States
|
|
546
|
+
|
|
547
|
+
```typescript
|
|
548
|
+
function ScreenGenerator() {
|
|
549
|
+
const { isLoading, error, generateScreen, getScreenOutput } = useStitch();
|
|
550
|
+
const [step, setStep] = useState('idle');
|
|
551
|
+
|
|
552
|
+
const handleGenerate = async () => {
|
|
553
|
+
setStep('generating');
|
|
554
|
+
const screen = await generateScreen('project-id', { prompt: '...' });
|
|
555
|
+
|
|
556
|
+
setStep('fetching');
|
|
557
|
+
const output = await getScreenOutput('project-id', screen.screenId);
|
|
558
|
+
|
|
559
|
+
setStep('complete');
|
|
560
|
+
return output;
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
return (
|
|
564
|
+
<View>
|
|
565
|
+
{step === 'generating' && <Text>Generating screen with AI...</Text>}
|
|
566
|
+
{step === 'fetching' && <Text>Fetching HTML and screenshot...</Text>}
|
|
567
|
+
{isLoading && <ActivityIndicator />}
|
|
568
|
+
</View>
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
## Step 13: Verify Setup
|
|
574
|
+
|
|
575
|
+
### Test Import
|
|
576
|
+
|
|
577
|
+
```typescript
|
|
578
|
+
import { useStitch } from '@umituz/react-native-google-stitch-sdk/core';
|
|
579
|
+
|
|
580
|
+
// Should work without errors
|
|
581
|
+
console.log('Stitch SDK imported successfully');
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
### Test Environment Variable
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
import '@env'; // If using react-native-dotenv
|
|
588
|
+
|
|
589
|
+
console.log('API Key:', process.env.STITCH_API_KEY?.substring(0, 10) + '...');
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### Test Basic Generation
|
|
593
|
+
|
|
594
|
+
```typescript
|
|
595
|
+
const test = async () => {
|
|
596
|
+
const { listProjects } = useStitch();
|
|
597
|
+
const projects = await listProjects();
|
|
598
|
+
console.log('Projects:', projects.length);
|
|
599
|
+
};
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
## Common Mistakes
|
|
603
|
+
|
|
604
|
+
| Mistake | Fix |
|
|
605
|
+
|---------|-----|
|
|
606
|
+
| **Missing process.env** | Set up react-native-dotenv or manual polyfill |
|
|
607
|
+
| **API key not set** | Add STITCH_API_KEY to .env file |
|
|
608
|
+
| **Wrong import path** | Use `/core` subpath, not root import |
|
|
609
|
+
| **WebView not installed** | Install react-native-webview for HTML display |
|
|
610
|
+
| **Device type wrong** | Use 'MOBILE', 'DESKTOP', 'TABLET', or 'AGNOSTIC' |
|
|
611
|
+
| **Project ID missing** | Create project first or use existing project ID |
|
|
612
|
+
|
|
613
|
+
## Troubleshooting
|
|
614
|
+
|
|
615
|
+
| Issue | Solution |
|
|
616
|
+
|-------|----------|
|
|
617
|
+
| **"process is not defined"** | Install react-native-dotenv or add polyfill |
|
|
618
|
+
| **"Cannot find module '@env'"** | Check babel.config.js has dotenv plugin |
|
|
619
|
+
| **"API key not found"** | Verify .env file has STITCH_API_KEY |
|
|
620
|
+
| **"Project not found"** | Use createProject() or valid project ID |
|
|
621
|
+
| **"Generation failed"** | Check prompt is descriptive enough |
|
|
622
|
+
| **WebView blank** | Use react-native-webview for HTML display |
|
|
623
|
+
|
|
624
|
+
## Summary
|
|
625
|
+
|
|
626
|
+
After setup, provide:
|
|
627
|
+
|
|
628
|
+
1. ✅ Package version installed
|
|
629
|
+
2. ✅ Environment variables configured
|
|
630
|
+
3. ✅ API key set in .env
|
|
631
|
+
4. ✅ Basic screen generation working
|
|
632
|
+
5. ✅ HTML/screenshot URLs accessible
|
|
633
|
+
6. ✅ Editing and variants working
|
|
634
|
+
7. ✅ Verification status
|
|
635
|
+
|
|
636
|
+
## TypeScript Types
|
|
637
|
+
|
|
638
|
+
All types are exported and available:
|
|
639
|
+
|
|
640
|
+
```typescript
|
|
641
|
+
import type {
|
|
642
|
+
StitchProject,
|
|
643
|
+
StitchScreen,
|
|
644
|
+
DeviceType,
|
|
645
|
+
ModelId,
|
|
646
|
+
CreativeRange,
|
|
647
|
+
VariantAspect,
|
|
648
|
+
VariantOptions,
|
|
649
|
+
ScreenGenerateInput,
|
|
650
|
+
ScreenEditInput,
|
|
651
|
+
ScreenVariantsInput,
|
|
652
|
+
ScreenOutput,
|
|
653
|
+
} from '@umituz/react-native-google-stitch-sdk/core';
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
## Platform Support
|
|
657
|
+
|
|
658
|
+
- ✅ React Native (Bare)
|
|
659
|
+
- ✅ Expo (with react-native-webview for HTML display)
|
|
660
|
+
- ✅ iOS & Android
|
|
661
|
+
- ✅ TypeScript
|
|
662
|
+
|
|
663
|
+
## Related Packages
|
|
664
|
+
|
|
665
|
+
- `react-native-webview` - Display generated HTML
|
|
666
|
+
- `react-native-dotenv` - Environment variables
|
|
667
|
+
- `expo-constants` - Expo environment variables (alternative)
|
|
668
|
+
|
|
669
|
+
---
|
|
670
|
+
|
|
671
|
+
**Compatible with:** @umituz/react-native-google-stitch-sdk@latest
|
|
672
|
+
**Platforms:** React Native (Expo & Bare)
|
|
673
|
+
**Dependencies:** @google/stitch-sdk, process.env polyfill required
|
|
674
|
+
**Environment:** STITCH_API_KEY required
|