mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add useFS hook
This commit is contained in:
11 files changed
+198
-199
No files matched your search
@@ -9,7 +9,7 @@ import { Send, Bot, User, Settings, Play } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useAISettings } from '@/contexts/AISettingsContext';
|
||||
import { FsToolSet } from '@/lib/FsToolSet';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
|
||||
const webcontainerPromise = WebContainer.boot();
|
||||
|
||||
@@ -26,6 +26,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
const { settings, isConfigured } = useAISettings();
|
||||
const { fs } = useFS();
|
||||
|
||||
const addMessage = (message: AIMessage) => {
|
||||
setMessages((prev) => {
|
||||
@@ -47,18 +48,18 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
|
||||
const buildFileTree = async (dirPath: string): Promise<FileSystemTree> => {
|
||||
const tree: FileSystemTree = {};
|
||||
const items = await fsManager.fs.promises.readdir(dirPath);
|
||||
const items = await fs.readdir(dirPath);
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = `${dirPath}/${item}`;
|
||||
const stat = await fsManager.fs.promises.stat(itemPath);
|
||||
const stat = await fs.stat(itemPath);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
tree[item] = {
|
||||
directory: await buildFileTree(itemPath),
|
||||
};
|
||||
} else {
|
||||
const content = await fsManager.fs.promises.readFile(itemPath, 'utf8');
|
||||
const content = await fs.readFile(itemPath, 'utf8');
|
||||
tree[item] = {
|
||||
file: {
|
||||
contents: content,
|
||||
@@ -89,7 +90,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
// Create dist directory in project if it doesn't exist
|
||||
const distPath = `/projects/${projectId}/dist`;
|
||||
try {
|
||||
await fsManager.fs.promises.mkdir(distPath);
|
||||
await fs.mkdir(distPath);
|
||||
} catch {
|
||||
// Directory might already exist, that's fine
|
||||
}
|
||||
@@ -109,7 +110,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
if (item.isDirectory()) {
|
||||
console.log(`Creating directory: ${destItemPath}`);
|
||||
try {
|
||||
await fsManager.fs.promises.mkdir(destItemPath);
|
||||
await fs.mkdir(destItemPath);
|
||||
} catch {
|
||||
// Directory might already exist
|
||||
}
|
||||
@@ -117,7 +118,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
} else {
|
||||
console.log(`Copying file: ${sourceItemPath} -> ${destItemPath}`);
|
||||
const content = await webcontainer.fs.readFile(sourceItemPath);
|
||||
await fsManager.fs.promises.writeFile(destItemPath, content);
|
||||
await fs.writeFile(destItemPath, content);
|
||||
}
|
||||
} catch (itemError) {
|
||||
console.warn(`Failed to copy item ${item.name}:`, itemError);
|
||||
@@ -137,7 +138,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
if (item.isDirectory()) {
|
||||
console.log(`Processing directory: ${item.name}`);
|
||||
try {
|
||||
await fsManager.fs.promises.mkdir(destPath);
|
||||
await fs.mkdir(destPath);
|
||||
} catch {
|
||||
// Directory might already exist
|
||||
}
|
||||
@@ -145,7 +146,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
} else {
|
||||
console.log(`Processing file: ${item.name}`);
|
||||
const content = await webcontainer.fs.readFile(sourcePath);
|
||||
await fsManager.fs.promises.writeFile(destPath, content);
|
||||
await fs.writeFile(destPath, content);
|
||||
}
|
||||
} catch (itemError) {
|
||||
console.warn(`Failed to process ${item.name}:`, itemError);
|
||||
@@ -201,7 +202,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
});
|
||||
|
||||
const provider = openai(settings.model);
|
||||
const toolSet = new FsToolSet(fsManager.fs.promises, `/projects/${projectId}`);
|
||||
const toolSet = new FsToolSet(fs, `/projects/${projectId}`);
|
||||
|
||||
return generateText({
|
||||
model: provider,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { ProjectsManager } from '@/lib/fs';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { ChevronRight, ChevronDown, File, Folder } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
@@ -20,17 +21,19 @@ interface FileNode {
|
||||
export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProps) {
|
||||
const [tree, setTree] = useState<FileNode[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { fs } = useFS();
|
||||
const projectsManager = new ProjectsManager(fs);
|
||||
|
||||
const buildFileTree = useCallback(async (projectId: string, dirPath: string): Promise<FileNode[]> => {
|
||||
const items = await fsManager.listFiles(projectId, dirPath);
|
||||
const items = await projectsManager.listFiles(projectId, dirPath);
|
||||
const nodes: FileNode[] = [];
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = dirPath ? `${dirPath}/${item}` : item;
|
||||
const fullPath = `${fsManager['dir']}/${projectId}/${itemPath}`;
|
||||
const fullPath = `${projectsManager['dir']}/${projectId}/${itemPath}`;
|
||||
|
||||
try {
|
||||
const stat = await fsManager['fs'].promises.stat(fullPath);
|
||||
const stat = await fs.stat(fullPath);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
const children = await buildFileTree(projectId, itemPath);
|
||||
@@ -59,7 +62,7 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp
|
||||
}
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
}, [projectId]);
|
||||
}, [projectsManager, fs]);
|
||||
|
||||
const loadFileTree = useCallback(async () => {
|
||||
setIsLoading(true);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { ProjectsManager } from '@/lib/fs';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { Tabs, TabsContent } from '@/components/ui/tabs';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Button } from '@/components/ui/button';
|
||||
@@ -49,11 +50,13 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [hasBuiltProject, setHasBuiltProject] = useState(false);
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
const { fs } = useFS();
|
||||
const projectsManager = new ProjectsManager(fs);
|
||||
|
||||
const loadFileContent = useCallback(async (filePath: string) => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const content = await fsManager.readFile(projectId, filePath);
|
||||
const content = await projectsManager.readFile(projectId, filePath);
|
||||
setFileContent(content);
|
||||
} catch (_error) {
|
||||
console.error('Failed to load file:', _error);
|
||||
@@ -61,7 +64,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [projectId]);
|
||||
}, [projectId, projectsManager]);
|
||||
|
||||
const getContentType = (filename: string): string => {
|
||||
const ext = filename.split('.').pop()?.toLowerCase();
|
||||
@@ -91,13 +94,13 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
|
||||
const checkForBuiltProject = useCallback(async () => {
|
||||
try {
|
||||
const exists = await fsManager.fileExists(projectId, 'dist/index.html');
|
||||
const exists = await projectsManager.fileExists(projectId, 'dist/index.html');
|
||||
setHasBuiltProject(exists);
|
||||
} catch (error) {
|
||||
console.error('Failed to check for built project:', error);
|
||||
setHasBuiltProject(false);
|
||||
}
|
||||
}, [projectId]);
|
||||
}, [projectId, projectsManager]);
|
||||
|
||||
const refreshIframe = useCallback(() => {
|
||||
if (iframeRef.current) {
|
||||
@@ -158,7 +161,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
|
||||
// SPA routing: try to serve the exact file first
|
||||
try {
|
||||
const file = await fsManager.readFile(projectId, 'dist' + filePath);
|
||||
const file = await projectsManager.readFile(projectId, 'dist' + filePath);
|
||||
console.log(`Serving file: ${filePath}`);
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
@@ -181,7 +184,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
|
||||
// SPA fallback: serve index.html for non-file requests
|
||||
try {
|
||||
const indexFile = await fsManager.readFile(projectId, 'dist/index.html');
|
||||
const indexFile = await projectsManager.readFile(projectId, 'dist/index.html');
|
||||
console.log(`Serving index.html fallback for: ${path}`);
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
@@ -224,7 +227,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
id
|
||||
});
|
||||
}
|
||||
}, [projectId, sendResponse, sendError]);
|
||||
}, [projectId, projectsManager, sendResponse, sendError]);
|
||||
|
||||
// Setup messaging protocol for iframe communication
|
||||
useEffect(() => {
|
||||
@@ -265,7 +268,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
if (!selectedFile) return;
|
||||
|
||||
try {
|
||||
await fsManager.fs.promises.writeFile(`/projects/${projectId}/${selectedFile}`, content);
|
||||
await fs.writeFile(`/projects/${projectId}/${selectedFile}`, content);
|
||||
setFileContent(content);
|
||||
} catch (error) {
|
||||
console.error('Failed to save file:', error);
|
||||
|
||||
Reference in new issue
Block a user