Skip to content

Commit d9083ce

Browse files
committed
refactor: optimize sync tabs with improved state management and performance
- Refactored DynamicTab, ArticleTab, and VideoTab with more efficient state handling - Introduced useCallback and useMemo for performance optimization - Simplified file handling and platform selection logic - Updated PlatformCheckbox component with more compact design - Improved event listener management and file processing - Reduced code complexity and improved readability
1 parent 8eb9443 commit d9083ce

File tree

5 files changed

+299
-273
lines changed

5 files changed

+299
-273
lines changed

src/components/Sync/AboutTab.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ const AboutTab: React.FC = () => {
77
<div className="flex flex-col gap-4">
88
<Card className="shadow-none bg-default-50">
99
<CardBody className="gap-6">
10-
<div className="flex items-center justify-center">
10+
<div className="flex justify-center items-center">
1111
<img
1212
src="/assets/icon.png"
1313
alt="logo"
1414
className="w-24 h-24 rounded-xl shadow-lg"
1515
/>
1616
</div>
17-
<div className="text-center space-y-2">
17+
<div className="space-y-2 text-center">
1818
<h2 className="text-2xl font-bold">{chrome.i18n.getMessage('extensionDisplayName')}</h2>
1919
<p className="text-sm text-foreground/60">v{chrome.runtime.getManifest().version}</p>
2020
</div>
21-
<p className="text-center text-foreground/80 text-base">{chrome.i18n.getMessage('aboutDescription')}</p>
21+
<p className="text-base text-center text-foreground/80">{chrome.i18n.getMessage('aboutDescription')}</p>
2222
</CardBody>
2323
</Card>
2424

2525
<Card className="shadow-none bg-default-50">
2626
<CardBody className="gap-4">
27-
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
27+
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
2828
<Button
2929
as={Link}
3030
href="https://github.com/leaper-one/MultiPost-Extension"
3131
target="_blank"
3232
rel="noopener noreferrer"
3333
variant="flat"
34-
className="w-full justify-start">
35-
<GithubIcon className="w-5 h-5 mr-2" />
34+
className="justify-start w-full">
35+
<GithubIcon className="mr-2 w-5 h-5" />
3636
GitHub
3737
</Button>
3838
<Button
@@ -41,8 +41,8 @@ const AboutTab: React.FC = () => {
4141
target="_blank"
4242
rel="noopener noreferrer"
4343
variant="flat"
44-
className="w-full justify-start">
45-
<Book className="w-5 h-5 mr-2" />
44+
className="justify-start w-full">
45+
<Book className="mr-2 w-5 h-5" />
4646
{chrome.i18n.getMessage('optionsViewDocs')}
4747
</Button>
4848
<Button
@@ -51,16 +51,16 @@ const AboutTab: React.FC = () => {
5151
target="_blank"
5252
rel="noopener noreferrer"
5353
variant="flat"
54-
className="w-full justify-start">
55-
<Globe className="w-5 h-5 mr-2" />
54+
className="justify-start w-full">
55+
<Globe className="mr-2 w-5 h-5" />
5656
{chrome.i18n.getMessage('options2SOMErenHomepage')}
5757
</Button>
5858
<Button
5959
as={Link}
6060
href="mailto:support@leaper.one"
6161
variant="flat"
62-
className="w-full justify-start">
63-
<Mail className="w-5 h-5 mr-2" />
62+
className="justify-start w-full">
63+
<Mail className="mr-2 w-5 h-5" />
6464
{chrome.i18n.getMessage('optionsFeedbackEmail')}
6565
</Button>
6666
</div>

src/components/Sync/ArticleTab.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useRef, useEffect } from 'react';
22
import { Card, Button, Image, Input, Textarea, CardHeader, CardBody, Progress, CardFooter } from '@heroui/react';
3-
import { ImagePlusIcon, XIcon, DownloadIcon } from 'lucide-react';
3+
import { ImagePlusIcon, XIcon, DownloadIcon, Eraser } from 'lucide-react';
44
import type { FileData, SyncData } from '~sync/common';
55
import PlatformCheckbox from './PlatformCheckbox';
66
import { getPlatformInfos } from '~sync/common';
@@ -39,9 +39,9 @@ const ArticleTab: React.FC<ArticleTabProps> = ({ funcPublish, funcScraper }) =>
3939
codeBlockStyle: 'fenced',
4040
});
4141
const storage = new Storage({
42-
area: "local" // 明确指定使用 localStorage
42+
area: 'local', // 明确指定使用 localStorage
4343
});
44-
44+
4545
useEffect(() => {
4646
if (process.env.NODE_ENV === 'development') {
4747
setTitle('开发环境标题');
@@ -50,11 +50,11 @@ const ArticleTab: React.FC<ArticleTabProps> = ({ funcPublish, funcScraper }) =>
5050
}, []);
5151

5252
const handlePlatformChange = async (platform: string, isSelected: boolean) => {
53-
const newSelectedPlatforms = isSelected
54-
? [...selectedPlatforms, platform]
55-
: selectedPlatforms.filter((p) => p !== platform);
56-
setSelectedPlatforms(newSelectedPlatforms);
57-
await storage.set('articlePlatforms', newSelectedPlatforms);
53+
const newSelectedPlatforms = isSelected
54+
? [...selectedPlatforms, platform]
55+
: selectedPlatforms.filter((p) => p !== platform);
56+
setSelectedPlatforms(newSelectedPlatforms);
57+
await storage.set('articlePlatforms', newSelectedPlatforms);
5858
};
5959

6060
const clearSelectedPlatforms = async () => {
@@ -63,8 +63,8 @@ const ArticleTab: React.FC<ArticleTabProps> = ({ funcPublish, funcScraper }) =>
6363
};
6464

6565
const loadPlatforms = async () => {
66-
const platforms = await storage.get<string[]>("articlePlatforms");
67-
setSelectedPlatforms(platforms as string[] || []);
66+
const platforms = await storage.get<string[]>('articlePlatforms');
67+
setSelectedPlatforms((platforms as string[]) || []);
6868
};
6969
loadPlatforms();
7070

@@ -395,18 +395,19 @@ const ArticleTab: React.FC<ArticleTabProps> = ({ funcPublish, funcScraper }) =>
395395
<p className="text-sm font-medium">{chrome.i18n.getMessage('optionsSelectPublishPlatforms')}</p>
396396
</div>
397397
{selectedPlatforms.length > 0 && (
398-
<Button
399-
size="sm"
400-
variant="light"
401-
color="danger"
398+
<Button
399+
isIconOnly
400+
size="sm"
401+
variant="light"
402+
color="danger"
402403
onPress={clearSelectedPlatforms}
403-
className="text-xs"
404-
>
405-
{chrome.i18n.getMessage('optionsClearPlatforms') || chrome.i18n.getMessage('optionsClearAll') || '清空平台'}
404+
title="清空平台"
405+
className="hover:bg-danger-100">
406+
<Eraser className="size-4" />
406407
</Button>
407408
)}
408409
</div>
409-
<div className="grid grid-cols-2 gap-3">
410+
<div className="grid grid-cols-2 xs:grid-cols-3 sm:grid-cols-4 gap-1">
410411
{getPlatformInfos('ARTICLE').map((platform) => (
411412
<PlatformCheckbox
412413
key={platform.name}

0 commit comments

Comments
 (0)