2026-07-20 10:50:42 +08:00
|
|
|
import { useState } from 'react'
|
2026-07-20 13:02:26 +08:00
|
|
|
import { ConfigProvider, Message, Spin } from '@arco-design/web-react'
|
2026-07-20 10:50:42 +08:00
|
|
|
import '@arco-design/web-react/dist/css/arco.css'
|
|
|
|
|
import '../App.css'
|
2026-07-20 13:02:26 +08:00
|
|
|
import { login, setApiBaseUrl, type ApiSession } from '../api/client'
|
|
|
|
|
import { mapWorkspace } from '../api/mappers'
|
2026-07-20 21:45:36 +08:00
|
|
|
import {
|
|
|
|
|
createCronPlan,
|
|
|
|
|
createProject,
|
2026-07-21 00:46:09 +08:00
|
|
|
createProjectTag,
|
2026-07-20 21:45:36 +08:00
|
|
|
createTask,
|
|
|
|
|
fetchProjectWorkspace,
|
|
|
|
|
fetchProjects,
|
2026-07-21 00:46:09 +08:00
|
|
|
updateTask,
|
2026-07-20 21:45:36 +08:00
|
|
|
uploadSource,
|
|
|
|
|
} from '../api/projects'
|
2026-07-20 10:50:42 +08:00
|
|
|
import { LoginPage } from '../pages/login'
|
2026-07-20 21:45:36 +08:00
|
|
|
import { ProjectActionModals, type CronDraft, type ProjectActionModal, type ProjectDraft, type SourceDraft, type TaskDraft } from '../pages/projects/project-action-modals'
|
2026-07-21 00:46:09 +08:00
|
|
|
import type { ProjectSettingsUpdate } from '../pages/projects/project-sidebar'
|
2026-07-20 18:25:04 +08:00
|
|
|
import { ProjectPage } from '../pages/workspace-home'
|
2026-07-21 00:46:09 +08:00
|
|
|
import type { WorkspaceTaskUpdate } from '../pages/workspace-body'
|
2026-07-20 18:25:04 +08:00
|
|
|
import type { ChannelKey, Project, ProjectWorkspace, Screen, Theme, WorkbenchView } from '../pages/projects/project-types'
|
2026-07-20 10:50:42 +08:00
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
const [screen, setScreen] = useState<Screen>('login')
|
|
|
|
|
const [theme, setTheme] = useState<Theme>('light')
|
2026-07-20 11:40:45 +08:00
|
|
|
const [activeView, setActiveView] = useState<WorkbenchView>('workspace')
|
2026-07-20 13:02:26 +08:00
|
|
|
const [session, setSession] = useState<ApiSession | null>(null)
|
|
|
|
|
const [workspaces, setWorkspaces] = useState<ProjectWorkspace[]>([])
|
|
|
|
|
const [activeProjectID, setActiveProjectID] = useState<string>('')
|
2026-07-20 10:50:42 +08:00
|
|
|
const [activeChannel, setActiveChannel] = useState<ChannelKey>('overview')
|
2026-07-20 18:25:04 +08:00
|
|
|
const [activeTaskID, setActiveTaskID] = useState<string | null>(null)
|
2026-07-21 00:46:09 +08:00
|
|
|
const [, setSelectedItem] = useState('探索采集')
|
2026-07-20 13:02:26 +08:00
|
|
|
const [loading, setLoading] = useState(false)
|
2026-07-20 21:45:36 +08:00
|
|
|
const [actionLoading, setActionLoading] = useState(false)
|
|
|
|
|
const [activeModal, setActiveModal] = useState<ProjectActionModal>(null)
|
2026-07-20 10:50:42 +08:00
|
|
|
|
|
|
|
|
const dark = theme === 'dark'
|
2026-07-20 13:02:26 +08:00
|
|
|
const activeWorkspace = workspaces.find((workspace) => workspace.project.id === activeProjectID) ?? workspaces[0]
|
2026-07-21 00:46:09 +08:00
|
|
|
const activeTagOptions = activeWorkspace?.tags.filter((tag) => tag !== 'all' && tag !== '全部') ?? []
|
2026-07-20 13:02:26 +08:00
|
|
|
|
2026-07-20 21:45:36 +08:00
|
|
|
async function loadWorkspaces(nextSession: ApiSession, preferredProjectID = activeProjectID) {
|
|
|
|
|
const backendProjects = await fetchProjects(nextSession)
|
|
|
|
|
const backendWorkspaces = await Promise.all(
|
2026-07-21 16:29:42 +08:00
|
|
|
backendProjects.map((project, index) => fetchProjectWorkspace(nextSession, project.id).then((workspace) => mapWorkspace(workspace, index))),
|
2026-07-20 21:45:36 +08:00
|
|
|
)
|
|
|
|
|
setWorkspaces(backendWorkspaces)
|
|
|
|
|
const nextProjectID = backendWorkspaces.find((workspace) => workspace.project.id === preferredProjectID)?.project.id ?? backendWorkspaces[0]?.project.id ?? ''
|
|
|
|
|
setActiveProjectID(nextProjectID)
|
|
|
|
|
return backendWorkspaces
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 13:02:26 +08:00
|
|
|
async function handleLogin(input: { server: string; email: string; password: string }) {
|
|
|
|
|
setLoading(true)
|
|
|
|
|
try {
|
|
|
|
|
setApiBaseUrl(input.server)
|
|
|
|
|
const nextSession = await login(input.email, input.password)
|
2026-07-20 21:45:36 +08:00
|
|
|
const backendWorkspaces = await loadWorkspaces(nextSession, '')
|
2026-07-20 13:02:26 +08:00
|
|
|
setSession(nextSession)
|
|
|
|
|
setActiveProjectID(backendWorkspaces[0]?.project.id ?? '')
|
|
|
|
|
setActiveChannel('overview')
|
|
|
|
|
setActiveView('workspace')
|
|
|
|
|
setScreen('workbench')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
Message.error(error instanceof Error ? error.message : '登录失败')
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-20 10:50:42 +08:00
|
|
|
|
2026-07-20 21:45:36 +08:00
|
|
|
async function refreshAfterAction(nextProjectID = activeProjectID) {
|
|
|
|
|
if (!session) return
|
|
|
|
|
await loadWorkspaces(session, nextProjectID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function runAction(action: () => Promise<void>, success: string) {
|
|
|
|
|
setActionLoading(true)
|
|
|
|
|
try {
|
|
|
|
|
await action()
|
|
|
|
|
setActiveModal(null)
|
|
|
|
|
Message.success(success)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
Message.error(error instanceof Error ? error.message : '操作失败')
|
|
|
|
|
} finally {
|
|
|
|
|
setActionLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireSession() {
|
|
|
|
|
if (!session) throw new Error('未登录')
|
|
|
|
|
return session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireActiveProject() {
|
|
|
|
|
if (!activeWorkspace?.project.id) throw new Error('未选择项目')
|
|
|
|
|
return activeWorkspace.project.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeOptionalTime(value: string) {
|
|
|
|
|
const trimmed = value.trim()
|
|
|
|
|
return trimmed === '' ? undefined : trimmed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCreateProject(draft: ProjectDraft) {
|
|
|
|
|
if (!draft.name.trim()) {
|
|
|
|
|
Message.warning('请输入项目名称')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
void runAction(async () => {
|
2026-07-21 00:46:09 +08:00
|
|
|
const created = await createProject(requireSession(), {
|
|
|
|
|
name: draft.name.trim(),
|
|
|
|
|
identifier: draft.identifier.trim(),
|
|
|
|
|
icon: draft.icon.trim(),
|
|
|
|
|
background: draft.background.trim(),
|
|
|
|
|
description: draft.description.trim(),
|
|
|
|
|
})
|
2026-07-21 16:29:42 +08:00
|
|
|
const nextProjectID = created.id
|
2026-07-20 21:45:36 +08:00
|
|
|
await refreshAfterAction(nextProjectID)
|
|
|
|
|
if (nextProjectID) {
|
|
|
|
|
setActiveProjectID(nextProjectID)
|
|
|
|
|
setActiveView('project')
|
|
|
|
|
setActiveChannel('overview')
|
|
|
|
|
}
|
|
|
|
|
}, '项目已创建')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCreateTask(draft: TaskDraft) {
|
|
|
|
|
if (!draft.title.trim()) {
|
|
|
|
|
Message.warning('请输入任务标题')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
void runAction(async () => {
|
|
|
|
|
await createTask(requireSession(), requireActiveProject(), {
|
|
|
|
|
title: draft.title.trim(),
|
|
|
|
|
description: draft.description.trim(),
|
|
|
|
|
status: 'open',
|
2026-07-21 00:46:09 +08:00
|
|
|
tag: draft.tag.trim(),
|
2026-07-20 21:45:36 +08:00
|
|
|
})
|
|
|
|
|
await refreshAfterAction()
|
|
|
|
|
setActiveChannel('tasks')
|
|
|
|
|
}, '任务已创建')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleUploadSource(draft: SourceDraft) {
|
|
|
|
|
if (!draft.file) {
|
|
|
|
|
Message.warning('请选择文件')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const file = draft.file
|
|
|
|
|
void runAction(async () => {
|
|
|
|
|
await uploadSource(requireSession(), requireActiveProject(), {
|
|
|
|
|
title: draft.title.trim(),
|
|
|
|
|
file,
|
|
|
|
|
})
|
|
|
|
|
await refreshAfterAction()
|
|
|
|
|
setActiveChannel('notes')
|
|
|
|
|
}, '文件已上传')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCreateCronPlan(draft: CronDraft) {
|
|
|
|
|
if (!draft.title.trim()) {
|
|
|
|
|
Message.warning('请输入计划名称')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!draft.schedule.trim()) {
|
|
|
|
|
Message.warning('请输入 Cron 表达式')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
void runAction(async () => {
|
|
|
|
|
await createCronPlan(requireSession(), requireActiveProject(), {
|
|
|
|
|
title: draft.title.trim(),
|
|
|
|
|
schedule: draft.schedule.trim(),
|
|
|
|
|
enabled: draft.enabled,
|
|
|
|
|
nextRunAt: normalizeOptionalTime(draft.nextRunAt),
|
|
|
|
|
})
|
|
|
|
|
await refreshAfterAction()
|
|
|
|
|
setActiveChannel('cron')
|
|
|
|
|
}, '计划任务已创建')
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 00:46:09 +08:00
|
|
|
function handleCreateProjectTag(name: string) {
|
|
|
|
|
const trimmedName = name.trim()
|
|
|
|
|
if (!trimmedName) {
|
|
|
|
|
Message.warning('请输入标签名称')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
void runAction(async () => {
|
|
|
|
|
await createProjectTag(requireSession(), requireActiveProject(), { name: trimmedName })
|
|
|
|
|
await refreshAfterAction()
|
|
|
|
|
}, '标签已创建')
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 18:25:04 +08:00
|
|
|
function openTask(project: Project, taskID: string) {
|
|
|
|
|
setActiveProjectID(project.id)
|
|
|
|
|
setActiveView('project')
|
|
|
|
|
setActiveChannel('tasks')
|
|
|
|
|
setActiveTaskID(taskID)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 00:46:09 +08:00
|
|
|
function handleUpdateWorkspaceTask(update: WorkspaceTaskUpdate) {
|
|
|
|
|
void runAction(async () => {
|
|
|
|
|
await updateTask(requireSession(), update.originalProjectId, update.taskId, {
|
|
|
|
|
title: update.title,
|
|
|
|
|
description: update.summary,
|
|
|
|
|
completed: update.completed,
|
2026-07-21 16:29:42 +08:00
|
|
|
nextProjectId: update.nextProjectId,
|
2026-07-21 00:46:09 +08:00
|
|
|
tag: update.tag,
|
|
|
|
|
})
|
|
|
|
|
await refreshAfterAction(update.nextProjectId)
|
|
|
|
|
}, '任务已更新')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleUpdateProject(update: ProjectSettingsUpdate) {
|
|
|
|
|
setWorkspaces((current) =>
|
|
|
|
|
current.map((workspace) => {
|
|
|
|
|
if (workspace.project.id !== update.projectId) return workspace
|
|
|
|
|
const nextProject = {
|
|
|
|
|
...workspace.project,
|
|
|
|
|
name: update.name,
|
|
|
|
|
identifier: update.identifier,
|
|
|
|
|
icon: update.icon,
|
|
|
|
|
background: update.background,
|
|
|
|
|
description: update.description,
|
|
|
|
|
short: projectIconLabel(update.icon, update.identifier || update.name.slice(0, 2)),
|
|
|
|
|
color: projectColor(update.background, workspace.project.color),
|
|
|
|
|
}
|
|
|
|
|
return { ...workspace, project: nextProject }
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 10:50:42 +08:00
|
|
|
return (
|
|
|
|
|
<ConfigProvider>
|
|
|
|
|
<main className={dark ? 'app theme-dark' : 'app'}>
|
|
|
|
|
{screen === 'login' ? (
|
2026-07-20 13:02:26 +08:00
|
|
|
<Spin loading={loading} style={{ width: '100%' }}>
|
|
|
|
|
<LoginPage onLogin={handleLogin} />
|
|
|
|
|
</Spin>
|
|
|
|
|
) : activeWorkspace && session ? (
|
2026-07-20 10:50:42 +08:00
|
|
|
<ProjectPage
|
2026-07-20 11:40:45 +08:00
|
|
|
activeView={activeView}
|
2026-07-20 13:02:26 +08:00
|
|
|
activeWorkspace={activeWorkspace}
|
|
|
|
|
workspaces={workspaces}
|
2026-07-20 10:50:42 +08:00
|
|
|
activeChannel={activeChannel}
|
2026-07-20 18:25:04 +08:00
|
|
|
activeTaskID={activeTaskID}
|
2026-07-20 10:50:42 +08:00
|
|
|
theme={theme}
|
2026-07-20 11:40:45 +08:00
|
|
|
onSelectWorkspace={() => setActiveView('workspace')}
|
2026-07-21 00:46:09 +08:00
|
|
|
onSelectWorkspaceExplore={() => setActiveView('workspace-explore')}
|
2026-07-20 11:40:45 +08:00
|
|
|
onSelectProject={(project) => {
|
2026-07-20 13:02:26 +08:00
|
|
|
setActiveProjectID(project.id)
|
2026-07-20 11:40:45 +08:00
|
|
|
setActiveView('project')
|
2026-07-20 13:02:26 +08:00
|
|
|
setActiveChannel('overview')
|
2026-07-20 18:25:04 +08:00
|
|
|
setActiveTaskID(null)
|
|
|
|
|
}}
|
|
|
|
|
onSelectChannel={(channel) => {
|
|
|
|
|
setActiveChannel(channel)
|
|
|
|
|
setActiveTaskID(null)
|
2026-07-20 11:40:45 +08:00
|
|
|
}}
|
2026-07-20 10:50:42 +08:00
|
|
|
onSelectItem={setSelectedItem}
|
2026-07-20 18:25:04 +08:00
|
|
|
onOpenTask={openTask}
|
|
|
|
|
onCloseTask={() => setActiveTaskID(null)}
|
2026-07-20 10:50:42 +08:00
|
|
|
onToggleTheme={() => setTheme(dark ? 'light' : 'dark')}
|
2026-07-20 21:45:36 +08:00
|
|
|
onCreateProject={() => setActiveModal('project')}
|
|
|
|
|
onCreateTask={() => setActiveModal('task')}
|
|
|
|
|
onUploadSource={() => setActiveModal('source')}
|
|
|
|
|
onCreateCronPlan={() => setActiveModal('cron')}
|
2026-07-21 00:46:09 +08:00
|
|
|
onUpdateWorkspaceTask={handleUpdateWorkspaceTask}
|
|
|
|
|
onUpdateProject={handleUpdateProject}
|
|
|
|
|
onCreateProjectTag={handleCreateProjectTag}
|
2026-07-20 10:50:42 +08:00
|
|
|
/>
|
2026-07-20 13:02:26 +08:00
|
|
|
) : (
|
|
|
|
|
<Spin loading />
|
2026-07-20 10:50:42 +08:00
|
|
|
)}
|
2026-07-20 21:45:36 +08:00
|
|
|
<ProjectActionModals
|
|
|
|
|
activeModal={activeModal}
|
|
|
|
|
loading={actionLoading}
|
|
|
|
|
onClose={() => setActiveModal(null)}
|
|
|
|
|
onCreateProject={handleCreateProject}
|
|
|
|
|
onCreateTask={handleCreateTask}
|
|
|
|
|
onUploadSource={handleUploadSource}
|
|
|
|
|
onCreateCronPlan={handleCreateCronPlan}
|
2026-07-21 00:46:09 +08:00
|
|
|
tagOptions={activeTagOptions}
|
2026-07-20 21:45:36 +08:00
|
|
|
/>
|
2026-07-20 10:50:42 +08:00
|
|
|
</main>
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 00:46:09 +08:00
|
|
|
function projectIconLabel(icon: string, fallback: string) {
|
|
|
|
|
const trimmed = icon.trim()
|
|
|
|
|
if (!trimmed) return fallback
|
|
|
|
|
const chars = Array.from(trimmed)
|
|
|
|
|
if (chars.length <= 2) return trimmed
|
|
|
|
|
return chars.slice(0, 2).join('').toUpperCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function projectColor(background: string, fallback: string) {
|
|
|
|
|
const trimmed = background.trim()
|
|
|
|
|
if (trimmed.startsWith('#') || trimmed.startsWith('rgb') || trimmed.startsWith('hsl')) return trimmed
|
|
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 10:50:42 +08:00
|
|
|
export default App
|