mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Migrate git api to ts
This commit is contained in:
49
app/src/api/git.ts
Normal file
49
app/src/api/git.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { API_BASE_URL } from '@/types/authApi';
|
||||
import { apiCall } from './api';
|
||||
|
||||
/**
|
||||
* pullChanges fetches the latest changes from the remote repository
|
||||
* @param workspaceName - The name of the workspace
|
||||
* @returns {Promise<string>} A promise that resolves to a message indicating the result of the pull operation
|
||||
*/
|
||||
export const pullChanges = async (workspaceName: string): Promise<string> => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/git/pull`,
|
||||
{
|
||||
method: 'POST',
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
if (!('message' in data)) {
|
||||
throw new Error('Invalid pull response received from API');
|
||||
}
|
||||
return data.message;
|
||||
};
|
||||
|
||||
/**
|
||||
* pushChanges pushes the local changes to the remote repository
|
||||
* @param workspaceName - The name of the workspace
|
||||
* @returns {Promise<CommitHash>} A promise that resolves to the commit hash of the pushed changes
|
||||
*/
|
||||
export const commitAndPush = async (
|
||||
workspaceName: string,
|
||||
message: string
|
||||
): Promise<CommitHash> => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||
workspaceName
|
||||
)}/git/commit`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ message }),
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
if (!('commitHash' in data)) {
|
||||
throw new Error('Invalid commit response received from API');
|
||||
}
|
||||
return data.commitHash as CommitHash;
|
||||
};
|
||||
@@ -1,30 +0,0 @@
|
||||
import { API_BASE_URL } from '../utils/constants';
|
||||
import { apiCall } from './auth';
|
||||
|
||||
export const pullChanges = async (workspaceName) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/workspaces/${workspaceName}/git/pull`,
|
||||
{
|
||||
method: 'POST',
|
||||
}
|
||||
);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const commitAndPush = async (workspaceName, message) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/workspaces/${workspaceName}/git/commit`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ message }),
|
||||
}
|
||||
);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const getFileUrl = (workspaceName, filePath) => {
|
||||
return `${API_BASE_URL}/workspaces/${workspaceName}/files/${filePath}`;
|
||||
};
|
||||
@@ -2,7 +2,7 @@ import React from 'react';
|
||||
import { Text, Center } from '@mantine/core';
|
||||
import Editor from './Editor';
|
||||
import MarkdownPreview from './MarkdownPreview';
|
||||
import { getFileUrl } from '../../api/notes';
|
||||
import { getFileUrl } from '../../api/git';
|
||||
import { isImageFile } from '../../utils/fileHelpers';
|
||||
|
||||
const ContentView = ({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useState } from 'react';
|
||||
import { Modal, TextInput, Button, Group, Box } from '@mantine/core';
|
||||
import { useModalContext } from '../../../contexts/ModalContext';
|
||||
import { createWorkspace } from '../../../api/notes';
|
||||
import { createWorkspace } from '../../../api/git';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
|
||||
const CreateWorkspaceModal = ({ onWorkspaceCreated }) => {
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
import { IconFolders, IconSettings, IconFolderPlus } from '@tabler/icons-react';
|
||||
import { useWorkspace } from '../../contexts/WorkspaceContext';
|
||||
import { useModalContext } from '../../contexts/ModalContext';
|
||||
import { listWorkspaces } from '../../api/notes';
|
||||
import { listWorkspaces } from '../../api/git';
|
||||
import CreateWorkspaceModal from '../modals/workspace/CreateWorkspaceModal';
|
||||
|
||||
const WorkspaceSwitcher = () => {
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
updateLastWorkspaceName,
|
||||
deleteWorkspace,
|
||||
listWorkspaces,
|
||||
} from '../api/notes';
|
||||
} from '../api/git';
|
||||
import { DEFAULT_WORKSPACE_SETTINGS } from '../utils/constants';
|
||||
|
||||
const WorkspaceContext = createContext();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import { fetchFileContent } from '../api/notes';
|
||||
import { fetchFileContent } from '../api/git';
|
||||
import { isImageFile } from '../utils/fileHelpers';
|
||||
import { DEFAULT_FILE } from '../utils/constants';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { fetchFileList } from '../api/notes';
|
||||
import { fetchFileList } from '../api/git';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
|
||||
export const useFileList = () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback } from 'react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { saveFileContent, deleteFile } from '../api/notes';
|
||||
import { saveFileContent, deleteFile } from '../api/git';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
import { useGitOperations } from './useGitOperations';
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback } from 'react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { pullChanges, commitAndPush } from '../api/notes';
|
||||
import { pullChanges, commitAndPush } from '../api/git';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
|
||||
export const useGitOperations = () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useCallback } from 'react';
|
||||
import { getLastOpenedFile, updateLastOpenedFile } from '../api/notes';
|
||||
import { getLastOpenedFile, updateLastOpenedFile } from '../api/git';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
|
||||
export const useLastOpenedFile = () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { updateProfile, deleteProfile } from '../api/notes';
|
||||
import { updateProfile, deleteProfile } from '../api/git';
|
||||
|
||||
export function useProfileSettings() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
1
app/src/types/git.ts
Normal file
1
app/src/types/git.ts
Normal file
@@ -0,0 +1 @@
|
||||
type CommitHash = string;
|
||||
@@ -1,5 +1,5 @@
|
||||
import { visit } from 'unist-util-visit';
|
||||
import { lookupFileByName, getFileUrl } from '../api/notes';
|
||||
import { lookupFileByName, getFileUrl } from '../api/git';
|
||||
import { InlineContainerType, MARKDOWN_REGEX } from '../types/markdown';
|
||||
import { Node } from 'unist';
|
||||
import { Parent } from 'unist';
|
||||
|
||||
Reference in New Issue
Block a user