|
| 1 | +/** |
| 2 | + * Product Utility Functions |
| 3 | + * Provides data transformation utilities for product pages |
| 4 | + */ |
| 5 | + |
| 6 | +import type { |
| 7 | + ComponentCommunityUrls, |
| 8 | + ComponentResourceUrls, |
| 9 | + ManifestCLI, |
| 10 | + ManifestCommunityUrls, |
| 11 | + ManifestExtension, |
| 12 | + ManifestIDE, |
| 13 | + ManifestResourceUrls, |
| 14 | +} from '@/types/manifests' |
| 15 | + |
| 16 | +/** |
| 17 | + * Platform information type |
| 18 | + */ |
| 19 | +export type PlatformInfo = |
| 20 | + | { |
| 21 | + type: 'platforms' |
| 22 | + values: string[] |
| 23 | + } |
| 24 | + | { |
| 25 | + type: 'supportedIdes' |
| 26 | + values: string[] |
| 27 | + } |
| 28 | + | null |
| 29 | + |
| 30 | +/** |
| 31 | + * Get normalized platform information from a product |
| 32 | + * Handles both platforms (for IDE/CLI) and supportedIdes (for Extension) |
| 33 | + */ |
| 34 | +export function getPlatformInfo( |
| 35 | + product: ManifestIDE | ManifestCLI | ManifestExtension |
| 36 | +): PlatformInfo { |
| 37 | + // Check for platforms field (IDE/CLI) |
| 38 | + if ('platforms' in product && product.platforms && product.platforms.length > 0) { |
| 39 | + return { |
| 40 | + type: 'platforms', |
| 41 | + values: product.platforms.map(p => p.os), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Check for supportedIdes field (Extension) |
| 46 | + if ('supportedIdes' in product && product.supportedIdes && product.supportedIdes.length > 0) { |
| 47 | + return { |
| 48 | + type: 'supportedIdes', |
| 49 | + values: product.supportedIdes.map(ide => ide.ideId), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return null |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Transform resource URLs from manifest format to component format |
| 58 | + * Converts null to undefined for optional component props |
| 59 | + */ |
| 60 | +export function transformResourceUrls( |
| 61 | + resourceUrls?: ManifestResourceUrls | null |
| 62 | +): ComponentResourceUrls { |
| 63 | + if (!resourceUrls) { |
| 64 | + return {} |
| 65 | + } |
| 66 | + |
| 67 | + return { |
| 68 | + download: resourceUrls.download || undefined, |
| 69 | + changelog: resourceUrls.changelog || undefined, |
| 70 | + pricing: resourceUrls.pricing || undefined, |
| 71 | + mcp: resourceUrls.mcp || undefined, |
| 72 | + issue: resourceUrls.issue || undefined, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Transform community URLs from manifest format to component format |
| 78 | + * Converts null to undefined for optional component props |
| 79 | + */ |
| 80 | +export function transformCommunityUrls( |
| 81 | + communityUrls?: ManifestCommunityUrls | null |
| 82 | +): ComponentCommunityUrls { |
| 83 | + if (!communityUrls) { |
| 84 | + return {} |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + linkedin: communityUrls.linkedin || undefined, |
| 89 | + twitter: communityUrls.twitter || undefined, |
| 90 | + github: communityUrls.github || undefined, |
| 91 | + youtube: communityUrls.youtube || undefined, |
| 92 | + discord: communityUrls.discord || undefined, |
| 93 | + reddit: communityUrls.reddit || undefined, |
| 94 | + } |
| 95 | +} |
0 commit comments