zen-gitsync 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "一个 git 自动查看差异并提交的工具",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -11,6 +11,7 @@
11
11
  "dependencies": {
12
12
  "@gitgraph/js": "^1.4.0",
13
13
  "element-plus": "^2.9.9",
14
+ "pinia": "^3.0.2",
14
15
  "socket.io-client": "^4.8.1",
15
16
  "vue": "^3.5.13"
16
17
  },
@@ -6,16 +6,19 @@ import LogList from './components/LogList.vue'
6
6
  import { ElMessage } from 'element-plus'
7
7
  import { Plus } from '@element-plus/icons-vue'
8
8
  import logo from './assets/logo.svg'
9
+ import { useGitStore } from './stores/gitStore'
9
10
 
10
11
  const configInfo = ref('')
11
12
  // 添加组件实例类型
12
13
  const logListRef = ref<InstanceType<typeof LogList> | null>(null)
13
14
  const gitStatusRef = ref<InstanceType<typeof GitStatus> | null>(null)
14
- const currentBranch = ref('') // 添加当前分支状态变量
15
- const userName = ref('') // 添加用户名变量
16
- const userEmail = ref('') // 添加用户邮箱变量
17
- const allBranches = ref<string[]>([]) // 添加所有分支列表
18
- const isChangingBranch = ref(false) // 添加分支切换状态
15
+
16
+ // 使用Git Store
17
+ const gitStore = useGitStore()
18
+
19
+ // 添加初始化完成状态
20
+ const initCompleted = ref(false)
21
+ const currentDirectory = ref('')
19
22
 
20
23
  // 加载配置
21
24
  async function loadConfig() {
@@ -28,211 +31,139 @@ async function loadConfig() {
28
31
  }
29
32
  }
30
33
 
