carnet panel editor

This commit is contained in:
alma 2025-04-20 18:00:20 +02:00
parent 7e681e5a84
commit ff7fb74a82
3 changed files with 111 additions and 55 deletions

View File

@ -0,0 +1,70 @@
import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { PrismaClient } from '@prisma/client';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { createClient } from 'webdav';
// Use a single PrismaClient instance
declare global {
var prisma: PrismaClient | undefined;
}
const prisma = global.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') global.prisma = prisma;
// Helper function to create WebDAV client
const createWebDAVClient = async (userId: string) => {
const credentials = await prisma.webDAVCredentials.findUnique({
where: { userId },
});
if (!credentials) {
throw new Error('No WebDAV credentials found');
}
const baseURL = process.env.NEXTCLOUD_URL;
if (!baseURL) {
throw new Error('NEXTCLOUD_URL environment variable is not set');
}
const normalizedBaseURL = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL;
const webdavURL = `${normalizedBaseURL}/remote.php/dav`;
return {
client: createClient(webdavURL, {
username: credentials.username,
password: credentials.password,
authType: 'password',
}),
username: credentials.username
};
};
export async function GET(request: Request) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return NextResponse.json({ error: 'Note ID is required' }, { status: 400 });
}
const { client } = await createWebDAVClient(session.user.id);
try {
const content = await client.getFileContents(id, { format: 'text' });
return NextResponse.json({ content });
} catch (error) {
console.error('Error fetching note content:', error);
return NextResponse.json({ error: 'Failed to fetch note content' }, { status: 500 });
}
} catch (error) {
console.error('Error in GET request:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}

View File

@ -7,9 +7,10 @@ interface Note {
id: string;
title: string;
content: string;
lastEdited: Date;
category?: string;
tags?: string[];
lastModified: string;
type: string;
mime: string;
etag: string;
}
interface EditorProps {
@ -22,11 +23,33 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
const [title, setTitle] = useState(note?.title || '');
const [content, setContent] = useState(note?.content || '');
const [isSaving, setIsSaving] = useState(false);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const fetchNoteContent = async () => {
if (note?.id) {
setIsLoading(true);
try {
const response = await fetch(`/api/nextcloud/files/content?id=${note.id}`);
if (!response.ok) {
throw new Error('Failed to fetch note content');
}
const data = await response.json();
setContent(data.content);
} catch (error) {
console.error('Error fetching note content:', error);
} finally {
setIsLoading(false);
}
}
};
if (note) {
setTitle(note.title);
setContent(note.content);
fetchNoteContent();
} else {
setTitle('');
setContent('');
}
}, [note]);
@ -65,12 +88,8 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
const savedNote = await response.json();
onSave?.({
id: savedNote.id,
title: savedNote.title,
content,
lastEdited: new Date(savedNote.lastModified),
category: note?.category,
tags: note?.tags
...savedNote,
content
});
} catch (error) {
console.error('Error saving note:', error);
@ -117,12 +136,18 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
{/* Editor Area */}
<div className="flex-1 p-4">
<textarea
value={content}
onChange={handleContentChange}
placeholder="Ecrire..."
className="w-full h-full resize-none focus:outline-none bg-transparent text-carnet-text-primary placeholder-carnet-text-muted"
/>
{isLoading ? (
<div className="flex items-center justify-center h-full">
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-primary"></div>
</div>
) : (
<textarea
value={content}
onChange={handleContentChange}
placeholder="Ecrire..."
className="w-full h-full resize-none focus:outline-none bg-transparent text-carnet-text-primary placeholder-carnet-text-muted"
/>
)}
</div>
</div>
);

View File

@ -46,20 +46,6 @@ export const NotesView: React.FC<NotesViewProps> = ({ onNoteSelect, currentFolde
fetchNotes();
}, [currentFolder]);
const handleNewNote = () => {
const newNote: Note = {
id: Date.now().toString(),
title: 'New Note',
lastModified: new Date().toISOString(),
size: 0,
type: 'file',
mime: 'text/markdown',
etag: ''
};
setNotes([newNote, ...notes]);
onNoteSelect?.(newNote);
};
const formatDate = (dateString: string) => {
return format(new Date(dateString), 'EEEE d MMM yyyy', { locale: fr });
};
@ -133,20 +119,6 @@ export const NotesView: React.FC<NotesViewProps> = ({ onNoteSelect, currentFolde
</div>
</div>
{/* Folder Header */}
<div className="px-4 py-3 border-b border-carnet-border flex items-center justify-between">
<div className="flex items-center">
<Icon className="w-4 h-4 mr-2 text-carnet-text-muted" />
<span className="text-sm font-medium text-carnet-text-primary">{currentFolder}</span>
</div>
<button
className="p-1.5 rounded-md hover:bg-carnet-hover"
onClick={handleNewNote}
>
<Plus className="w-4 h-4 text-carnet-text-muted" />
</button>
</div>
{/* Notes List */}
<div className="flex-1 overflow-y-auto">
{loading ? (
@ -190,17 +162,6 @@ export const NotesView: React.FC<NotesViewProps> = ({ onNoteSelect, currentFolde
</ul>
)}
</div>
{/* New Note Button */}
<div className="p-4 border-t border-carnet-border">
<button
onClick={handleNewNote}
className="w-full flex items-center justify-center space-x-2 px-4 py-2 bg-primary text-white rounded-md hover:bg-primary-dark"
>
<Plus className="h-4 w-4" />
<span>Nouvelle note</span>
</button>
</div>
</div>
);
};