mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
717c4cc207 |
No files matched your search
@@ -27,13 +27,14 @@ import { useFS } from '@/hooks/useFS';
|
||||
import { useFSPaths } from '@/hooks/useFSPaths';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { ShakespeareAdapter, NetlifyAdapter, VercelAdapter, NsiteAdapter, DeployAdapter } from '@/lib/deploy';
|
||||
import { ShakespeareAdapter, NetlifyAdapter, VercelAdapter, NsiteAdapter, CloudflareAdapter, DeployAdapter } from '@/lib/deploy';
|
||||
import { Link } from 'react-router-dom';
|
||||
import type { DeployProvider, ShakespeareDeployProvider, NetlifyProvider, VercelProvider } from '@/contexts/DeploySettingsContext';
|
||||
import type { DeployProvider, ShakespeareDeployProvider, NetlifyProvider, VercelProvider, CloudflareProvider } from '@/contexts/DeploySettingsContext';
|
||||
import { ShakespeareDeployForm } from '@/components/deploy/ShakespeareDeployForm';
|
||||
import { NetlifyDeployForm } from '@/components/deploy/NetlifyDeployForm';
|
||||
import { VercelDeployForm } from '@/components/deploy/VercelDeployForm';
|
||||
import { NsiteDeployForm } from '@/components/deploy/NsiteDeployForm';
|
||||
import { CloudflareDeployForm } from '@/components/deploy/CloudflareDeployForm';
|
||||
|
||||
/**
|
||||
* Helper function to get provider URL for favicon
|
||||
@@ -58,6 +59,12 @@ function getProviderUrl(provider: DeployProvider): string | null {
|
||||
}
|
||||
return 'https://vercel.com';
|
||||
|
||||
case 'cloudflare':
|
||||
if (provider.baseURL) {
|
||||
return provider.baseURL;
|
||||
}
|
||||
return 'https://cloudflare.com';
|
||||
|
||||
case 'nsite':
|
||||
return null;
|
||||
|
||||
@@ -110,6 +117,11 @@ interface VercelFormData {
|
||||
teamId: string;
|
||||
}
|
||||
|
||||
interface CloudflareFormData {
|
||||
projectName: string;
|
||||
branch: string;
|
||||
}
|
||||
|
||||
export function DeployDialog({ projectId, projectName, open, onOpenChange }: DeployDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const { settings } = useDeploySettings();
|
||||
@@ -141,6 +153,10 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
projectName: projectName || projectId,
|
||||
teamId: '',
|
||||
});
|
||||
const [cloudflareForm, setCloudflareForm] = useState<CloudflareFormData>({
|
||||
projectName: projectName || projectId,
|
||||
branch: 'main',
|
||||
});
|
||||
|
||||
// Load project settings when dialog opens
|
||||
useEffect(() => {
|
||||
@@ -230,6 +246,24 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
projectName: vercelForm.projectName || undefined,
|
||||
corsProxy: vercelProvider.proxy ? config.corsProxy : undefined,
|
||||
});
|
||||
} else if (selectedProvider.type === 'cloudflare') {
|
||||
const cloudflareProvider = selectedProvider as CloudflareProvider;
|
||||
if (!cloudflareProvider.apiToken) {
|
||||
throw new Error('Cloudflare API token is required');
|
||||
}
|
||||
if (!cloudflareProvider.accountId) {
|
||||
throw new Error('Cloudflare Account ID is required');
|
||||
}
|
||||
|
||||
adapter = new CloudflareAdapter({
|
||||
fs,
|
||||
apiToken: cloudflareProvider.apiToken,
|
||||
accountId: cloudflareProvider.accountId,
|
||||
baseURL: cloudflareProvider.baseURL,
|
||||
projectName: cloudflareForm.projectName || undefined,
|
||||
branch: cloudflareForm.branch || undefined,
|
||||
corsProxy: cloudflareProvider.proxy ? config.corsProxy : undefined,
|
||||
});
|
||||
} else {
|
||||
throw new Error('Unknown provider type');
|
||||
}
|
||||
@@ -269,6 +303,15 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
projectId: vercelForm.projectName || undefined,
|
||||
},
|
||||
});
|
||||
} else if (selectedProvider.type === 'cloudflare') {
|
||||
await updateProjectSettings(selectedProviderId, {
|
||||
type: 'cloudflare',
|
||||
data: {
|
||||
projectName: cloudflareForm.projectName || undefined,
|
||||
branch: cloudflareForm.branch || undefined,
|
||||
projectId: result.metadata?.projectId as string | undefined,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
setDeployResult(result);
|
||||
@@ -288,6 +331,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
setNsiteForm({ nsec: '' });
|
||||
setNetlifyForm({ siteId: '', siteName: '' });
|
||||
setVercelForm({ projectName: projectName || projectId, teamId: '' });
|
||||
setCloudflareForm({ projectName: projectName || projectId, branch: 'main' });
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
@@ -311,6 +355,10 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
setVercelForm({ projectName, teamId });
|
||||
}, []);
|
||||
|
||||
const handleCloudflareConfigChange = useCallback((projectName: string, branch: string) => {
|
||||
setCloudflareForm({ projectName, branch });
|
||||
}, []);
|
||||
|
||||
const renderProviderFields = () => {
|
||||
if (!selectedProvider) return null;
|
||||
|
||||
@@ -373,6 +421,22 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep
|
||||
);
|
||||
}
|
||||
|
||||
if (selectedProvider.type === 'cloudflare') {
|
||||
const savedConfig = projectSettings.providers[selectedProviderId];
|
||||
const savedProjectName = savedConfig?.type === 'cloudflare' ? savedConfig.data.projectName : undefined;
|
||||
const savedBranch = savedConfig?.type === 'cloudflare' ? savedConfig.data.branch : undefined;
|
||||
|
||||
return (
|
||||
<CloudflareDeployForm
|
||||
projectId={projectId}
|
||||
projectName={projectName}
|
||||
savedProjectName={savedProjectName}
|
||||
savedBranch={savedBranch}
|
||||
onConfigChange={handleCloudflareConfigChange}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Input } from '@/components/ui/input';
|
||||
|
||||
interface CloudflareDeployFormProps {
|
||||
projectId: string;
|
||||
projectName?: string;
|
||||
savedProjectName?: string;
|
||||
savedBranch?: string;
|
||||
onConfigChange: (projectName: string, branch: string) => void;
|
||||
}
|
||||
|
||||
export function CloudflareDeployForm({
|
||||
projectId,
|
||||
projectName: initialProjectName,
|
||||
savedProjectName,
|
||||
savedBranch,
|
||||
onConfigChange,
|
||||
}: CloudflareDeployFormProps) {
|
||||
const [projectName, setProjectName] = useState(savedProjectName || initialProjectName || projectId);
|
||||
const [branch, setBranch] = useState(savedBranch || 'main');
|
||||
|
||||
useEffect(() => {
|
||||
onConfigChange(projectName, branch);
|
||||
}, [projectName, branch, onConfigChange]);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="cloudflare-project-name">Project Name</Label>
|
||||
<Input
|
||||
id="cloudflare-project-name"
|
||||
value={projectName}
|
||||
onChange={(e) => setProjectName(e.target.value)}
|
||||
placeholder={initialProjectName || projectId}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
URL: <span className="font-mono">{projectName || projectId}.pages.dev</span>
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="cloudflare-branch">Branch Name</Label>
|
||||
<Input
|
||||
id="cloudflare-branch"
|
||||
value={branch}
|
||||
onChange={(e) => setBranch(e.target.value)}
|
||||
placeholder="main"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Production branch for your Cloudflare Pages project
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -32,7 +32,15 @@ export interface NsiteProvider extends BaseDeployProvider {
|
||||
blossomServers: string[];
|
||||
}
|
||||
|
||||
export type DeployProvider = ShakespeareDeployProvider | NetlifyProvider | VercelProvider | NsiteProvider;
|
||||
export interface CloudflareProvider extends BaseDeployProvider {
|
||||
type: 'cloudflare';
|
||||
apiToken: string;
|
||||
accountId: string;
|
||||
baseURL?: string;
|
||||
proxy?: boolean;
|
||||
}
|
||||
|
||||
export type DeployProvider = ShakespeareDeployProvider | NetlifyProvider | VercelProvider | NsiteProvider | CloudflareProvider;
|
||||
|
||||
export interface DeploySettings {
|
||||
providers: DeployProvider[];
|
||||
|
||||
@@ -49,7 +49,10 @@ export function useOAuth(config: OAuthConfig) {
|
||||
const { corsProxy } = appConfig;
|
||||
|
||||
// Check if OAuth is configured
|
||||
const isOAuthConfigured = !!(config.clientId && config.clientSecret);
|
||||
// For PKCE flows (like Cloudflare), we only need clientId
|
||||
const isOAuthConfigured = config.usePKCE
|
||||
? !!config.clientId
|
||||
: !!(config.clientId && config.clientSecret);
|
||||
|
||||
// Generate a cryptographically secure random string for PKCE code verifier
|
||||
const generateCodeVerifier = (): string => {
|
||||
|
||||
@@ -31,7 +31,16 @@ export interface VercelProjectConfig {
|
||||
};
|
||||
}
|
||||
|
||||
export type ProjectProviderConfig = ShakespeareProjectConfig | NsiteProjectConfig | NetlifyProjectConfig | VercelProjectConfig;
|
||||
export interface CloudflareProjectConfig {
|
||||
type: 'cloudflare';
|
||||
data: {
|
||||
projectName?: string;
|
||||
branch?: string;
|
||||
projectId?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type ProjectProviderConfig = ShakespeareProjectConfig | NsiteProjectConfig | NetlifyProjectConfig | VercelProjectConfig | CloudflareProjectConfig;
|
||||
|
||||
export interface ProjectDeploySettings {
|
||||
providers: Record<string, ProjectProviderConfig>;
|
||||
|
||||
@@ -0,0 +1,321 @@
|
||||
import type { JSRuntimeFS } from '../JSRuntime';
|
||||
import type { DeployAdapter, DeployOptions, DeployResult, CloudflareDeployConfig } from './types';
|
||||
import { proxyUrl } from '../proxyUrl';
|
||||
|
||||
interface CloudflareFile {
|
||||
key: string;
|
||||
value: string;
|
||||
metadata: {
|
||||
contentType: string;
|
||||
};
|
||||
base64: boolean;
|
||||
}
|
||||
|
||||
interface CloudflareDeployment {
|
||||
id: string;
|
||||
url: string;
|
||||
environment: string;
|
||||
deployment_trigger: {
|
||||
type: string;
|
||||
};
|
||||
stages: Array<{
|
||||
name: string;
|
||||
status: string;
|
||||
}>;
|
||||
project_name: string;
|
||||
}
|
||||
|
||||
interface CloudflareProject {
|
||||
id: string;
|
||||
name: string;
|
||||
subdomain: string;
|
||||
domains: string[];
|
||||
created_on: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cloudflare Pages Deploy Adapter
|
||||
* Uses Cloudflare Pages Direct Upload API with API token
|
||||
*
|
||||
* Mimics the behavior of "wrangler pages deploy dist"
|
||||
*/
|
||||
export class CloudflareAdapter implements DeployAdapter {
|
||||
private fs: JSRuntimeFS;
|
||||
private apiToken: string;
|
||||
private accountId: string;
|
||||
private baseURL: string;
|
||||
private projectName?: string;
|
||||
private branch?: string;
|
||||
private corsProxy?: string;
|
||||
|
||||
constructor(config: CloudflareDeployConfig) {
|
||||
this.fs = config.fs;
|
||||
this.apiToken = config.apiToken;
|
||||
this.accountId = config.accountId;
|
||||
this.baseURL = config.baseURL || 'https://api.cloudflare.com/client/v4';
|
||||
this.projectName = config.projectName;
|
||||
this.branch = config.branch || 'main';
|
||||
this.corsProxy = config.corsProxy;
|
||||
}
|
||||
|
||||
async deploy(options: DeployOptions): Promise<DeployResult> {
|
||||
const { projectId, projectPath } = options;
|
||||
|
||||
// Check if dist directory exists and contains index.html
|
||||
const distPath = `${projectPath}/dist`;
|
||||
try {
|
||||
await this.fs.readFile(`${distPath}/index.html`, 'utf8');
|
||||
} catch {
|
||||
throw new Error('No index.html found in dist directory. Please build the project first.');
|
||||
}
|
||||
|
||||
// Determine project name
|
||||
const cfProjectName = this.projectName || projectId;
|
||||
|
||||
// Ensure project exists or create it
|
||||
let project: CloudflareProject;
|
||||
try {
|
||||
project = await this.getProject(cfProjectName);
|
||||
} catch {
|
||||
// Project doesn't exist, create it
|
||||
project = await this.createProject(cfProjectName);
|
||||
}
|
||||
|
||||
// Collect all files from dist directory
|
||||
const manifest: Record<string, string> = {};
|
||||
const files: CloudflareFile[] = [];
|
||||
await this.collectFiles(distPath, '', files, manifest);
|
||||
|
||||
// Create deployment using Direct Upload API
|
||||
const deployment = await this.createDeployment(cfProjectName, files, manifest);
|
||||
|
||||
// Construct the deployment URL
|
||||
const deploymentUrl = deployment.url || `https://${deployment.id}.${project.subdomain}.pages.dev`;
|
||||
|
||||
return {
|
||||
url: deploymentUrl,
|
||||
metadata: {
|
||||
deploymentId: deployment.id,
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
provider: 'cloudflare',
|
||||
environment: deployment.environment,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private async getProject(projectName: string): Promise<CloudflareProject> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}`;
|
||||
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url;
|
||||
|
||||
const response = await fetch(targetUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.apiToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => 'Unknown error');
|
||||
throw new Error(`Failed to get Cloudflare Pages project: ${response.status} ${response.statusText}. ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.result;
|
||||
}
|
||||
|
||||
private async createProject(projectName: string): Promise<CloudflareProject> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects`;
|
||||
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url;
|
||||
|
||||
const response = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.apiToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: projectName,
|
||||
production_branch: this.branch,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => 'Unknown error');
|
||||
throw new Error(`Failed to create Cloudflare Pages project: ${response.status} ${response.statusText}. ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.result;
|
||||
}
|
||||
|
||||
private async createDeployment(
|
||||
projectName: string,
|
||||
files: CloudflareFile[],
|
||||
manifest: Record<string, string>
|
||||
): Promise<CloudflareDeployment> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}/deployments`;
|
||||
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url;
|
||||
|
||||
// Create form data for multipart upload
|
||||
const formData = new FormData();
|
||||
|
||||
// Add manifest
|
||||
formData.append('manifest', JSON.stringify(manifest));
|
||||
|
||||
// Add each file
|
||||
for (const file of files) {
|
||||
const blob = new Blob([file.value], { type: file.metadata.contentType });
|
||||
formData.append(file.key, blob);
|
||||
}
|
||||
|
||||
const response = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.apiToken}`,
|
||||
// Don't set Content-Type - browser will set it with boundary for multipart/form-data
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => 'Unknown error');
|
||||
throw new Error(`Failed to create Cloudflare Pages deployment: ${response.status} ${response.statusText}. ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.result;
|
||||
}
|
||||
|
||||
private async collectFiles(
|
||||
dirPath: string,
|
||||
relativePath: string,
|
||||
files: CloudflareFile[],
|
||||
manifest: Record<string, string>
|
||||
): Promise<void> {
|
||||
try {
|
||||
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = `${dirPath}/${entry.name}`;
|
||||
const entryRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
// Recursively collect files from subdirectories
|
||||
await this.collectFiles(fullPath, entryRelativePath, files, manifest);
|
||||
} else if (entry.isFile()) {
|
||||
try {
|
||||
// Read file content
|
||||
const fileContent = await this.fs.readFile(fullPath);
|
||||
|
||||
// Determine content type based on file extension
|
||||
const contentType = this.getContentType(entry.name);
|
||||
|
||||
// Convert to base64 for binary files, or keep as string for text files
|
||||
let base64Content: string;
|
||||
let isBase64 = false;
|
||||
|
||||
if (this.isBinaryFile(entry.name)) {
|
||||
// Handle binary files (images, fonts, etc.)
|
||||
if (typeof fileContent === 'string') {
|
||||
base64Content = btoa(fileContent);
|
||||
} else {
|
||||
// Handle Uint8Array
|
||||
const binary = Array.from(fileContent)
|
||||
.map(byte => String.fromCharCode(byte))
|
||||
.join('');
|
||||
base64Content = btoa(binary);
|
||||
}
|
||||
isBase64 = true;
|
||||
} else {
|
||||
// Handle text files (HTML, CSS, JS, etc.)
|
||||
if (typeof fileContent === 'string') {
|
||||
base64Content = fileContent;
|
||||
} else {
|
||||
// Decode Uint8Array to string
|
||||
const decoder = new TextDecoder();
|
||||
base64Content = decoder.decode(fileContent);
|
||||
}
|
||||
}
|
||||
|
||||
// Add to files array
|
||||
files.push({
|
||||
key: entryRelativePath,
|
||||
value: base64Content,
|
||||
metadata: {
|
||||
contentType,
|
||||
},
|
||||
base64: isBase64,
|
||||
});
|
||||
|
||||
// Add to manifest (hash placeholder - Cloudflare will compute actual hash)
|
||||
manifest[entryRelativePath] = '';
|
||||
} catch (error) {
|
||||
console.warn(`Failed to read file ${fullPath}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to read directory ${dirPath}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get MIME type based on file extension
|
||||
*/
|
||||
private getContentType(filename: string): string {
|
||||
const ext = filename.split('.').pop()?.toLowerCase();
|
||||
|
||||
const mimeTypes: Record<string, string> = {
|
||||
// Text
|
||||
'html': 'text/html',
|
||||
'htm': 'text/html',
|
||||
'css': 'text/css',
|
||||
'js': 'application/javascript',
|
||||
'mjs': 'application/javascript',
|
||||
'json': 'application/json',
|
||||
'xml': 'application/xml',
|
||||
'txt': 'text/plain',
|
||||
'md': 'text/markdown',
|
||||
|
||||
// Images
|
||||
'png': 'image/png',
|
||||
'jpg': 'image/jpeg',
|
||||
'jpeg': 'image/jpeg',
|
||||
'gif': 'image/gif',
|
||||
'svg': 'image/svg+xml',
|
||||
'webp': 'image/webp',
|
||||
'ico': 'image/x-icon',
|
||||
|
||||
// Fonts
|
||||
'woff': 'font/woff',
|
||||
'woff2': 'font/woff2',
|
||||
'ttf': 'font/ttf',
|
||||
'otf': 'font/otf',
|
||||
'eot': 'application/vnd.ms-fontobject',
|
||||
|
||||
// Other
|
||||
'pdf': 'application/pdf',
|
||||
'zip': 'application/zip',
|
||||
'wasm': 'application/wasm',
|
||||
};
|
||||
|
||||
return mimeTypes[ext || ''] || 'application/octet-stream';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a file should be treated as binary
|
||||
*/
|
||||
private isBinaryFile(filename: string): boolean {
|
||||
const ext = filename.split('.').pop()?.toLowerCase();
|
||||
|
||||
const binaryExtensions = [
|
||||
'png', 'jpg', 'jpeg', 'gif', 'webp', 'ico',
|
||||
'woff', 'woff2', 'ttf', 'otf', 'eot',
|
||||
'pdf', 'zip', 'wasm',
|
||||
];
|
||||
|
||||
return binaryExtensions.includes(ext || '');
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@ export * from './ShakespeareAdapter';
|
||||
export * from './NetlifyAdapter';
|
||||
export * from './VercelAdapter';
|
||||
export * from './NsiteAdapter';
|
||||
export * from './CloudflareAdapter';
|
||||
@@ -54,3 +54,13 @@ export interface NsiteDeployConfig {
|
||||
relayUrls: string[];
|
||||
blossomServers: string[];
|
||||
}
|
||||
|
||||
export interface CloudflareDeployConfig {
|
||||
fs: JSRuntimeFS;
|
||||
apiToken: string;
|
||||
accountId: string;
|
||||
baseURL?: string;
|
||||
projectName?: string;
|
||||
branch?: string;
|
||||
corsProxy?: string;
|
||||
}
|
||||
+161
-26
@@ -45,12 +45,14 @@ import { normalizeUrl } from '@/lib/faviconUrl';
|
||||
|
||||
interface PresetProvider {
|
||||
id: string;
|
||||
type: 'shakespeare' | 'netlify' | 'vercel' | 'nsite';
|
||||
type: 'shakespeare' | 'netlify' | 'vercel' | 'nsite' | 'cloudflare';
|
||||
name: string;
|
||||
description: string;
|
||||
requiresNostr?: boolean;
|
||||
apiKeyLabel?: string;
|
||||
apiKeyURL?: string;
|
||||
accountIdLabel?: string;
|
||||
accountIdURL?: string;
|
||||
proxy?: boolean;
|
||||
}
|
||||
|
||||
@@ -186,29 +188,54 @@ function SortableProviderItem({ provider, index, preset, onRemove, onUpdate, sho
|
||||
<PasswordInput
|
||||
id={`provider-${index}-apiKey`}
|
||||
placeholder={t('enterApiKey')}
|
||||
value={provider.apiKey}
|
||||
value={
|
||||
provider.type === 'cloudflare' ? provider.apiToken :
|
||||
provider.type === 'netlify' ? provider.apiKey :
|
||||
provider.type === 'vercel' ? provider.apiKey : ''
|
||||
}
|
||||
onChange={(e) => {
|
||||
if (provider.type === 'netlify') {
|
||||
onUpdate(index, { ...provider, apiKey: e.target.value });
|
||||
} else if (provider.type === 'vercel') {
|
||||
onUpdate(index, { ...provider, apiKey: e.target.value });
|
||||
} else if (provider.type === 'cloudflare') {
|
||||
onUpdate(index, { ...provider, apiToken: e.target.value });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{provider.type === 'cloudflare' && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`provider-${index}-accountId`}>
|
||||
Account ID
|
||||
</Label>
|
||||
<Input
|
||||
id={`provider-${index}-accountId`}
|
||||
placeholder="Enter your Cloudflare Account ID"
|
||||
value={provider.accountId || ''}
|
||||
onChange={(e) => onUpdate(index, { ...provider, accountId: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`provider-${index}-baseURL`}>
|
||||
Base URL (Optional)
|
||||
</Label>
|
||||
<Input
|
||||
id={`provider-${index}-baseURL`}
|
||||
placeholder={provider.type === 'netlify' ? 'https://api.netlify.com/api/v1' : 'https://api.vercel.com'}
|
||||
placeholder={
|
||||
provider.type === 'netlify' ? 'https://api.netlify.com/api/v1' :
|
||||
provider.type === 'cloudflare' ? 'https://api.cloudflare.com/client/v4' :
|
||||
'https://api.vercel.com'
|
||||
}
|
||||
value={provider.baseURL || ''}
|
||||
onChange={(e) => {
|
||||
if (provider.type === 'netlify') {
|
||||
onUpdate(index, { ...provider, baseURL: e.target.value });
|
||||
} else if (provider.type === 'vercel') {
|
||||
onUpdate(index, { ...provider, baseURL: e.target.value });
|
||||
} else if (provider.type === 'cloudflare') {
|
||||
onUpdate(index, { ...provider, baseURL: e.target.value });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
@@ -256,6 +283,17 @@ const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
name: 'nsite',
|
||||
description: 'Deploy to Nostr as a static website',
|
||||
},
|
||||
{
|
||||
id: 'cloudflare',
|
||||
type: 'cloudflare',
|
||||
name: 'Cloudflare Pages',
|
||||
description: 'Deploy to Cloudflare Pages',
|
||||
apiKeyLabel: 'API Token',
|
||||
apiKeyURL: 'https://dash.cloudflare.com/profile/api-tokens',
|
||||
accountIdLabel: 'Account ID',
|
||||
accountIdURL: 'https://dash.cloudflare.com/',
|
||||
proxy: true,
|
||||
},
|
||||
{
|
||||
id: 'netlify',
|
||||
type: 'netlify',
|
||||
@@ -306,6 +344,13 @@ function getProviderUrl(provider: DeployProvider | PresetProvider): string | nul
|
||||
}
|
||||
return 'https://vercel.com';
|
||||
|
||||
case 'cloudflare':
|
||||
// For Cloudflare providers, check for custom baseURL first, then use default
|
||||
if ('baseURL' in provider && provider.baseURL) {
|
||||
return provider.baseURL;
|
||||
}
|
||||
return 'https://cloudflare.com';
|
||||
|
||||
case 'nsite':
|
||||
// nsite uses Rocket icon fallback
|
||||
return null;
|
||||
@@ -327,12 +372,14 @@ export function DeploySettings() {
|
||||
const vercelOAuth = useVercelOAuth();
|
||||
|
||||
const [presetApiKeys, setPresetApiKeys] = useState<Record<string, string>>({});
|
||||
const [presetAccountIds, setPresetAccountIds] = useState<Record<string, string>>({});
|
||||
const [forceManualEntry, setForceManualEntry] = useState<Record<string, boolean>>({});
|
||||
|
||||
// Custom provider form state
|
||||
const [customProviderType, setCustomProviderType] = useState<'shakespeare' | 'netlify' | 'vercel' | 'nsite' | ''>('');
|
||||
const [customProviderType, setCustomProviderType] = useState<'shakespeare' | 'netlify' | 'vercel' | 'nsite' | 'cloudflare' | ''>('');
|
||||
const [customName, setCustomName] = useState('');
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
const [customAccountId, setCustomAccountId] = useState('');
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
const [customHost, setCustomHost] = useState('');
|
||||
const [customProxy, setCustomProxy] = useState(false);
|
||||
@@ -366,12 +413,18 @@ export function DeploySettings() {
|
||||
}
|
||||
|
||||
const apiKey = presetApiKeys[preset.id];
|
||||
const accountId = presetAccountIds[preset.id];
|
||||
|
||||
// For non-Shakespeare/non-nsite providers, require API key
|
||||
if (!preset.requiresNostr && preset.type !== 'nsite' && !apiKey?.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For Cloudflare, also require account ID
|
||||
if (preset.type === 'cloudflare' && !accountId?.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let newProvider: DeployProvider;
|
||||
|
||||
if (preset.type === 'shakespeare') {
|
||||
@@ -400,6 +453,15 @@ export function DeploySettings() {
|
||||
'https://cdn.sovbit.host',
|
||||
],
|
||||
};
|
||||
} else if (preset.type === 'cloudflare') {
|
||||
newProvider = {
|
||||
id: preset.id, // Use preset ID for presets
|
||||
name: preset.name,
|
||||
type: 'cloudflare',
|
||||
apiToken: apiKey.trim(),
|
||||
accountId: accountId.trim(),
|
||||
...(preset.proxy && { proxy: true }),
|
||||
};
|
||||
} else if (preset.type === 'netlify') {
|
||||
newProvider = {
|
||||
id: preset.id, // Use preset ID for presets
|
||||
@@ -422,6 +484,7 @@ export function DeploySettings() {
|
||||
|
||||
// Clear inputs
|
||||
setPresetApiKeys(prev => ({ ...prev, [preset.id]: '' }));
|
||||
setPresetAccountIds(prev => ({ ...prev, [preset.id]: '' }));
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
@@ -454,6 +517,17 @@ export function DeploySettings() {
|
||||
relayUrls,
|
||||
blossomServers,
|
||||
};
|
||||
} else if (customProviderType === 'cloudflare') {
|
||||
if (!customApiKey.trim() || !customAccountId.trim()) return;
|
||||
newProvider = {
|
||||
id: generateCustomProviderId(customProviderType), // Generate custom ID
|
||||
name: customName.trim(),
|
||||
type: 'cloudflare',
|
||||
apiToken: customApiKey.trim(),
|
||||
accountId: customAccountId.trim(),
|
||||
...(customBaseURL?.trim() && { baseURL: customBaseURL.trim() }),
|
||||
...(customProxy && { proxy: true }),
|
||||
};
|
||||
} else if (customProviderType === 'netlify') {
|
||||
if (!customApiKey.trim()) return;
|
||||
newProvider = {
|
||||
@@ -482,6 +556,7 @@ export function DeploySettings() {
|
||||
setCustomProviderType('');
|
||||
setCustomName('');
|
||||
setCustomApiKey('');
|
||||
setCustomAccountId('');
|
||||
setCustomBaseURL('');
|
||||
setCustomHost('');
|
||||
setCustomProxy(false);
|
||||
@@ -694,34 +769,77 @@ export function DeploySettings() {
|
||||
) : (
|
||||
// Show manual token input
|
||||
<div className="space-y-2">
|
||||
<div className="flex gap-2">
|
||||
{!preset.requiresNostr && preset.type !== 'nsite' && (
|
||||
{preset.type === 'cloudflare' ? (
|
||||
// Cloudflare needs both API token and Account ID
|
||||
<>
|
||||
<PasswordInput
|
||||
placeholder={preset.apiKeyLabel || t('enterApiKey')}
|
||||
className="flex-1"
|
||||
value={presetApiKeys[preset.id] || ''}
|
||||
onChange={(e) => setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
<Input
|
||||
placeholder={preset.accountIdLabel || 'Account ID'}
|
||||
value={presetAccountIds[preset.id] || ''}
|
||||
onChange={(e) => setPresetAccountIds(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && presetApiKeys[preset.id]?.trim()) {
|
||||
if (e.key === 'Enter' && presetApiKeys[preset.id]?.trim() && presetAccountIds[preset.id]?.trim()) {
|
||||
handleAddPresetProvider(preset);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
(preset.requiresNostr && !isLoggedIntoNostr) ||
|
||||
(!preset.requiresNostr && preset.type !== 'nsite' && !presetApiKeys[preset.id]?.trim())
|
||||
}
|
||||
className={(preset.requiresNostr || preset.type === 'nsite') ? "w-full" : "h-10 px-4 ml-auto"}
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
{preset.accountIdURL && (
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-muted-foreground underline hover:text-foreground"
|
||||
onClick={() => window.open(preset.accountIdURL, '_blank')}
|
||||
>
|
||||
Find your Account ID
|
||||
</button>
|
||||
)}
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={!presetApiKeys[preset.id]?.trim() || !presetAccountIds[preset.id]?.trim()}
|
||||
className="w-full"
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<div className="flex gap-2">
|
||||
{!preset.requiresNostr && preset.type !== 'nsite' && (
|
||||
<PasswordInput
|
||||
placeholder={preset.apiKeyLabel || t('enterApiKey')}
|
||||
className="flex-1"
|
||||
value={presetApiKeys[preset.id] || ''}
|
||||
onChange={(e) => setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && presetApiKeys[preset.id]?.trim()) {
|
||||
handleAddPresetProvider(preset);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
(preset.requiresNostr && !isLoggedIntoNostr) ||
|
||||
(!preset.requiresNostr && preset.type !== 'nsite' && !presetApiKeys[preset.id]?.trim())
|
||||
}
|
||||
className={(preset.requiresNostr || preset.type === 'nsite') ? "w-full" : "h-10 px-4 ml-auto"}
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -744,10 +862,11 @@ export function DeploySettings() {
|
||||
<Label htmlFor="custom-provider-type">{t('providerType')}</Label>
|
||||
<Select
|
||||
value={customProviderType}
|
||||
onValueChange={(value: 'shakespeare' | 'netlify' | 'vercel' | 'nsite') => {
|
||||
onValueChange={(value: 'shakespeare' | 'netlify' | 'vercel' | 'nsite' | 'cloudflare') => {
|
||||
setCustomProviderType(value);
|
||||
// Reset form when provider type changes
|
||||
setCustomApiKey('');
|
||||
setCustomAccountId('');
|
||||
setCustomBaseURL('');
|
||||
setCustomHost('');
|
||||
setCustomProxy(false);
|
||||
@@ -762,6 +881,7 @@ export function DeploySettings() {
|
||||
<SelectContent>
|
||||
<SelectItem value="shakespeare">Shakespeare Deploy</SelectItem>
|
||||
<SelectItem value="nsite">nsite</SelectItem>
|
||||
<SelectItem value="cloudflare">Cloudflare Pages</SelectItem>
|
||||
<SelectItem value="netlify">Netlify</SelectItem>
|
||||
<SelectItem value="vercel">Vercel</SelectItem>
|
||||
</SelectContent>
|
||||
@@ -832,7 +952,8 @@ export function DeploySettings() {
|
||||
<>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-apikey">
|
||||
{customProviderType === 'netlify' ? 'Personal Access Token' : 'Access Token'}
|
||||
{customProviderType === 'cloudflare' ? 'API Token' :
|
||||
customProviderType === 'netlify' ? 'Personal Access Token' : 'Access Token'}
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id="custom-apikey"
|
||||
@@ -841,14 +962,27 @@ export function DeploySettings() {
|
||||
onChange={(e) => setCustomApiKey(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
{customProviderType === 'cloudflare' && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-accountid">Account ID</Label>
|
||||
<Input
|
||||
id="custom-accountid"
|
||||
placeholder="Enter your Cloudflare Account ID"
|
||||
value={customAccountId}
|
||||
onChange={(e) => setCustomAccountId(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-baseurl">Base URL (Optional)</Label>
|
||||
<Input
|
||||
id="custom-baseurl"
|
||||
placeholder={
|
||||
customProviderType === 'netlify'
|
||||
? 'https://api.netlify.com/api/v1'
|
||||
: 'https://api.vercel.com'
|
||||
customProviderType === 'cloudflare'
|
||||
? 'https://api.cloudflare.com/client/v4'
|
||||
: customProviderType === 'netlify'
|
||||
? 'https://api.netlify.com/api/v1'
|
||||
: 'https://api.vercel.com'
|
||||
}
|
||||
value={customBaseURL}
|
||||
onChange={(e) => setCustomBaseURL(e.target.value)}
|
||||
@@ -873,7 +1007,8 @@ export function DeploySettings() {
|
||||
disabled={
|
||||
!customProviderType ||
|
||||
!customName.trim() ||
|
||||
(customProviderType !== 'shakespeare' && customProviderType !== 'nsite' && !customApiKey.trim())
|
||||
(customProviderType === 'cloudflare' && (!customApiKey.trim() || !customAccountId.trim())) ||
|
||||
(customProviderType !== 'shakespeare' && customProviderType !== 'nsite' && customProviderType !== 'cloudflare' && !customApiKey.trim())
|
||||
}
|
||||
className="gap-2 ml-auto"
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user