Add basic Notes UI
This commit is contained in:
56
routes/notes/open/[fileName].tsx
Normal file
56
routes/notes/open/[fileName].tsx
Normal 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} />;
|
||||
}
|
||||
Reference in New Issue
Block a user