carnet api
This commit is contained in:
parent
cc6e8a69d3
commit
72d22c02c6
75
app/api/nextcloud/status/route.ts
Normal file
75
app/api/nextcloud/status/route.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
||||
const nextcloudClientId = process.env.NEXTCLOUD_CLIENT_ID;
|
||||
const nextcloudClientSecret = process.env.NEXTCLOUD_CLIENT_SECRET;
|
||||
|
||||
if (!nextcloudUrl || !nextcloudClientId || !nextcloudClientSecret) {
|
||||
console.error('Missing Nextcloud configuration');
|
||||
return NextResponse.json(
|
||||
{ error: 'Nextcloud configuration is missing' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// Test Nextcloud connectivity
|
||||
const testResponse = await fetch(`${nextcloudUrl}/status.php`);
|
||||
if (!testResponse.ok) {
|
||||
console.error('Nextcloud is not accessible:', await testResponse.text());
|
||||
return NextResponse.json(
|
||||
{ error: "Nextcloud n'est pas accessible" },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get user's folders
|
||||
const foldersResponse = await fetch(
|
||||
`${nextcloudUrl}/remote.php/dav/files/${session.user.email}/`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(`${nextcloudClientId}:${nextcloudClientSecret}`).toString('base64')}`,
|
||||
'Depth': '1',
|
||||
'Content-Type': 'application/xml',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!foldersResponse.ok) {
|
||||
console.error('Failed to fetch folders:', await foldersResponse.text());
|
||||
return NextResponse.json(
|
||||
{ error: "Impossible d'accéder aux dossiers" },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
const folderData = await foldersResponse.text();
|
||||
// Parse the XML response to get folder names
|
||||
const folders = folderData.match(/<d:displayname>(.*?)<\/d:displayname>/g)
|
||||
?.map(match => match.replace(/<\/?d:displayname>/g, ''))
|
||||
?.filter(name => name !== session.user.email) || [];
|
||||
|
||||
return NextResponse.json({
|
||||
isConnected: true,
|
||||
folders
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error checking Nextcloud status:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to check Nextcloud status' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,7 @@ import { NotesView } from "@/components/carnet/notes-view";
|
||||
import { Editor } from "@/components/carnet/editor";
|
||||
import { PanelResizer } from "@/components/carnet/panel-resizer";
|
||||
import { useMediaQuery } from "@/hooks/use-media-query";
|
||||
import { toast } from "sonner";
|
||||
|
||||
// Layout modes
|
||||
export enum PaneLayout {
|
||||
@ -24,9 +25,19 @@ interface Note {
|
||||
lastEdited: Date;
|
||||
}
|
||||
|
||||
interface NextcloudStatus {
|
||||
isConnected: boolean;
|
||||
folders: string[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export default function CarnetPage() {
|
||||
const { data: session, status } = useSession();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [nextcloudStatus, setNextcloudStatus] = useState<NextcloudStatus>({
|
||||
isConnected: false,
|
||||
folders: []
|
||||
});
|
||||
const [layoutMode, setLayoutMode] = useState<PaneLayout>(PaneLayout.ItemSelection);
|
||||
const [selectedNote, setSelectedNote] = useState<Note | null>(null);
|
||||
const [isMobile, setIsMobile] = useState(false);
|
||||
@ -43,6 +54,37 @@ export default function CarnetPage() {
|
||||
const isSmallScreen = useMediaQuery("(max-width: 768px)");
|
||||
const isMediumScreen = useMediaQuery("(max-width: 1024px)");
|
||||
|
||||
// Check Nextcloud connection and get folders
|
||||
useEffect(() => {
|
||||
async function checkNextcloudConnection() {
|
||||
try {
|
||||
const response = await fetch('/api/nextcloud/status');
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || 'Failed to connect to Nextcloud');
|
||||
}
|
||||
|
||||
setNextcloudStatus({
|
||||
isConnected: true,
|
||||
folders: data.folders || []
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Nextcloud connection error:', error);
|
||||
setNextcloudStatus({
|
||||
isConnected: false,
|
||||
folders: [],
|
||||
error: error instanceof Error ? error.message : 'Failed to connect to Nextcloud'
|
||||
});
|
||||
toast.error("Impossible de se connecter à Nextcloud. Veuillez vérifier votre connexion.");
|
||||
}
|
||||
}
|
||||
|
||||
if (session?.user) {
|
||||
checkNextcloudConnection();
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
useEffect(() => {
|
||||
if (status === "unauthenticated") {
|
||||
redirect("/signin");
|
||||
@ -105,6 +147,27 @@ export default function CarnetPage() {
|
||||
);
|
||||
}
|
||||
|
||||
if (!nextcloudStatus.isConnected) {
|
||||
return (
|
||||
<div className="flex h-[calc(100vh-3.5rem)] mt-14 items-center justify-center bg-carnet-bg">
|
||||
<div className="text-center">
|
||||
<h2 className="text-xl font-semibold text-carnet-text-primary mb-2">
|
||||
Connexion à Nextcloud impossible
|
||||
</h2>
|
||||
<p className="text-carnet-text-muted mb-4">
|
||||
{nextcloudStatus.error || "Veuillez vérifier votre connexion et réessayer."}
|
||||
</p>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="px-4 py-2 bg-primary text-white rounded-md hover:bg-primary/90"
|
||||
>
|
||||
Réessayer
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="flex h-[calc(100vh-3.5rem)] mt-14 bg-carnet-bg">
|
||||
{/* Navigation Panel */}
|
||||
@ -114,7 +177,7 @@ export default function CarnetPage() {
|
||||
className="flex flex-col h-full bg-carnet-sidebar"
|
||||
style={{ width: `${navWidth}px` }}
|
||||
>
|
||||
<Navigation onLayoutChange={setLayoutMode} />
|
||||
<Navigation onLayoutChange={setLayoutMode} nextcloudFolders={nextcloudStatus.folders} />
|
||||
</div>
|
||||
|
||||
{/* Navigation Resizer */}
|
||||
|
||||
@ -3,13 +3,18 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Image, FileText, Link, List } from 'lucide-react';
|
||||
|
||||
interface Note {
|
||||
id: string;
|
||||
title: string;
|
||||
content: string;
|
||||
lastEdited: Date;
|
||||
category?: string;
|
||||
tags?: string[];
|
||||
}
|
||||
|
||||
interface EditorProps {
|
||||
note?: {
|
||||
id: string;
|
||||
title: string;
|
||||
content: string;
|
||||
};
|
||||
onSave?: (note: { id: string; title: string; content: string }) => void;
|
||||
note?: Note | null;
|
||||
onSave?: (note: Note) => void;
|
||||
}
|
||||
|
||||
export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||
@ -36,7 +41,10 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||
onSave?.({
|
||||
id: note.id,
|
||||
title,
|
||||
content
|
||||
content,
|
||||
lastEdited: new Date(),
|
||||
category: note.category,
|
||||
tags: note.tags
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { Search, BookOpen, Tag, Trash2, Star, Archive, X } from 'lucide-react';
|
||||
import { Search, BookOpen, Tag, Trash2, Star, Archive, X, Folder } from 'lucide-react';
|
||||
import { PaneLayout } from '@/app/carnet/page';
|
||||
|
||||
interface NavigationProps {
|
||||
onLayoutChange?: (layout: PaneLayout) => void;
|
||||
nextcloudFolders?: string[];
|
||||
}
|
||||
|
||||
export const Navigation: React.FC<NavigationProps> = ({ onLayoutChange }) => {
|
||||
export const Navigation: React.FC<NavigationProps> = ({ onLayoutChange, nextcloudFolders = [] }) => {
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
|
||||
return (
|
||||
@ -92,6 +93,24 @@ export const Navigation: React.FC<NavigationProps> = ({ onLayoutChange }) => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Nextcloud Folders */}
|
||||
{nextcloudFolders.length > 0 && (
|
||||
<div className="mt-6 px-3">
|
||||
<h3 className="px-3 mb-2 text-xs font-medium text-carnet-text-muted uppercase">Dossiers Nextcloud</h3>
|
||||
<div className="space-y-0.5">
|
||||
{nextcloudFolders.map((folder) => (
|
||||
<div
|
||||
key={folder}
|
||||
className="flex items-center px-3 py-2 rounded-md hover:bg-carnet-hover cursor-pointer"
|
||||
>
|
||||
<Folder className="w-4 h-4 mr-3 text-carnet-text-muted" />
|
||||
<span className="text-sm text-carnet-text-primary">{folder}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user