carnet panel editor
This commit is contained in:
parent
7e681e5a84
commit
ff7fb74a82
70
app/api/nextcloud/files/content/route.ts
Normal file
70
app/api/nextcloud/files/content/route.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,9 +7,10 @@ interface Note {
|
|||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
content: string;
|
content: string;
|
||||||
lastEdited: Date;
|
lastModified: string;
|
||||||
category?: string;
|
type: string;
|
||||||
tags?: string[];
|
mime: string;
|
||||||
|
etag: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditorProps {
|
interface EditorProps {
|
||||||
@ -22,11 +23,33 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
|
|||||||
const [title, setTitle] = useState(note?.title || '');
|
const [title, setTitle] = useState(note?.title || '');
|
||||||
const [content, setContent] = useState(note?.content || '');
|
const [content, setContent] = useState(note?.content || '');
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
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) {
|
if (note) {
|
||||||
setTitle(note.title);
|
setTitle(note.title);
|
||||||
setContent(note.content);
|
fetchNoteContent();
|
||||||
|
} else {
|
||||||
|
setTitle('');
|
||||||
|
setContent('');
|
||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
@ -65,12 +88,8 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
|
|||||||
|
|
||||||
const savedNote = await response.json();
|
const savedNote = await response.json();
|
||||||
onSave?.({
|
onSave?.({
|
||||||
id: savedNote.id,
|
...savedNote,
|
||||||
title: savedNote.title,
|
content
|
||||||
content,
|
|
||||||
lastEdited: new Date(savedNote.lastModified),
|
|
||||||
category: note?.category,
|
|
||||||
tags: note?.tags
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving note:', error);
|
console.error('Error saving note:', error);
|
||||||
@ -117,12 +136,18 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave, currentFolder = 'N
|
|||||||
|
|
||||||
{/* Editor Area */}
|
{/* Editor Area */}
|
||||||
<div className="flex-1 p-4">
|
<div className="flex-1 p-4">
|
||||||
<textarea
|
{isLoading ? (
|
||||||
value={content}
|
<div className="flex items-center justify-center h-full">
|
||||||
onChange={handleContentChange}
|
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-primary"></div>
|
||||||
placeholder="Ecrire..."
|
</div>
|
||||||
className="w-full h-full resize-none focus:outline-none bg-transparent text-carnet-text-primary placeholder-carnet-text-muted"
|
) : (
|
||||||
/>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -46,20 +46,6 @@ export const NotesView: React.FC<NotesViewProps> = ({ onNoteSelect, currentFolde
|
|||||||
fetchNotes();
|
fetchNotes();
|
||||||
}, [currentFolder]);
|
}, [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) => {
|
const formatDate = (dateString: string) => {
|
||||||
return format(new Date(dateString), 'EEEE d MMM yyyy', { locale: fr });
|
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>
|
||||||
</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 */}
|
{/* Notes List */}
|
||||||
<div className="flex-1 overflow-y-auto">
|
<div className="flex-1 overflow-y-auto">
|
||||||
{loading ? (
|
{loading ? (
|
||||||
@ -190,17 +162,6 @@ export const NotesView: React.FC<NotesViewProps> = ({ onNoteSelect, currentFolde
|
|||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
</div>
|
</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>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user