Skip to content

Commit

Permalink
feat: added git pull progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 29, 2024
1 parent 42e5cb3 commit 0b1a2ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ export const getRequestBaseUrl = (): string => {
return import.meta.env.VITE_APP_API_BASE_URL || 'http://localhost:8000';
};

export const getRequestBaseUrlWs = (): string => {
if (import.meta.env.VITE_APP_API_BASE_URL.startsWith('http://')) {
return (
import.meta.env.VITE_APP_API_BASE_URL.replace(/https?/, 'ws') ||
'ws://localhost:8000'
);
}
return `ws://${window.location.hostname}${import.meta.env.VITE_APP_API_BASE_URL}`;
};

export const getEmptyResponseWithListData = <
T = any,
>(): ResponseWithListData<T> => {
Expand Down
47 changes: 29 additions & 18 deletions src/views/git/detail/useGitDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TAB_NAME_LOGS,
TAB_NAME_OVERVIEW,
} from '@/constants';
import { getRequestBaseUrlWs } from '@/utils';

// i18n
const t = translate;
Expand Down Expand Up @@ -160,26 +161,36 @@ const useGitDetail = () => {
};

const onPull = async () => {
pullLoading.value = true;
try {
const res = await store.dispatch(`${ns}/pull`, {
id: id.value,
});
if (res.data) {
ElMessage.info(res.data);
const ws = new WebSocket(
getRequestBaseUrlWs() + `/gits/${id.value}/pull/ws`
);
ws.onopen = () => {
pullLoading.value = true;
};
ws.onmessage = event => {
console.debug(event.data);
};
ws.onerror = error => {
pullLoading.value = false;
ElMessage.error(error);
};
ws.onclose = event => {
pullLoading.value = false;
if (event.code === 1000) {
if (event.reason) {
ElMessage.info(event.reason);
} else {
ElMessage.success(t('components.git.common.message.success.pull'));
if (activeTabName.value === TAB_NAME_FILES) {
store.dispatch(`${ns}/listDir`, { id: id.value });
} else if (activeTabName.value === TAB_NAME_LOGS) {
store.dispatch(`${ns}/getLogs`, { id: id.value });
}
}
} else {
ElMessage.success(t('components.git.common.message.success.pull'));
}
if (activeTabName.value === TAB_NAME_FILES) {
await store.dispatch(`${ns}/listDir`, { id: id.value });
} else if (activeTabName.value === TAB_NAME_LOGS) {
await store.dispatch(`${ns}/getLogs`, { id: id.value });
ElMessage.error(event.reason);
}
} catch (e: any) {
ElMessage.error(e.message);
} finally {
pullLoading.value = false;
}
};
};

const onPush = async () => {
Expand Down

0 comments on commit 0b1a2ed

Please sign in to comment.