Add basic Notes UI

This commit is contained in:
Bruno Bernardino
2024-04-26 14:31:25 +01:00
parent 2920de90b6
commit 3f5422f8eb
14 changed files with 849 additions and 104 deletions

View File

@@ -0,0 +1,56 @@
import { Handlers, PageProps } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { getFile } from '/lib/data/files.ts';
import Note from '/islands/notes/Note.tsx';
interface Data {
fileName: string;
currentPath: string;
contents: string;
}
export const handler: Handlers<Data, FreshContextState> = {
async GET(request, context) {
if (!context.state.user) {
return new Response('Redirect', { status: 303, headers: { 'Location': `/login` } });
}
const { fileName } = context.params;
if (!fileName) {
return new Response('Not Found', { status: 404 });
}
const searchParams = new URL(request.url).searchParams;
let currentPath = searchParams.get('path') || '/';
// Send invalid paths back to notes root
if (!currentPath.startsWith('/Notes/') || currentPath.includes('../')) {
currentPath = '/Notes/';
}
// Always append a trailing slash
if (!currentPath.endsWith('/')) {
currentPath = `${currentPath}/`;
}
// Don't allow non-markdown files here
if (!fileName.endsWith('.md')) {
return new Response('Not Found', { status: 404 });
}
const fileResult = await getFile(context.state.user.id, currentPath, decodeURIComponent(fileName));
if (!fileResult.success) {
return new Response('Not Found', { status: 404 });
}
return await context.render({ fileName, currentPath, contents: new TextDecoder().decode(fileResult.contents!) });
},
};
export default function OpenNotePage({ data }: PageProps<Data, FreshContextState>) {
return <Note fileName={data.fileName} currentPath={data.currentPath} contents={data.contents} />;
}