mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Migrate useLastOpenedFile hook
This commit is contained in:
@@ -3,10 +3,8 @@ import { apiCall } from './api';
|
|||||||
import {
|
import {
|
||||||
FileNode,
|
FileNode,
|
||||||
isFileNode,
|
isFileNode,
|
||||||
isLastOpenedFileResponse,
|
|
||||||
isLookupResponse,
|
isLookupResponse,
|
||||||
isSaveFileResponse,
|
isSaveFileResponse,
|
||||||
LastOpenedFileResponse,
|
|
||||||
LookupResponse,
|
LookupResponse,
|
||||||
SaveFileResponse,
|
SaveFileResponse,
|
||||||
} from '@/types/fileApi';
|
} from '@/types/fileApi';
|
||||||
@@ -138,11 +136,10 @@ export const getLastOpenedFile = async (
|
|||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/files/last`
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/files/last`
|
||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (!isLastOpenedFileResponse(data)) {
|
if (!('lastOpenedFilePath' in data)) {
|
||||||
throw new Error('Invalid last opened file response received from API');
|
throw new Error('Invalid last opened file response received from API');
|
||||||
}
|
}
|
||||||
const lastOpenedFileResponse = data as LastOpenedFileResponse;
|
return data.lastOpenedFilePath;
|
||||||
return lastOpenedFileResponse.lastOpenedFilePath;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { getLastOpenedFile, updateLastOpenedFile } from '../api/git';
|
import { getLastOpenedFile, updateLastOpenedFile } from '../api/file';
|
||||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
|
|
||||||
export const useLastOpenedFile = () => {
|
interface UseLastOpenedFileResult {
|
||||||
|
loadLastOpenedFile: () => Promise<string | null>;
|
||||||
|
saveLastOpenedFile: (filePath: string) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useLastOpenedFile = (): UseLastOpenedFileResult => {
|
||||||
const { currentWorkspace } = useWorkspace();
|
const { currentWorkspace } = useWorkspace();
|
||||||
|
|
||||||
const loadLastOpenedFile = useCallback(async () => {
|
const loadLastOpenedFile = useCallback(async (): Promise<string | null> => {
|
||||||
if (!currentWorkspace) return null;
|
if (!currentWorkspace) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await getLastOpenedFile(currentWorkspace.name);
|
const response: string = await getLastOpenedFile(currentWorkspace.name);
|
||||||
return response.lastOpenedFilePath || null;
|
return response || null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load last opened file:', error);
|
console.error('Failed to load last opened file:', error);
|
||||||
return null;
|
return null;
|
||||||
@@ -18,7 +23,7 @@ export const useLastOpenedFile = () => {
|
|||||||
}, [currentWorkspace]);
|
}, [currentWorkspace]);
|
||||||
|
|
||||||
const saveLastOpenedFile = useCallback(
|
const saveLastOpenedFile = useCallback(
|
||||||
async (filePath) => {
|
async (filePath: string): Promise<void> => {
|
||||||
if (!currentWorkspace) return;
|
if (!currentWorkspace) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -31,21 +31,6 @@ export function isSaveFileResponse(obj: unknown): obj is SaveFileResponse {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LastOpenedFileResponse {
|
|
||||||
lastOpenedFilePath: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isLastOpenedFileResponse(
|
|
||||||
obj: unknown
|
|
||||||
): obj is LastOpenedFileResponse {
|
|
||||||
return (
|
|
||||||
typeof obj === 'object' &&
|
|
||||||
obj !== null &&
|
|
||||||
'lastOpenedFilePath' in obj &&
|
|
||||||
typeof (obj as LastOpenedFileResponse).lastOpenedFilePath === 'string'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdateLastOpenedFileRequest {
|
export interface UpdateLastOpenedFileRequest {
|
||||||
filePath: string;
|
filePath: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user