Skip to content

Commit 5da7bce

Browse files
committed
refactor: clean up code formatting and improve readability in setup components
- Removed unnecessary blank lines and adjusted indentation for better code clarity in ExtensionLauncher and SetupContent components. - Enhanced the formatting of utility function imports in extension-utils for consistency. - Streamlined the handling of plugin ID and token saving logic to improve maintainability.
1 parent 44dfcde commit 5da7bce

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

frontend/components/setup/ExtensionLauncher.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export function ExtensionLauncher({ onSidebarOpened }: ExtensionLauncherProps) {
4141

4242
if (success) {
4343
setSidebarOpened(true);
44-
44+
4545
// 如果提供了回调函数,则执行它
4646
if (onSidebarOpened) {
4747
onSidebarOpened();
4848
}
49-
49+
5050
// 显示固定侧边栏的提示
5151
setTimeout(() => {
5252
setShowTooltip(true);

frontend/components/setup/SetupContent.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { useRouter } from "next/navigation";
1515
import { useSearchParams } from "next/navigation";
1616
import { ExtensionLauncher } from "./ExtensionLauncher";
1717
import { useAuth } from "@/lib/auth";
18-
import {
19-
getExtensionPluginId,
20-
saveTokenToExtension
18+
import {
19+
getExtensionPluginId,
20+
saveTokenToExtension,
2121
} from "@/lib/extension-utils";
2222
import { useToast } from "@/components/ui/use-toast";
2323

@@ -156,7 +156,7 @@ const CompleteStep = ({ fromExtension, onFinish }: CompleteStepProps) => (
156156
<p className="text-muted-foreground">
157157
您已成功完成 Nexus 的初始设置。现在您可以开始体验全部功能。
158158
</p>
159-
159+
160160
{fromExtension && (
161161
<div className="mt-4 text-sm text-green-600 dark:text-green-400">
162162
您的浏览器扩展将自动配置,无需额外设置。
@@ -178,16 +178,20 @@ export function SetupContent() {
178178
const router = useRouter();
179179
const { user } = useAuth();
180180
const searchParams = useSearchParams();
181-
const [extensionPluginId, setExtensionPluginId] = useState<string | null>(null);
182-
const [extensionCallback, setExtensionCallback] = useState<string | null>(null);
181+
const [extensionPluginId, setExtensionPluginId] = useState<string | null>(
182+
null,
183+
);
184+
const [extensionCallback, setExtensionCallback] = useState<string | null>(
185+
null,
186+
);
183187
const [tokenSent, setTokenSent] = useState(false);
184188
const { toast } = useToast();
185-
189+
186190
// 检查URL参数中是否包含plugin_id和extension_callback
187191
useEffect(() => {
188192
const pluginId = searchParams?.get("plugin_id");
189193
const callback = searchParams?.get("extension_callback");
190-
194+
191195
// 如果URL中有plugin_id,则保存它
192196
if (pluginId) {
193197
console.log("Setup页面从URL获取了plugin_id:", pluginId);
@@ -203,27 +207,30 @@ export function SetupContent() {
203207
}
204208
fetchPluginId();
205209
}
206-
210+
207211
if (callback) {
208212
setExtensionCallback(callback);
209213
}
210214
}, [searchParams]);
211-
215+
212216
// 在完成设置时向扩展发送Token
213217
const handleFinish = async () => {
214218
if (user?.token && extensionPluginId && !tokenSent) {
215219
try {
216220
console.log("Setup页面尝试向扩展发送Token");
217-
const success = await saveTokenToExtension(user.token, extensionPluginId);
218-
221+
const success = await saveTokenToExtension(
222+
user.token,
223+
extensionPluginId,
224+
);
225+
219226
if (success) {
220227
setTokenSent(true);
221228
toast({
222229
title: "扩展配置成功",
223230
description: "Nexus扩展已完成设置",
224231
variant: "default",
225232
});
226-
233+
227234
// 如果有回调URL,则重定向
228235
if (extensionCallback) {
229236
window.location.href = `${extensionCallback}?token=${encodeURIComponent(user.token)}`;
@@ -240,7 +247,7 @@ export function SetupContent() {
240247
console.error("发送Token到扩展时出错:", error);
241248
}
242249
}
243-
250+
244251
// 如果没有扩展或发送失败,则正常重定向到仪表盘
245252
router.push("/dashboard");
246253
};
@@ -259,17 +266,19 @@ export function SetupContent() {
259266
setCurrentStep(currentStep - 1);
260267
}
261268
};
262-
269+
263270
// 动态添加完成步骤组件
264271
const AllStepComponents = [
265-
...StepComponents,
266-
(props: CompleteStepProps) => <CompleteStep
267-
fromExtension={!!extensionPluginId}
268-
onFinish={handleFinish}
269-
{...props}
270-
/>
272+
...StepComponents,
273+
(props: CompleteStepProps) => (
274+
<CompleteStep
275+
fromExtension={!!extensionPluginId}
276+
onFinish={handleFinish}
277+
{...props}
278+
/>
279+
),
271280
];
272-
281+
273282
const CurrentStepComponent = AllStepComponents[currentStep];
274283

275284
return (

frontend/lib/extension-utils.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,16 @@ export const getExtensionPluginId = async (): Promise<string | null> => {
154154
sendMessage: (
155155
id: string,
156156
msg: object,
157-
cb: (response: ExtensionResponse & { pluginId?: string }) => void,
157+
cb: (
158+
response: ExtensionResponse & { pluginId?: string },
159+
) => void,
158160
) => void;
159161
};
160162
}
161-
).runtime.sendMessage(
162-
extensionId,
163-
{ action: "ping" },
164-
(response) => {
165-
clearTimeout(timeoutId);
166-
resolve(response?.pluginId || null);
167-
},
168-
);
163+
).runtime.sendMessage(extensionId, { action: "ping" }, (response) => {
164+
clearTimeout(timeoutId);
165+
resolve(response?.pluginId || null);
166+
});
169167
} else {
170168
resolve(null);
171169
}
@@ -184,7 +182,7 @@ export const getExtensionPluginId = async (): Promise<string | null> => {
184182
*/
185183
export const saveTokenToExtension = async (
186184
token: string,
187-
pluginId: string | null
185+
pluginId: string | null,
188186
): Promise<boolean> => {
189187
// 确保代码在浏览器环境中运行
190188
if (typeof window === "undefined" || !window.chrome) {
@@ -219,12 +217,12 @@ export const saveTokenToExtension = async (
219217
}
220218
).runtime.sendMessage(
221219
extensionId,
222-
{
223-
action: "saveToken",
224-
data: {
220+
{
221+
action: "saveToken",
222+
data: {
225223
token,
226-
pluginId
227-
}
224+
pluginId,
225+
},
228226
},
229227
(response) => {
230228
clearTimeout(timeoutId);

0 commit comments

Comments
 (0)