|
| 1 | +import { Stack, TextField } from '@mui/material'; |
| 2 | +import { createContext, useContext, useState } from 'react'; |
| 3 | +import CodeBlock from '@theme/CodeBlock'; |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +const URLContext = createContext({ |
| 8 | + url: "", |
| 9 | + setUrl: (url: string) => { } |
| 10 | +}); |
| 11 | + |
| 12 | +export function URLProvider({ children }: { children: React.ReactNode }) { |
| 13 | + 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); |
| 25 | + return ( |
| 26 | + <Stack spacing={2}> |
| 27 | + <TextField value={url} onChange={e => setUrl(e.target.value)} label="Your Repo URL" helperText="Like https://github.com/defanglabs/defang" /> |
| 28 | + </Stack> |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +function getTemplateUrl(url: string) { |
| 33 | + // extract github user and repo from url |
| 34 | + const regex = /https:\/\/github.com\/([^\/]+)\/([^\/]+)/; |
| 35 | + const match = url.match(regex); |
| 36 | + const user = match ? match[1] : ""; |
| 37 | + const repo = match ? match[2] : ""; |
| 38 | + |
| 39 | + // https://github.com/new?template_name=sample-django-template&template_owner=DefangSamples |
| 40 | + return `https://github.com/new?template_name=${repo}&template_owner=${user}&name=${repo}`; |
| 41 | +} |
| 42 | + |
| 43 | +export function TemplateUrl() { |
| 44 | + const { url } = useContext(URLContext); |
| 45 | + |
| 46 | + const templateUrl = getTemplateUrl(url); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Stack spacing={2}> |
| 50 | + <TextField value={templateUrl} label="Your Template URL" /> |
| 51 | + </Stack> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function getEncodedTemplateUrl(url: string) { |
| 56 | + const templateUrl = getTemplateUrl(url); |
| 57 | + return encodeURIComponent(templateUrl); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +export function EncodedTemplateUrl() { |
| 62 | + const { url } = useContext(URLContext); |
| 63 | + const encodedTemplateUrl = getEncodedTemplateUrl(url); |
| 64 | + return ( |
| 65 | + <Stack spacing={2}> |
| 66 | + <TextField value={encodedTemplateUrl} label="The Encoded Template URL" /> |
| 67 | + </Stack> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function getOneClickUrl(url: string) { |
| 72 | + const encodedTemplateUrl = getEncodedTemplateUrl(url); |
| 73 | + return `https://portal.defang.dev/redirect?url=${encodedTemplateUrl}`; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +export function OneClickUrl() { |
| 78 | + const { url } = useContext(URLContext); |
| 79 | + const oneClickUrl = getOneClickUrl(url); |
| 80 | + return ( |
| 81 | + <Stack spacing={2}> |
| 82 | + <TextField value={oneClickUrl} label="Your 1-Click URL" /> |
| 83 | + </Stack> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 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 | +} |
0 commit comments