Skip to content

Commit a19e7b8

Browse files
author
Vandita Patidar
committed
Web view
1 parent eec9dcf commit a19e7b8

File tree

5 files changed

+145
-4
lines changed

5 files changed

+145
-4
lines changed

packages/core/src/awsService/appBuilder/serverlessLand/metadata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"s3-lambda-resizing-sam": {
44
"name": "Resizing image",
55
"description": "Lambda, S3 • Python, Javascript, Java, .NET • SAM",
6+
"gitUrl": "https://github.com/aws-samples/serverless-patterns/tree/main/s3-lambda-resizing-python",
67
"implementation": [
78
{
89
"iac": "sam",

packages/core/src/awsService/appBuilder/serverlessLand/metadataManager.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ interface PatternData {
1616
implementation: Implementation[]
1717
}
1818

19+
interface PatternUrls {
20+
githubUrl: string
21+
previewUrl: string
22+
}
1923
export interface ProjectMetadata {
2024
patterns: Record<string, PatternData>
2125
}
@@ -100,6 +104,24 @@ export class MetadataManager {
100104
}))
101105
}
102106

107+
public getUrl(pattern: string): PatternUrls {
108+
const patternData = this.metadata?.patterns?.[pattern]
109+
if (!patternData || !patternData.implementation) {
110+
return {
111+
githubUrl: '',
112+
previewUrl: '',
113+
}
114+
}
115+
const asset = patternData.implementation[0].assetName
116+
117+
return {
118+
// GitHub URL for the pattern
119+
githubUrl: `https://github.com/aws-samples/serverless-patterns/tree/main/${asset}`,
120+
// Serverless Land preview URL
121+
previewUrl: `https://serverlessland.com/patterns/${asset}`,
122+
}
123+
}
124+
103125
/**
104126
* Gets available Infrastructure as Code options for a specific pattern
105127
* @param pattern The pattern name to get IaC options for
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
export class WebviewService {
6+
constructor() {}
7+
8+
public static getWebviewContent(url: string) {
9+
return `
10+
<!DOCTYPE html>
11+
<html lang="en">
12+
<head>
13+
<meta charset="UTF-8">
14+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
15+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; frame-src ${url}; style-src 'unsafe-inline';">
16+
<style>
17+
body, html {
18+
margin: 0;
19+
padding: 0;
20+
width: 100%;
21+
height: 100vh;
22+
overflow: hidden;
23+
}
24+
iframe {
25+
width: 100%;
26+
height: 100%;
27+
border: none;
28+
}
29+
</style>
30+
</head>
31+
<body>
32+
<iframe src="${url}" frameborder="0" allowfullscreen></iframe>
33+
</body>
34+
</html>
35+
`
36+
}
37+
38+
public static getGitWebviewContent(url: string): string {
39+
const htmlContent = `
40+
<html>
41+
<head>
42+
<meta http-equiv="refresh" content="0; url=${url}">
43+
<style>
44+
body {
45+
display: flex;
46+
justify-content: center;
47+
align-items: center;
48+
height: 100vh;
49+
margin: 0;
50+
font-family: var(--vscode-font-family);
51+
}
52+
p {
53+
text-align: center;
54+
padding: 20px;
55+
}
56+
a {
57+
color: var(--vscode-textLink-foreground);
58+
text-decoration: none;
59+
}
60+
a:hover {
61+
text-decoration: underline;
62+
color: var(--vscode-textLink-activeForeground);
63+
}
64+
</style>
65+
</head>
66+
<body>
67+
<p>To preview GitHub page, <a href="${url}">click here</a>.</p>
68+
</body>
69+
</html>
70+
`
71+
return htmlContent
72+
}
73+
}

packages/core/src/awsService/appBuilder/serverlessLand/wizard.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ export interface CreateServerlessLandWizardForm {
2828

2929
async function loadMetadata(ctx: vscode.ExtensionContext): Promise<MetadataManager> {
3030
const metadataManager = MetadataManager.getInstance()
31-
// const projectRoot = path.resolve(__dirname, '../../../../../')
32-
// const metadataPath = path.join(projectRoot, 'src', 'awsService', 'appBuilder', 'serverlessLand', 'metadata.json')
3331
const metadataPath = ctx.asAbsolutePath(path.join('dist', 'src', 'serverlessLand', 'metadata.json'))
3432
await metadataManager.loadMetadata(metadataPath)
3533
return metadataManager
@@ -41,7 +39,7 @@ function promptPattern(metadataManager: MetadataManager) {
4139
throw new ToolkitError('No patterns found in metadata')
4240
}
4341

44-
return createQuickPick<string>(
42+
const quickPick = createQuickPick<string>(
4543
patterns.map((p) => ({
4644
label: p.label,
4745
detail: p.description,
@@ -65,6 +63,8 @@ function promptPattern(metadataManager: MetadataManager) {
6563
matchOnDetail: true,
6664
}
6765
)
66+
67+
return quickPick
6868
}
6969

7070
function promptRuntime(metadataManager: MetadataManager, pattern: string | undefined) {
@@ -153,7 +153,11 @@ export class CreateServerlessLandWizard extends Wizard<CreateServerlessLandWizar
153153
throw new ToolkitError(`Failed to load metadata: ${err}`)
154154
})
155155
this.metadataManager = MetadataManager.getInstance()
156-
this.form.pattern.bindPrompter(() => promptPattern(this.metadataManager))
156+
this.form.pattern.bindPrompter(() => {
157+
const quickPick = promptPattern(this.metadataManager)
158+
159+
return quickPick
160+
})
157161
this.form.runtime.bindPrompter((state) => promptRuntime(this.metadataManager, state.pattern))
158162
this.form.iac.bindPrompter((state) => promptIac(this.metadataManager, state.pattern))
159163
this.form.location.bindPrompter(() => promptLocation())

packages/core/src/shared/ui/pickerPrompter.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { Prompter, PromptResult, Transform } from './prompter'
1212
import { assign, isAsyncIterable } from '../utilities/collectionUtils'
1313
import { recentlyUsed } from '../localizedText'
1414
import { getLogger } from '../logger/logger'
15+
import { MetadataManager } from '../../awsService/appBuilder/serverlessLand/metadataManager'
16+
import { WebviewService } from '../../awsService/appBuilder/serverlessLand/webViewManager'
1517

1618
const localize = nls.loadMessageBundle()
1719

@@ -143,6 +145,45 @@ export function createQuickPick<T>(
143145
assign(mergedOptions, picker)
144146
picker.buttons = mergedOptions.buttons ?? []
145147

148+
picker.onDidTriggerItemButton(async (event) => {
149+
const metadataManager = MetadataManager.getInstance()
150+
if (event.button.tooltip === 'Open in GitHub' || event.button.tooltip === 'Open in Serverless Land') {
151+
const selectedPattern = event.item
152+
if (selectedPattern) {
153+
const patternUrl = metadataManager.getUrl(selectedPattern.label)
154+
if (patternUrl) {
155+
if (event.button.tooltip === 'Open in GitHub') {
156+
const panel = vscode.window.createWebviewPanel(
157+
'githubPreview',
158+
`GitHub Repository ${selectedPattern.label}`,
159+
vscode.ViewColumn.One,
160+
{
161+
enableScripts: true,
162+
retainContextWhenHidden: true,
163+
enableCommandUris: true,
164+
enableFindWidget: true,
165+
}
166+
)
167+
panel.webview.html = WebviewService.getGitWebviewContent(patternUrl.githubUrl)
168+
} else if (event.button.tooltip === 'Open in Serverless Land') {
169+
const panel = vscode.window.createWebviewPanel(
170+
'serverlessLandPreview',
171+
'Serverless Land Preview',
172+
vscode.ViewColumn.One,
173+
{
174+
enableScripts: true,
175+
retainContextWhenHidden: true,
176+
enableCommandUris: true,
177+
enableFindWidget: true,
178+
}
179+
)
180+
panel.webview.html = WebviewService.getWebviewContent(patternUrl.previewUrl)
181+
}
182+
}
183+
}
184+
}
185+
})
186+
146187
const prompter =
147188
mergedOptions.filterBoxInputSettings !== undefined
148189
? new FilterBoxQuickPickPrompter<T>(picker, mergedOptions)

0 commit comments

Comments
 (0)