Skip to content

Commit 880541f

Browse files
committed
add url encoding component
1 parent 9637068 commit 880541f

File tree

5 files changed

+52
-48
lines changed

5 files changed

+52
-48
lines changed

docs/tutorials/adding-custom-one-click-deploy.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ sidebar_position: 800
44
description: Add a custom 1-Click Deploy link to deploy your own app.
55
---
66

7+
import { URLProvider, URLEncode } from "../../src/components/OneClick";
8+
9+
<URLProvider>
10+
711
# Adding Custom 1-Click Deploy to Your App
812

9-
This tutorial will show you how to add a 1-Click Deploy link to deploy your app to the Defang Playground.
13+
This tutorial will show you how to add a 1-Click Deploy link to deploy your app to the Defang Playground so that other people can easily deploy your app.
1014

1115
The link is often placed as a button in the `README.md` file of your project repository, and is the easiest way to allow anyone to deploy your app.
1216

@@ -83,12 +87,16 @@ You will need the encoded version of the URL of the page from the previous step.
8387
```
8488
https://github.com/new?template_name=<your-repo-name>&template_owner=<your-github-username>
8589
```
86-
2. You can go online and paste the URL from the step above into a URL encoder tool of your choice. You should end up with an encoded URL, similar to the one shown below:
90+
2. You need to URL encode your url for the next step. For example, the url above would be encoded as:
8791

8892
```
8993
https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3D<your-repo-name>%26template_owner%3D<your-github-username>
9094
```
9195

96+
You can just paste your url in here to get the encoded version:
97+
98+
<URLEncode />
99+
92100
## Step 5 - Create the 1-Click Deploy Link
93101

94102
You will need to create a 1-Click Deploy link with the following format: `https://portal.defang.dev/redirect?url=` + your encoded URL. This ensures that the user can get [logged in](/docs/concepts/authentication/) to Defang before they get redirected to clone your app for deployment.
@@ -113,3 +121,5 @@ Or perhaps you can add it to a button with your own styling:
113121
```
114122
[![1-click-deploy-button](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=<your-encoded-url>&name=<your-project-here>)
115123
```
124+
125+
</URLProvider>

package-lock.json

Lines changed: 0 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"docusaurus-lunr-search": "^3.2.0",
3030
"fuse.js": "^7.0.0",
3131
"gray-matter": "^4.0.3",
32-
"jotai": "^2.11.0",
3332
"prism-react-renderer": "^2.1.0",
3433
"react": "^18.2.0",
3534
"react-dom": "^18.2.0",
Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
import { Stack, TextField } from '@mui/material';
2-
import { useState } from 'react';
3-
import {atom, useAtom} from 'jotai';
2+
import { createContext, useContext, useState } from 'react';
3+
import CodeBlock from '@theme/CodeBlock';
44

5-
const urlAtom = atom("");
65

7-
export function OriginalRepoUrl(){
6+
7+
const URLContext = createContext({
8+
url: "",
9+
setUrl: (url: string) => { }
10+
});
11+
12+
export function URLProvider({ children }: { children: React.ReactNode }) {
813
const [url, setUrl] = useState("");
14+
return (
15+
<URLContext.Provider value={{ url, setUrl }}>
16+
{children}
17+
</URLContext.Provider>
18+
);
19+
}
20+
21+
22+
23+
export function OriginalRepoUrl() {
24+
const { url, setUrl } = useContext(URLContext);
925
return (
1026
<Stack spacing={2}>
1127
<TextField value={url} onChange={e => setUrl(e.target.value)} label="Your Repo URL" helperText="Like https://github.com/defanglabs/defang" />
1228
</Stack>
1329
);
1430
}
1531

16-
function getTemplateUrl(url: string){
32+
function getTemplateUrl(url: string) {
1733
// extract github user and repo from url
1834
const regex = /https:\/\/github.com\/([^\/]+)\/([^\/]+)/;
1935
const match = url.match(regex);
@@ -24,8 +40,8 @@ function getTemplateUrl(url: string){
2440
return `https://github.com/new?template_name=${repo}&template_owner=${user}&name=${repo}`;
2541
}
2642

27-
export function TemplateUrl(){
28-
const [url] = useAtom(urlAtom);
43+
export function TemplateUrl() {
44+
const { url } = useContext(URLContext);
2945

3046
const templateUrl = getTemplateUrl(url);
3147

@@ -36,14 +52,14 @@ export function TemplateUrl(){
3652
);
3753
}
3854

39-
function getEncodedTemplateUrl(url: string){
55+
function getEncodedTemplateUrl(url: string) {
4056
const templateUrl = getTemplateUrl(url);
4157
return encodeURIComponent(templateUrl);
4258
}
4359

4460

45-
export function EncodedTemplateUrl(){
46-
const [url] = useAtom(urlAtom);
61+
export function EncodedTemplateUrl() {
62+
const { url } = useContext(URLContext);
4763
const encodedTemplateUrl = getEncodedTemplateUrl(url);
4864
return (
4965
<Stack spacing={2}>
@@ -52,14 +68,14 @@ export function EncodedTemplateUrl(){
5268
);
5369
}
5470

55-
function getOneClickUrl(url: string){
71+
function getOneClickUrl(url: string) {
5672
const encodedTemplateUrl = getEncodedTemplateUrl(url);
5773
return `https://portal.defang.dev/redirect?url=${encodedTemplateUrl}`;
5874
}
5975

6076

61-
export function OneClickUrl(){
62-
const [url] = useAtom(urlAtom);
77+
export function OneClickUrl() {
78+
const { url } = useContext(URLContext);
6379
const oneClickUrl = getOneClickUrl(url);
6480
return (
6581
<Stack spacing={2}>
@@ -68,3 +84,14 @@ export function OneClickUrl(){
6884
);
6985
}
7086

87+
export function URLEncode() {
88+
const [val, setVal] = useState("");
89+
return (
90+
<Stack spacing={2}>
91+
<TextField value={val} placeholder='Paste a url here' onChange={e => setVal(e.target.value)} />
92+
<CodeBlock className="language-bash">
93+
{encodeURIComponent(val)}
94+
</CodeBlock>
95+
</Stack>
96+
)
97+
}

src/components/URLEncoder/styles.module.css

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)