2025-10-15 09:15:25 +08:00
|
|
|
|
import React, { useState } from "react";
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
import { Play, Wand2 } from "lucide-react";
|
2025-10-17 17:49:16 +08:00
|
|
|
|
import { toast } from "sonner";
|
2025-10-19 11:55:46 +08:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-10-15 09:15:25 +08:00
|
|
|
|
import { Provider, UsageScript } from "../types";
|
2025-10-16 12:13:51 +08:00
|
|
|
|
import { usageApi, type AppType } from "@/lib/api";
|
2025-10-15 09:15:25 +08:00
|
|
|
|
import JsonEditor from "./JsonEditor";
|
|
|
|
|
|
import * as prettier from "prettier/standalone";
|
|
|
|
|
|
import * as parserBabel from "prettier/parser-babel";
|
|
|
|
|
|
import * as pluginEstree from "prettier/plugins/estree";
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
DialogFooter,
|
|
|
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-10-15 09:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
interface UsageScriptModalProps {
|
|
|
|
|
|
provider: Provider;
|
|
|
|
|
|
appType: AppType;
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
isOpen: boolean;
|
2025-10-15 09:15:25 +08:00
|
|
|
|
onClose: () => void;
|
|
|
|
|
|
onSave: (script: UsageScript) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 预设模板(JS 对象字面量格式)
|
|
|
|
|
|
const PRESET_TEMPLATES: Record<string, string> = {
|
|
|
|
|
|
通用模板: `({
|
|
|
|
|
|
request: {
|
|
|
|
|
|
url: "{{baseUrl}}/user/balance",
|
|
|
|
|
|
method: "GET",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Authorization": "Bearer {{apiKey}}",
|
|
|
|
|
|
"User-Agent": "cc-switch/1.0"
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
extractor: function(response) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
isValid: response.is_active || true,
|
|
|
|
|
|
remaining: response.balance,
|
|
|
|
|
|
unit: "USD"
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
})`,
|
|
|
|
|
|
|
|
|
|
|
|
NewAPI: `({
|
|
|
|
|
|
request: {
|
|
|
|
|
|
url: "{{baseUrl}}/api/usage/token",
|
|
|
|
|
|
method: "GET",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Authorization: "Bearer {{apiKey}}",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
extractor: function (response) {
|
|
|
|
|
|
if (response.code) {
|
|
|
|
|
|
if (response.data.unlimited_quota) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
planName: response.data.name,
|
|
|
|
|
|
total: -1,
|
|
|
|
|
|
used: response.data.total_used / 500000,
|
|
|
|
|
|
unit: "USD",
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
isValid: true,
|
|
|
|
|
|
planName: response.data.name,
|
|
|
|
|
|
total: response.data.total_granted / 500000,
|
|
|
|
|
|
used: response.data.total_used / 500000,
|
|
|
|
|
|
remaining: response.data.total_available / 500000,
|
|
|
|
|
|
unit: "USD",
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
if (response.error) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
isValid: false,
|
|
|
|
|
|
invalidMessage: response.error.message,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})`,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const UsageScriptModal: React.FC<UsageScriptModalProps> = ({
|
|
|
|
|
|
provider,
|
|
|
|
|
|
appType,
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
isOpen,
|
2025-10-15 09:15:25 +08:00
|
|
|
|
onClose,
|
|
|
|
|
|
onSave,
|
|
|
|
|
|
}) => {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
const { t } = useTranslation();
|
2025-10-15 09:15:25 +08:00
|
|
|
|
const [script, setScript] = useState<UsageScript>(() => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
provider.meta?.usage_script || {
|
|
|
|
|
|
enabled: false,
|
|
|
|
|
|
language: "javascript",
|
2025-10-19 11:55:46 +08:00
|
|
|
|
code: PRESET_TEMPLATES[t("usageScript.presetTemplate") === "预设模板" ? "通用模板" : "General"],
|
2025-10-15 09:15:25 +08:00
|
|
|
|
timeout: 10,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const [testing, setTesting] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const handleSave = () => {
|
|
|
|
|
|
// 验证脚本格式
|
|
|
|
|
|
if (script.enabled && !script.code.trim()) {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.error(t("usageScript.scriptEmpty"));
|
2025-10-15 09:15:25 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 基本的 JS 语法检查(检查是否包含 return 语句)
|
|
|
|
|
|
if (script.enabled && !script.code.includes("return")) {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.error(t("usageScript.mustHaveReturn"), { duration: 5000 });
|
2025-10-15 09:15:25 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSave(script);
|
|
|
|
|
|
onClose();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleTest = async () => {
|
|
|
|
|
|
setTesting(true);
|
|
|
|
|
|
try {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
const result = await usageApi.query(provider.id, appType);
|
2025-10-15 09:15:25 +08:00
|
|
|
|
if (result.success && result.data && result.data.length > 0) {
|
|
|
|
|
|
// 显示所有套餐数据
|
|
|
|
|
|
const summary = result.data
|
|
|
|
|
|
.map((plan) => {
|
|
|
|
|
|
const planInfo = plan.planName ? `[${plan.planName}]` : "";
|
2025-10-19 11:55:46 +08:00
|
|
|
|
return `${planInfo} ${t("usage.remaining")} ${plan.remaining} ${plan.unit}`;
|
2025-10-15 09:15:25 +08:00
|
|
|
|
})
|
|
|
|
|
|
.join(", ");
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.success(`${t("usageScript.testSuccess")}${summary}`, { duration: 3000 });
|
2025-10-15 09:15:25 +08:00
|
|
|
|
} else {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.error(`${t("usageScript.testFailed")}: ${result.error || t("endpointTest.noResult")}`, {
|
2025-10-17 17:49:16 +08:00
|
|
|
|
duration: 5000,
|
|
|
|
|
|
});
|
2025-10-15 09:15:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error: any) {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.error(`${t("usageScript.testFailed")}: ${error?.message || t("common.unknown")}`, {
|
2025-10-17 17:49:16 +08:00
|
|
|
|
duration: 5000,
|
|
|
|
|
|
});
|
2025-10-15 09:15:25 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
setTesting(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleFormat = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const formatted = await prettier.format(script.code, {
|
|
|
|
|
|
parser: "babel",
|
|
|
|
|
|
plugins: [parserBabel as any, pluginEstree as any],
|
|
|
|
|
|
semi: true,
|
|
|
|
|
|
singleQuote: false,
|
|
|
|
|
|
tabWidth: 2,
|
|
|
|
|
|
printWidth: 80,
|
|
|
|
|
|
});
|
|
|
|
|
|
setScript({ ...script, code: formatted.trim() });
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.success(t("usageScript.formatSuccess"), { duration: 1000 });
|
2025-10-15 09:15:25 +08:00
|
|
|
|
} catch (error: any) {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
toast.error(`${t("usageScript.formatFailed")}: ${error?.message || t("jsonEditor.invalidJson")}`, {
|
2025-10-17 17:49:16 +08:00
|
|
|
|
duration: 3000,
|
|
|
|
|
|
});
|
2025-10-15 09:15:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleUsePreset = (presetName: string) => {
|
|
|
|
|
|
const preset = PRESET_TEMPLATES[presetName];
|
|
|
|
|
|
if (preset) {
|
|
|
|
|
|
setScript({ ...script, code: preset });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
|
|
|
|
|
<DialogContent className="max-w-3xl max-h-[90vh] flex flex-col">
|
|
|
|
|
|
<DialogHeader>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<DialogTitle>{t("usageScript.title")} - {provider.name}</DialogTitle>
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</DialogHeader>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
{/* Content - Scrollable */}
|
2025-10-18 16:52:02 +08:00
|
|
|
|
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
|
2025-10-15 09:15:25 +08:00
|
|
|
|
{/* 启用开关 */}
|
|
|
|
|
|
<label className="flex items-center gap-2 cursor-pointer">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="checkbox"
|
|
|
|
|
|
checked={script.enabled}
|
|
|
|
|
|
onChange={(e) =>
|
|
|
|
|
|
setScript({ ...script, enabled: e.target.checked })
|
|
|
|
|
|
}
|
|
|
|
|
|
className="w-4 h-4"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.enableUsageQuery")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
|
|
|
|
|
|
{script.enabled && (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{/* 预设模板选择 */}
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.presetTemplate")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</label>
|
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
|
{Object.keys(PRESET_TEMPLATES).map((name) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={name}
|
|
|
|
|
|
onClick={() => handleUsePreset(name)}
|
|
|
|
|
|
className="px-3 py-1.5 text-xs bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded transition-colors"
|
|
|
|
|
|
>
|
|
|
|
|
|
{name}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 脚本编辑器 */}
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<label className="block text-sm font-medium mb-2 text-gray-900 dark:text-gray-100">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.queryScript")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</label>
|
|
|
|
|
|
<JsonEditor
|
|
|
|
|
|
value={script.code}
|
|
|
|
|
|
onChange={(code) => setScript({ ...script, code })}
|
|
|
|
|
|
height="300px"
|
|
|
|
|
|
language="javascript"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<p className="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.variablesHint", {
|
|
|
|
|
|
apiKey: "{{apiKey}}",
|
|
|
|
|
|
baseUrl: "{{baseUrl}}"
|
|
|
|
|
|
})}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 配置选项 */}
|
|
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
|
|
|
|
<label className="block">
|
|
|
|
|
|
<span className="text-sm font-medium text-gray-900 dark:text-gray-100">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.timeoutSeconds")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
min="2"
|
|
|
|
|
|
max="30"
|
|
|
|
|
|
value={script.timeout || 10}
|
|
|
|
|
|
onChange={(e) =>
|
2025-10-16 12:13:51 +08:00
|
|
|
|
setScript({
|
|
|
|
|
|
...script,
|
|
|
|
|
|
timeout: parseInt(e.target.value),
|
|
|
|
|
|
})
|
2025-10-15 09:15:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
className="mt-1 w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 脚本说明 */}
|
|
|
|
|
|
<div className="p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-sm text-gray-700 dark:text-gray-300">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<h4 className="font-medium mb-2">{t("usageScript.scriptHelp")}</h4>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<div className="space-y-3 text-xs">
|
|
|
|
|
|
<div>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<strong>{t("usageScript.configFormat")}</strong>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<pre className="mt-1 p-2 bg-white/50 dark:bg-black/20 rounded text-[10px] overflow-x-auto">
|
2025-10-16 12:13:51 +08:00
|
|
|
|
{`({
|
2025-10-15 09:15:25 +08:00
|
|
|
|
request: {
|
|
|
|
|
|
url: "{{baseUrl}}/api/usage",
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Authorization": "Bearer {{apiKey}}",
|
|
|
|
|
|
"User-Agent": "cc-switch/1.0"
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify({ key: "value" }) // 可选
|
|
|
|
|
|
},
|
|
|
|
|
|
extractor: function(response) {
|
|
|
|
|
|
// response 是 API 返回的 JSON 数据
|
|
|
|
|
|
return {
|
|
|
|
|
|
isValid: !response.error,
|
|
|
|
|
|
remaining: response.balance,
|
|
|
|
|
|
unit: "USD"
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
})`}
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<strong>{t("usageScript.extractorFormat")}</strong>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<ul className="mt-1 space-y-0.5 ml-2">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<li>{t("usageScript.fieldIsValid")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldInvalidMessage")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldRemaining")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldUnit")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldPlanName")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldTotal")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldUsed")}</li>
|
|
|
|
|
|
<li>{t("usageScript.fieldExtra")}</li>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="text-gray-600 dark:text-gray-400">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<strong>{t("usageScript.tips")}</strong>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<ul className="mt-1 space-y-0.5 ml-2">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<li>{t("usageScript.tip1", { apiKey: "{{apiKey}}", baseUrl: "{{baseUrl}}" })}</li>
|
|
|
|
|
|
<li>{t("usageScript.tip2")}</li>
|
|
|
|
|
|
<li>{t("usageScript.tip3")}</li>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Footer */}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
<DialogFooter className="flex-col sm:flex-row sm:justify-between gap-3 pt-4">
|
|
|
|
|
|
{/* Left side - Test and Format buttons */}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<div className="flex gap-2">
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
2025-10-15 09:15:25 +08:00
|
|
|
|
onClick={handleTest}
|
|
|
|
|
|
disabled={!script.enabled || testing}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Play size={14} />
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{testing ? t("usageScript.testing") : t("usageScript.testScript")}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
2025-10-15 09:15:25 +08:00
|
|
|
|
onClick={handleFormat}
|
|
|
|
|
|
disabled={!script.enabled}
|
2025-10-19 11:55:46 +08:00
|
|
|
|
title={t("usageScript.format")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Wand2 size={14} />
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.format")}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</Button>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
{/* Right side - Cancel and Save buttons */}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<div className="flex gap-2">
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
<Button variant="ghost" size="sm" onClick={onClose}>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("common.cancel")}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
<Button variant="default" size="sm" onClick={handleSave}>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usageScript.saveConfig")}
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</Button>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</div>
|
refactor: migrate UsageScriptModal to shadcn/ui Dialog component
Migrate the usage script configuration modal from custom modal implementation to shadcn/ui Dialog component to maintain consistent styling across the entire application.
## Changes
### UsageScriptModal.tsx
- Replace custom modal structure (fixed positioning, backdrop) with Dialog component
- Remove X icon import (Dialog includes built-in close button)
- Add Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter imports
- Add Button component import for action buttons
- Update props interface to include isOpen boolean prop
- Restructure component layout:
- Use DialogHeader with DialogTitle for header section
- Apply -mx-6 px-6 pattern for full-width scrollable content
- Use DialogFooter with flex-col sm:flex-row sm:justify-between layout
- Convert custom buttons to Button components:
- Test/Format buttons: variant="outline" size="sm"
- Cancel button: variant="ghost" size="sm"
- Save button: variant="default" size="sm"
- Maintain all existing functionality (preset templates, JSON editor, validation, testing, formatting)
### App.tsx
- Update UsageScriptModal usage to pass isOpen prop
- Use Boolean(usageProvider) to control dialog open state
## Benefits
- **Consistent styling**: All dialogs now use the same shadcn/ui Dialog component
- **Better accessibility**: Automatic focus management, ESC key handling, ARIA attributes
- **Code maintainability**: Reduced custom modal boilerplate, easier to update styling globally
- **User experience**: Unified look and feel across settings, providers, MCP, and usage script dialogs
All TypeScript type checks and Prettier formatting checks pass.
2025-10-16 16:32:50 +08:00
|
|
|
|
</DialogFooter>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default UsageScriptModal;
|