31
- // 获取当前分支
32
- async function getCurrentBranch() {
33
- try {
34
- const response = await fetch('/api/branch')
35
- const data = await response.json()
36
- if (data.branch) {
37
- currentBranch.value = data.branch
38
- }
39
- } catch (error) {
40
- console.error('获取分支信息失败:', error)
41
- }
42
- }
43
-
44
- // 获取所有分支
45
- async function getAllBranches() {
34
+ // 加载当前目录信息
35
+ async function loadCurrentDirectory() {
46
36
  try {
47
- const response = await fetch('/api/branches')
48
- const data = await response.json()
49
- if (data.branches && Array.isArray(data.branches)) {
50
- allBranches.value = data.branches
51
- }
37
+ const responseDir = await fetch('/api/current_directory')
38
+ const dirData = await responseDir.json()
39
+ currentDirectory.value = dirData.directory || '未知目录'
40
+ return dirData
52
41
  } catch (error) {
53
- console.error('获取所有分支信息失败:', error)
42
+ console.error('获取当前目录失败:', error)
43
+ return { directory: '未知目录', isGitRepo: false }
54
44
  }
55
45
  }
56
46
 
57
- // 切换分支
58
- async function changeBranch(branch: string) {
59
- console.log('切换到分支:', branch)
47
+ onMounted(async () => {
48
+ console.log('---------- 页面初始化开始 ----------')
60
49
 
61
50
  try {
62
- isChangingBranch.value = true
63
- const response = await fetch('/api/checkout', {
64
- method: 'POST',
65
- headers: {
66
- 'Content-Type': 'application/json'
67
- },
68
- body: JSON.stringify({ branch })
69
- })
51
+ // 并行加载配置和目录信息
52
+ const [_, dirData] = await Promise.all([
53
+ loadConfig(),
54
+ loadCurrentDirectory()
55
+ ])
70
56
 
71
- const result = await response.json()
72
- if (result.success) {
73
- ElMessage({
74
- message: `已切换到分支: ${branch}`,
75
- type: 'success'
76
- })
77
-
78
- // 刷新状态
79
- getCurrentBranch()
80
-
81
- // 刷新Git状态
82
- if (gitStatusRef.value) {
83
- gitStatusRef.value.refreshStatus()
84
- }
57
+ // 设置Git仓库状态
58
+ gitStore.isGitRepo = dirData.isGitRepo === true
59
+ gitStore.lastCheckedTime = Date.now()
60
+
61
+ // 只有是Git仓库的情况下才加载Git相关信息
62
+ if (gitStore.isGitRepo) {
63
+ // 只获取分支和用户信息,不重复获取日志
64
+ await Promise.all([
65
+ gitStore.getCurrentBranch(),
66
+ gitStore.getAllBranches(),
67
+ gitStore.getUserInfo()
68
+ ])
85
69
 
86
- // 刷新提交历史
87
- if (logListRef.value) {
88
- logListRef.value.refreshLog()
89
- }
70
+ // 日志信息通过LogList组件直接加载即可,避免重复调用
71
+ // 移除 gitLogStore.fetchLog() 调用
90
72
  } else {
91
- ElMessage({
92
- message: `切换分支失败: ${result.error}`,
93
- type: 'error'
94
- })
95
- // 恢复选择框为当前分支
96
- currentBranch.value = currentBranch.value
73
+ ElMessage.warning('当前目录不是Git仓库,部分功能将不可用')
97
74
  }
98
75
  } catch (error) {
99
- ElMessage({
100
- message: `切换分支失败: ${(error as Error).message}`,
101
- type: 'error'
102
- })
103
- // 恢复选择框为当前分支
104
- currentBranch.value = currentBranch.value
76
+ console.error('初始化失败:', error)
105
77
  } finally {
106
- isChangingBranch.value = false
107
- }
108
- }
109
-
110
- // 获取Git用户信息
111
- async function getUserInfo() {
112
- try {
113
- const response = await fetch('/api/user-info')
114
- const data = await response.json()
115
- if (data.name && data.email) {
116
- userName.value = data.name
117
- userEmail.value = data.email
118
- }
119
- } catch (error) {
120
- console.error('获取用户信息失败:', error)
78
+ // 标记初始化完成
79
+ initCompleted.value = true
80
+ console.log('---------- 页面初始化完成 ----------')
121
81
  }
122
- }
123
-
124
- onMounted(() => {
125
- loadConfig()
126
- getCurrentBranch() // 初始加载分支信息
127
- getAllBranches() // 加载所有分支
128
- getUserInfo() // 初始加载用户信息
129
82
  })
130
83
 
131
84
  // 处理提交成功事件
132
85
  function handleCommitSuccess() {
133
- // 刷新提交历史
134
- if (logListRef.value) {
135
- logListRef.value.refreshLog()
136
- }
86
+ // 不再调用gitLogStore.fetchLog(),改用更新LogList组件
137
87
 
138
88
  // 刷新Git状态
139
89
  if (gitStatusRef.value) {
140
90
  gitStatusRef.value.refreshStatus()
141
91
  }
92
+
93
+ // 直接刷新提交历史
94
+ if (logListRef.value) {
95
+ logListRef.value.refreshLog()
96
+ }
142
97
  }
143
98
 
144
99
  // 处理推送成功事件
145
100
  function handlePushSuccess() {
146
- // 刷新提交历史
101
+ // 不再调用gitLogStore.fetchLog(),改用更新LogList组件
102
+ gitStore.getCurrentBranch()
103
+
104
+ // 直接刷新提交历史
147
105
  if (logListRef.value) {
148
106
  logListRef.value.refreshLog()
149
107
  }
150
-
108
+ }
109
+
110
+ // 处理状态更新事件
111
+ function handleStatusUpdate() {
151
112
  // 刷新Git状态
152
113
  if (gitStatusRef.value) {
153
114
  gitStatusRef.value.refreshStatus()
154
115
  }
155
-
156
- // 刷新分支信息
157
- getCurrentBranch()
158
116
  }
159
117
 
160
118
  const createBranchDialogVisible = ref(false)
161
119
  const newBranchName = ref('')
162
120
  const selectedBaseBranch = ref('')
163
- const isCreatingBranch = ref(false)
164
121
 
165
122
  // 创建新分支
166
123
  async function createNewBranch() {
167
- if (!newBranchName.value.trim()) {
168
- ElMessage({
169
- message: '分支名称不能为空',
170
- type: 'warning'
171
- })
172
- return
173
- }
124
+ const success = await gitStore.createBranch(newBranchName.value, selectedBaseBranch.value)
174
125
 
175
- try {
176
- isCreatingBranch.value = true
126
+ if (success) {
127
+ // 关闭对话框
128
+ createBranchDialogVisible.value = false
177
129
 
178
- const response = await fetch('/api/create-branch', {
179
- method: 'POST',
180
- headers: {
181
- 'Content-Type': 'application/json'
182
- },
183
- body: JSON.stringify({
184
- newBranchName: newBranchName.value,
185
- baseBranch: selectedBaseBranch.value || currentBranch.value
186
- })
187
- })
130
+ // 重置表单
131
+ newBranchName.value = ''
188
132
 
189
- const result = await response.json()
190
- if (result.success) {
191
- ElMessage({
192
- message: `已创建并切换到分支: ${newBranchName.value}`,
193
- type: 'success'
194
- })
195
-
196
- // 关闭对话框
197
- createBranchDialogVisible.value = false
198
-
199
- // 重置表单
200
- newBranchName.value = ''
201
-
202
- // 刷新状态
203
- getCurrentBranch()
204
- getAllBranches()
205
-
206
- // 刷新Git状态
207
- if (gitStatusRef.value) {
208
- gitStatusRef.value.refreshStatus()
209
- }
210
-
211
- // 刷新提交历史
212
- if (logListRef.value) {
213
- logListRef.value.refreshLog()
214
- }
215
- } else {
216
- ElMessage({
217
- message: `创建分支失败: ${result.error}`,
218
- type: 'error'
219
- })
133
+ // 刷新Git状态
134
+ if (gitStatusRef.value) {
135
+ gitStatusRef.value.refreshStatus()
136
+ }
137
+
138
+ // 刷新提交历史
139
+ if (logListRef.value) {
140
+ logListRef.value.refreshLog()
220
141
  }
221
- } catch (error) {
222
- ElMessage({
223
- message: `创建分支失败: ${(error as Error).message}`,
224
- type: 'error'
225
- })
226
- } finally {
227
- isCreatingBranch.value = false
228
142
  }
229
143
  }
230
144
 
231
145
  // 打开创建分支对话框
232
146
  function openCreateBranchDialog() {
233
- selectedBaseBranch.value = currentBranch.value
147
+ selectedBaseBranch.value = gitStore.currentBranch
234
148
  createBranchDialogVisible.value = true
235
149
  }
150
+
151
+ // 切换分支
152
+ async function handleBranchChange(branch: string) {
153
+ const success = await gitStore.changeBranch(branch)
154
+
155
+ if (success) {
156
+ // 刷新Git状态
157
+ if (gitStatusRef.value) {
158
+ gitStatusRef.value.refreshStatus()
159
+ }
160
+
161
+ // 刷新提交历史
162
+ if (logListRef.value) {
163
+ logListRef.value.refreshLog()
164
+ }
165
+ }
166
+ }
236
167
  </script>
237
168
 
238
169
  <template>
@@ -242,30 +173,54 @@ function openCreateBranchDialog() {
242
173
  <h1>Zen GitSync UI</h1>
243
174
  </div>
244
175
  <div class="header-info">
245
- <div id="user-info" v-if="userName && userEmail">
176
+ <div id="user-info" v-if="gitStore.userName && gitStore.userEmail">
246
177
  <span class="user-label">用户:</span>
247
- <span class="user-name">{{ userName }}</span>
248
- <span class="user-email">&lt;{{ userEmail }}&gt;</span>
178
+ <span class="user-name">{{ gitStore.userName }}</span>
179
+ <span class="user-email">&lt;{{ gitStore.userEmail }}&gt;</span>
249
180
  </div>
250
181
  <!-- <div id="config-info">{{ configInfo }}</div> -->
251
182
  </div>
252
183
  </header>
253
184
 
254
185
  <div class="container">
255
- <div class="layout-container">
186
+ <div v-if="!initCompleted" class="loading-container">
187
+ <el-card class="loading-card">
188
+ <div class="loading-spinner">
189
+ <el-icon class="is-loading"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M512 64a32 32 0 0 1 32 32v192a32 32 0 0 1-64 0V96a32 32 0 0 1 32-32zm0 640a32 32 0 0 1 32 32v192a32 32 0 1 1-64 0V736a32 32 0 0 1 32-32zm448-192a32 32 0 0 1-32 32H736a32 32 0 1 1 0-64h192a32 32 0 0 1 32 32zm-640 0a32 32 0 0 1-32 32H96a32 32 0 0 1 0-64h192a32 32 0 0 1 32 32zM195.2 195.2a32 32 0 0 1 45.248 0L376.32 331.008a32 32 0 0 1-45.248 45.248L195.2 240.448a32 32 0 0 1 0-45.248zm452.544 452.544a32 32 0 0 1 45.248 0L828.8 783.552a32 32 0 0 1-45.248 45.248L647.744 692.992a32 32 0 0 1 0-45.248zM828.8 195.264a32 32 0 0 1 0 45.184L692.992 376.32a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0zm-452.544 452.48a32 32 0 0 1 0 45.248L240.448 828.8a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0z"></path></svg></el-icon>
190
+ </div>
191
+ <div class="loading-text">加载中...</div>
192
+ </el-card>
193
+ </div>
194
+
195
+ <div v-else class="layout-container">
256
196
  <!-- 左侧Git状态 -->
257
197
  <div class="left-panel">
258
- <GitStatus ref="gitStatusRef" />
198
+ <GitStatus
199
+ ref="gitStatusRef"
200
+ :initial-directory="currentDirectory"
201
+ />
259
202
  </div>
260
203
 
261
204
  <!-- 右侧提交表单和历史 -->
262
- <div class="right-panel">
205
+ <div class="right-panel" v-if="gitStore.isGitRepo">
263
206
  <CommitForm
264
207
  @commit-success="handleCommitSuccess"
265
- @push-success="handlePushSuccess"
208
+ @push-success="handlePushSuccess"
209
+ @status-update="handleStatusUpdate"
266
210
  />
267
211
  <LogList ref="logListRef" />
268
212
  </div>
213
+ <div class="right-panel" v-else>
214
+ <div class="card">
215
+ <h2>Git仓库初始化</h2>
216
+ <p>当前目录不是Git仓库,请先初始化Git仓库或切换到Git仓库目录。</p>
217
+ <!-- 实用提示 -->
218
+ <div class="tips">
219
+ <h3>可以使用以下命令初始化仓库:</h3>
220
+ <div class="code-block">git init</div>
221
+ </div>
222
+ </div>
223
+ </div>
269
224
 
270
225
  <!-- 创建分支对话框 -->
271
226
  <el-dialog
@@ -281,7 +236,7 @@ function openCreateBranchDialog() {
281
236
  <el-form-item label="基于分支">
282
237
  <el-select v-model="selectedBaseBranch" placeholder="选择基础分支" style="width: 100%;">
283
238
  <el-option
284
- v-for="branch in allBranches"
239
+ v-for="branch in gitStore.allBranches"
285
240
  :key="branch"
286
241
  :label="branch"
287
242
  :value="branch"
@@ -295,7 +250,7 @@ function openCreateBranchDialog() {
295
250
  <el-button
296
251
  type="primary"
297
252
  @click="createNewBranch"
298
- :loading="isCreatingBranch"
253
+ :loading="gitStore.isCreatingBranch"
299
254
  >
300
255
  创建
301
256
  </el-button>
@@ -307,17 +262,17 @@ function openCreateBranchDialog() {
307
262
  </div>
308
263
 
309
264
  <footer class="main-footer">
310
- <div class="branch-info" v-if="currentBranch">
265
+ <div class="branch-info" v-if="gitStore.currentBranch">
311
266
  <span class="branch-label">当前分支:</span>
312
267
  <el-select
313
- v-model="currentBranch"
268
+ v-model="gitStore.currentBranch"
314
269
  size="small"
315
- @change="changeBranch"
316
- :loading="isChangingBranch"
270
+ @change="handleBranchChange"
271
+ :loading="gitStore.isChangingBranch"
317
272
  class="branch-select"
318
273
  >
319
274
  <el-option
320
- v-for="branch in allBranches"
275
+ v-for="branch in gitStore.allBranches"
321
276
  :key="branch"
322
277
  :label="branch"
323
278
  :value="branch"
@@ -503,6 +458,54 @@ h1 {
503
458
  .branch-select :deep(.el-input__suffix) {
504
459
  color: white;
505
460
  }
461
+
462
+ .tips {
463
+ margin-top: 20px;
464
+ padding: 15px;
465
+ background-color: #f5f7fa;
466
+ border-radius: 5px;
467
+ border-left: 4px solid #409eff;
468
+ }
469
+
470
+ .tips h3 {
471
+ margin-top: 0;
472
+ font-size: 16px;
473
+ margin-bottom: 10px;
474
+ }
475
+
476
+ .code-block {
477
+ background-color: #2d2d2d;
478
+ color: #f8f8f2;
479
+ font-family: monospace;
480
+ padding: 10px 15px;
481
+ border-radius: 4px;
482
+ margin-bottom: 10px;
483
+ }
484
+
485
+ /* 添加加载中样式 */
486
+ .loading-container {
487
+ display: flex;
488
+ justify-content: center;
489
+ align-items: center;
490
+ min-height: 400px;
491
+ }
492
+
493
+ .loading-card {
494
+ width: 300px;
495
+ text-align: center;
496
+ padding: 30px;
497
+ }
498
+
499
+ .loading-spinner {
500
+ font-size: 48px;
501
+ margin-bottom: 20px;
502
+ color: #409eff;
503
+ }
504
+
505
+ .loading-text {
506
+ font-size: 18px;
507
+ color: #606266;
508
+ }
506
509
  </style>
507
510
 
508
511
  <style scoped>