Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor: cleanup duplicate AddTopicButon components and optimize migration script (#12095)

Innei83e5f3157650
diff --git a/scripts/migrate-spa-navigation.ts b/scripts/migrate-spa-navigation.ts
index aac6d8d9832..440ba839c2a 100644
--- a/scripts/migrate-spa-navigation.ts
+++ b/scripts/migrate-spa-navigation.ts
@@ -10,9 +10,8 @@
*
* @see RFC 147: LOBE-2850 - Phase 3
*/
-
import { readFile, writeFile } from 'node:fs/promises';
-import { dirname, join, relative } from 'node:path';
+import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -24,7 +23,6 @@ const SPA_FILES = [
'src/app/[variants]/(main)/group/_layout/Sidebar/Header/Nav.tsx',
'src/app/[variants]/(main)/group/_layout/Sidebar/Topic/hooks/useTopicNavigation.ts',
'src/app/[variants]/(main)/group/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts',
- 'src/app/[variants]/(main)/chat/_layout/Sidebar/Header/AddTopicButon.tsx',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Header/Nav.tsx',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Topic/hooks/useTopicNavigation.ts',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts',
@@ -44,7 +42,7 @@ async function migrateFile(relativePath: string): Promise<MigrationResult | null
// Check what hooks are being imported from @/libs/next/navigation
const importMatch = content.match(
- /import\s*\{([^}]+)\}\s*from\s*['"]@\/libs\/next\/navigation['"]/
+ /import\s*{([^}]+)}\s*from\s*["']@\/libs\/next\/navigation["']/,
);
if (!importMatch) {
@@ -64,20 +62,31 @@ async function migrateFile(relativePath: string): Promise<MigrationResult | null
const newImports: string[] = [];
for (const hook of importedHooks) {
- if (hook === 'usePathname') {
- newImports.push(`import { usePathname } from '@/app/[variants]/(main)/hooks/usePathname';`);
- changes.push('usePathname -> React Router version');
- } else if (hook === 'useSearchParams') {
- newImports.push(
- `import { useSearchParams } from '@/app/[variants]/(main)/hooks/useSearchParams';`
- );
- changes.push('useSearchParams -> React Router version');
- } else if (hook === 'useRouter') {
- newImports.push(`import { useRouter } from '@/app/[variants]/(main)/hooks/useRouter';`);
- changes.push('useRouter -> React Router version');
- } else {
- // Keep other imports (like notFound, redirect) from next/navigation
- console.log(` ⚠️ Unknown hook "${hook}" - keeping original import`);
+ switch (hook) {
+ case 'usePathname': {
+ newImports.push(`import { usePathname } from '@/app/[variants]/(main)/hooks/usePathname';`);
+ changes.push('usePathname -> React Router version');
+
+ break;
+ }
+ case 'useSearchParams': {
+ newImports.push(
+ `import { useSearchParams } from '@/app/[variants]/(main)/hooks/useSearchParams';`,
+ );
+ changes.push('useSearchParams -> React Router version');
+
+ break;
+ }
+ case 'useRouter': {
+ newImports.push(`import { useRouter } from '@/app/[variants]/(main)/hooks/useRouter';`);
+ changes.push('useRouter -> React Router version');
+
+ break;
+ }
+ default: {
+ // Keep other imports (like notFound, redirect) from next/navigation
+ console.log(` ⚠️ Unknown hook "${hook}" - keeping original import`);
+ }
}
}
@@ -88,8 +97,8 @@ async function migrateFile(relativePath: string): Promise<MigrationResult | null
// Replace the old import with new imports
newContent = newContent.replace(
- /import\s*\{[^}]+\}\s*from\s*['"]@\/libs\/next\/navigation['"];?\n?/,
- newImports.join('\n') + '\n'
+ /import\s*{[^}]+}\s*from\s*["']@\/libs\/next\/navigation["'];?\n?/,
+ newImports.join('\n') + '\n',
);
if (newContent !== content) {
diff --git a/src/app/[variants]/(main)/agent/_layout/Sidebar/Header/AddTopicButon.tsx b/src/app/[variants]/(main)/agent/_layout/Sidebar/Header/AddTopicButon.tsx
deleted file mode 100644
index a01638395d6..00000000000
--- a/src/app/[variants]/(main)/agent/_layout/Sidebar/Header/AddTopicButon.tsx
+++ /dev/null
@@ -1,53 +0,0 @@
-'use client';
-
-import { ActionIcon } from '@lobehub/ui';
-import { MessageSquarePlusIcon } from 'lucide-react';
-import { memo } from 'react';
-import { useTranslation } from 'react-i18next';
-import { useParams } from 'react-router-dom';
-import urlJoin from 'url-join';
-
-import { DESKTOP_HEADER_ICON_SIZE } from '@/const/layoutTokens';
-import { useQueryRoute } from '@/hooks/useQueryRoute';
-import { usePathname } from '@/libs/router/navigation';
-import { useActionSWR } from '@/libs/swr';
-import { useChatStore } from '@/store/chat';
-import { useUserStore } from '@/store/user';
-import { settingsSelectors } from '@/store/user/selectors';
-import { HotkeyEnum } from '@/types/hotkey';
-
-const AddTopicButon = memo(() => {
- const { t } = useTranslation('topic');
- const hotkey = useUserStore(settingsSelectors.getHotkeyById(HotkeyEnum.SaveTopic));
- const params = useParams();
- const agentId = params.aid;
- const pathname = usePathname();
- const isProfileActive = pathname.includes('/profile');
- const router = useQueryRoute();
- const [openNewTopicOrSaveTopic] = useChatStore((s) => [s.openNewTopicOrSaveTopic]);
-
- const { mutate, isValidating } = useActionSWR('openNewTopicOrSaveTopic', openNewTopicOrSaveTopic);
- const handleNewTopic = () => {
- // If in agent sub-route, navigate back to agent chat first
- if (isProfileActive && agentId) {
- router.push(urlJoin('/agent', agentId));
- }
- mutate();
- };
-
- return (
- <ActionIcon
- icon={MessageSquarePlusIcon}
- loading={isValidating}
- onClick={handleNewTopic}
- size={DESKTOP_HEADER_ICON_SIZE}
- title={t('actions.addNewTopic')}
- tooltipProps={{
- hotkey,
- placement: 'bottom',
- }}
- />
- );
-});
-
-export default AddTopicButon;
diff --git a/src/app/[variants]/(main)/group/_layout/Sidebar/Header/AddTopicButon.tsx b/src/app/[variants]/(main)/group/_layout/Sidebar/Header/AddTopicButon.tsx
deleted file mode 100644
index 26f41c502e4..00000000000
--- a/src/app/[variants]/(main)/group/_layout/Sidebar/Header/AddTopicButon.tsx
+++ /dev/null
@@ -1,33 +0,0 @@
-'use client';
-
-import { ActionIcon } from '@lobehub/ui';
-import { MessageSquarePlusIcon } from 'lucide-react';
-import { memo } from 'react';
-import { useTranslation } from 'react-i18next';
-
-import { DESKTOP_HEADER_ICON_SIZE } from '@/const/layoutTokens';
-import { useAgentGroupStore } from '@/store/agentGroup';
-import { useUserStore } from '@/store/user';
-import { settingsSelectors } from '@/store/user/selectors';
-import { HotkeyEnum } from '@/types/hotkey';
-
-const AddTopicButon = memo(() => {
- const { t } = useTranslation('topic');
- const hotkey = useUserStore(settingsSelectors.getHotkeyById(HotkeyEnum.SaveTopic));
- const switchToNewTopic = useAgentGroupStore((s) => s.switchToNewTopic);
-
- return (
- <ActionIcon
- icon={MessageSquarePlusIcon}
- onClick={switchToNewTopic}
- size={DESKTOP_HEADER_ICON_SIZE}
- title={t('actions.addNewTopic')}
- tooltipProps={{
- hotkey,
- placement: 'bottom',
- }}
- />
- );
-});
-
-export default AddTopicButon;