From ab851ff5fa2d47172c08dab66a02a43fe00f9852 Mon Sep 17 00:00:00 2001 From: Wes Cole Date: Fri, 19 Dec 2025 14:48:23 -0500 Subject: [PATCH 1/2] feat(react): add Target component with dynamic iframe height - Add Target component that renders UI extensions from third-party apps - Fetch enabled extensions via GraphQL from app-registry-subgraph - Support dynamic height via postMessage resize events from extensions - Add minHeight prop with 200px default - Extract normalizeApiHost to shared utils.ts to prevent duplication - Add React Native version with WebView support Amp-Thread-ID: https://ampcode.com/threads/T-019b3817-486f-75ec-9ec2-44b031f528a0 Co-authored-by: Amp --- packages/react-native/package.json | 41 ++++ .../src/components/target/index.ts | 6 + .../src/components/target/target.tsx | 224 +++++++++++++++++ .../react-native/src/godaddy-provider.tsx | 31 +++ packages/react-native/src/index.ts | 11 + packages/react-native/tsconfig.json | 23 ++ packages/react-native/tsdown.config.ts | 8 + packages/react/src/components/target/index.ts | 1 + .../react/src/components/target/target.tsx | 227 ++++++++++++++++++ packages/react/src/lib/utils.ts | 7 + packages/react/src/server.ts | 64 ++++- 11 files changed, 641 insertions(+), 2 deletions(-) create mode 100644 packages/react-native/package.json create mode 100644 packages/react-native/src/components/target/index.ts create mode 100644 packages/react-native/src/components/target/target.tsx create mode 100644 packages/react-native/src/godaddy-provider.tsx create mode 100644 packages/react-native/src/index.ts create mode 100644 packages/react-native/tsconfig.json create mode 100644 packages/react-native/tsdown.config.ts create mode 100644 packages/react/src/components/target/index.ts create mode 100644 packages/react/src/components/target/target.tsx diff --git a/packages/react-native/package.json b/packages/react-native/package.json new file mode 100644 index 000000000..db6c77dff --- /dev/null +++ b/packages/react-native/package.json @@ -0,0 +1,41 @@ +{ + "name": "@godaddy/react-native", + "private": false, + "version": "0.0.1", + "type": "module", + "types": "./dist/index.d.ts", + "files": [ + "dist" + ], + "exports": { + "./package.json": "./package.json", + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "default": "./dist/index.js" + } + }, + "scripts": { + "dev": "tsdown --watch", + "build": "tsdown", + "typecheck": "tsc --noEmit", + "test": "echo ok", + "clean": "rimraf node_modules dist" + }, + "peerDependencies": { + "react": "18.x || 19.x", + "react-native": ">=0.70", + "react-native-webview": ">=13" + }, + "devDependencies": { + "@types/react": "^19.0.8", + "react": "^19", + "react-native": "^0.76.0", + "react-native-webview": "^13.12.5", + "tsdown": "^0.15.6", + "typescript": "~5.7.3" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/" + } +} diff --git a/packages/react-native/src/components/target/index.ts b/packages/react-native/src/components/target/index.ts new file mode 100644 index 000000000..fcb69d1c3 --- /dev/null +++ b/packages/react-native/src/components/target/index.ts @@ -0,0 +1,6 @@ +export { + Target, + type TargetProps, + type EntityType, + type EnabledExtension, +} from './target'; diff --git a/packages/react-native/src/components/target/target.tsx b/packages/react-native/src/components/target/target.tsx new file mode 100644 index 000000000..77970da1a --- /dev/null +++ b/packages/react-native/src/components/target/target.tsx @@ -0,0 +1,224 @@ +import React from 'react'; +import { + View, + Text, + ActivityIndicator, + StyleSheet, + type ViewStyle, +} from 'react-native'; +import { WebView } from 'react-native-webview'; +import { useGoDaddyContext } from '@/godaddy-provider'; + +export type EntityType = 'STORE' | 'CUSTOMER'; + +export interface EnabledExtension { + applicationId: string; + releaseId: string; + target: string; +} + +export interface TargetProps { + id: string; + entityId: string; + entityType: EntityType; + style?: ViewStyle; + webViewHeight?: number; +} + +interface EnabledExtensionsResponse { + data: { + enabledExtensions: EnabledExtension[]; + }; +} + +function normalizeApiHost(host: string): string { + return host.startsWith('http') ? host : `https://${host}`; +} + +async function fetchEnabledExtensions( + target: string, + entityId: string, + entityType: EntityType, + apiHost: string, + accessToken?: string +): Promise { + const baseUrl = normalizeApiHost(apiHost); + const endpoint = `${baseUrl}/v1/apps/app-registry-subgraph`; + + const query = ` + query EnabledExtensions($entityId: String!, $entityType: EntityType!, $target: String!) { + enabledExtensions(entityId: $entityId, entityType: $entityType, target: $target) { + applicationId + releaseId + target + } + } + `; + + const headers: Record = { + 'Content-Type': 'application/json', + }; + + if (accessToken) { + headers['Authorization'] = `Bearer ${accessToken}`; + } + + const response = await fetch(endpoint, { + method: 'POST', + headers, + body: JSON.stringify({ + query, + variables: { + entityId, + entityType, + target, + }, + }), + }); + + if (!response.ok) { + throw new Error(`Failed to fetch extensions: ${response.statusText}`); + } + + const result: EnabledExtensionsResponse = await response.json(); + return result.data.enabledExtensions ?? []; +} + +function buildExtensionUrl( + applicationId: string, + releaseId: string, + apiHost: string +): string { + return `https://cdn.ui-extensions.commerce.${apiHost}/${applicationId}/${releaseId}/index.html`; +} + +function TargetError() { + return ( + + Failed to load extension + + ); +} + +function TargetLoading() { + return ( + + + + ); +} + +interface ExtensionWebViewProps { + extension: EnabledExtension; + apiHost: string; + height: number; +} + +function ExtensionWebView({ extension, apiHost, height }: ExtensionWebViewProps) { + const src = buildExtensionUrl( + extension.applicationId, + extension.releaseId, + apiHost + ); + + return ( + } + /> + ); +} + +type FetchState = + | { status: 'loading' } + | { status: 'error'; error: Error } + | { status: 'success'; data: EnabledExtension[] }; + +export function Target({ + id, + entityId, + entityType, + style, + webViewHeight = 200, +}: TargetProps) { + const { apiHost, accessToken } = useGoDaddyContext(); + const [state, setState] = React.useState({ status: 'loading' }); + + const resolvedApiHost = apiHost || 'api.godaddy.com'; + + React.useEffect(() => { + let cancelled = false; + + setState({ status: 'loading' }); + + fetchEnabledExtensions(id, entityId, entityType, resolvedApiHost, accessToken) + .then(data => { + if (!cancelled) { + setState({ status: 'success', data }); + } + }) + .catch(error => { + if (!cancelled) { + setState({ status: 'error', error }); + } + }); + + return () => { + cancelled = true; + }; + }, [id, entityId, entityType, resolvedApiHost, accessToken]); + + if (state.status === 'loading') { + return ; + } + + if (state.status === 'error') { + return ; + } + + if (state.data.length === 0) { + return null; + } + + return ( + + {state.data.map(extension => ( + + ))} + + ); +} + +const styles = StyleSheet.create({ + container: { + gap: 8, + }, + loadingContainer: { + height: 64, + justifyContent: 'center', + alignItems: 'center', + }, + errorContainer: { + padding: 16, + backgroundColor: '#FEE2E2', + borderRadius: 8, + }, + errorText: { + color: '#DC2626', + fontSize: 14, + textAlign: 'center', + }, + webView: { + width: '100%', + }, +}); diff --git a/packages/react-native/src/godaddy-provider.tsx b/packages/react-native/src/godaddy-provider.tsx new file mode 100644 index 000000000..e6a6058ec --- /dev/null +++ b/packages/react-native/src/godaddy-provider.tsx @@ -0,0 +1,31 @@ +import React from 'react'; + +interface GoDaddyContextValue { + apiHost?: string; + accessToken?: string; +} + +const GoDaddyContext = React.createContext({}); + +export const useGoDaddyContext = () => React.useContext(GoDaddyContext); + +export interface GoDaddyProviderProps { + apiHost?: string; + accessToken?: string; + children: React.ReactNode; +} + +export function GoDaddyProvider({ + apiHost, + accessToken, + children, +}: GoDaddyProviderProps) { + const value = React.useMemo( + () => ({ apiHost, accessToken }), + [apiHost, accessToken] + ); + + return ( + {children} + ); +} diff --git a/packages/react-native/src/index.ts b/packages/react-native/src/index.ts new file mode 100644 index 000000000..d4e002c93 --- /dev/null +++ b/packages/react-native/src/index.ts @@ -0,0 +1,11 @@ +export { + GoDaddyProvider, + useGoDaddyContext, + type GoDaddyProviderProps, +} from './godaddy-provider'; +export { + Target, + type TargetProps, + type EntityType, + type EnabledExtension, +} from './components/target'; diff --git a/packages/react-native/tsconfig.json b/packages/react-native/tsconfig.json new file mode 100644 index 000000000..f0685eb32 --- /dev/null +++ b/packages/react-native/tsconfig.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "compilerOptions": { + "isolatedModules": true, + "resolveJsonModule": true, + "moduleResolution": "bundler", + "module": "ESNext", + "target": "ES2022", + "lib": ["ES2022"], + "baseUrl": ".", + "jsx": "react-jsx", + "jsxImportSource": "react", + "allowImportingTsExtensions": true, + "noEmit": true, + "skipLibCheck": true, + "strict": true, + "paths": { + "@/*": ["./src/*"] + } + }, + "include": ["src"], + "exclude": ["node_modules", "dist"] +} diff --git a/packages/react-native/tsdown.config.ts b/packages/react-native/tsdown.config.ts new file mode 100644 index 000000000..aec00f446 --- /dev/null +++ b/packages/react-native/tsdown.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'tsdown'; + +export default defineConfig({ + entry: ['src/index.ts'], + tsconfig: './tsconfig.json', + dts: true, + external: [/^react($|\/)/, /^react-native($|\/)/, /^react-native-webview($|\/)/], +}); diff --git a/packages/react/src/components/target/index.ts b/packages/react/src/components/target/index.ts new file mode 100644 index 000000000..d80b54810 --- /dev/null +++ b/packages/react/src/components/target/index.ts @@ -0,0 +1 @@ +export { Target, type TargetProps, type EntityType, type EnabledExtension } from './target'; diff --git a/packages/react/src/components/target/target.tsx b/packages/react/src/components/target/target.tsx new file mode 100644 index 000000000..5f1f7c766 --- /dev/null +++ b/packages/react/src/components/target/target.tsx @@ -0,0 +1,227 @@ +'use client'; + +import { useQuery } from '@tanstack/react-query'; +import React from 'react'; +import { Skeleton } from '@/components/ui/skeleton'; +import { useGoDaddyContext } from '@/godaddy-provider'; +import { normalizeApiHost, DEFAULT_API_HOST } from '@/lib/utils'; + +export type EntityType = 'STORE' | 'CUSTOMER'; + +export interface EnabledExtension { + applicationId: string; + releaseId: string; + target: string; +} + +export interface TargetProps { + id: string; + entityId: string; + entityType: EntityType; + minHeight?: number; +} + +interface ExtensionResizeMessage { + type: 'extension:resize'; + applicationId: string; + releaseId: string; + height: number; +} + +interface EnabledExtensionsResponse { + data: { + enabledExtensions: EnabledExtension[]; + }; +} + +async function fetchEnabledExtensions( + target: string, + entityId: string, + entityType: EntityType, + apiHost: string, + accessToken?: string +): Promise { + const baseUrl = normalizeApiHost(apiHost); + const endpoint = `${baseUrl}/v1/apps/app-registry-subgraph`; + + const query = ` + query EnabledExtensions($entityId: String!, $entityType: EntityType!, $target: String!) { + enabledExtensions(entityId: $entityId, entityType: $entityType, target: $target) { + applicationId + releaseId + target + } + } + `; + + const headers: Record = { + 'Content-Type': 'application/json', + }; + + if (accessToken) { + headers['Authorization'] = `Bearer ${accessToken}`; + } + + const response = await fetch(endpoint, { + method: 'POST', + headers, + body: JSON.stringify({ + query, + variables: { + entityId, + entityType, + target, + }, + }), + }); + + if (!response.ok) { + throw new Error(`Failed to fetch extensions: ${response.statusText}`); + } + + const result: EnabledExtensionsResponse = await response.json(); + return result.data.enabledExtensions ?? []; +} + +function buildExtensionUrl( + applicationId: string, + releaseId: string, + apiHost: string +): string { + return `https://cdn.ui-extensions.commerce.${apiHost}/${applicationId}/${releaseId}/index.html`; +} + +function TargetError() { + return ( +
+ Failed to load extension +
+ ); +} + +function TargetLoading() { + return ; +} + +interface ExtensionIframeProps { + extension: EnabledExtension; + apiHost: string; + height: number | undefined; + minHeight: number; +} + +function ExtensionIframe({ + extension, + apiHost, + height, + minHeight, +}: ExtensionIframeProps) { + const src = buildExtensionUrl( + extension.applicationId, + extension.releaseId, + apiHost + ); + + return ( +