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