Add basic Notes UI
This commit is contained in:
@@ -20,7 +20,7 @@ export const handler: Handlers<Data, FreshContextState> = {
|
||||
|
||||
const parentPath = requestBody.get('parent_path') as string;
|
||||
const name = requestBody.get('name') as string;
|
||||
const contents = requestBody.get('contents') as File;
|
||||
const contents = requestBody.get('contents') as File | string;
|
||||
|
||||
if (
|
||||
!parentPath || !name.trim() || !contents || !parentPath.startsWith('/') ||
|
||||
@@ -29,7 +29,9 @@ export const handler: Handlers<Data, FreshContextState> = {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
const createdFile = await createFile(context.state.user.id, parentPath, name.trim(), await contents.arrayBuffer());
|
||||
const fileContents = typeof contents === 'string' ? contents : await contents.arrayBuffer();
|
||||
|
||||
const createdFile = await createFile(context.state.user.id, parentPath, name.trim(), fileContents);
|
||||
|
||||
const newFiles = await getFiles(context.state.user.id, parentPath);
|
||||
|
||||
|
||||
66
routes/api/notes/save.tsx
Normal file
66
routes/api/notes/save.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState } from '/lib/types.ts';
|
||||
import { getFile, updateFile } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
fileName: string;
|
||||
currentPath: string;
|
||||
contents: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.currentPath || !requestBody.fileName || !requestBody.currentPath.startsWith('/Notes/') ||
|
||||
requestBody.currentPath.includes('../') || !requestBody.currentPath.endsWith('/')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
if (
|
||||
!requestBody.currentPath || !requestBody.currentPath.startsWith('/Notes/') ||
|
||||
requestBody.currentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// Don't allow non-markdown files here
|
||||
if (!requestBody.fileName.endsWith('.md')) {
|
||||
return new Response('Not Found', { status: 404 });
|
||||
}
|
||||
|
||||
const fileResult = await getFile(
|
||||
context.state.user.id,
|
||||
requestBody.currentPath,
|
||||
decodeURIComponent(requestBody.fileName),
|
||||
);
|
||||
|
||||
if (!fileResult.success) {
|
||||
return new Response('Not Found', { status: 404 });
|
||||
}
|
||||
|
||||
const updatedFile = await updateFile(
|
||||
context.state.user.id,
|
||||
requestBody.currentPath,
|
||||
decodeURIComponent(requestBody.fileName),
|
||||
requestBody.contents || '',
|
||||
);
|
||||
|
||||
const responseBody: ResponseBody = { success: updatedFile };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
53
routes/notes.tsx
Normal file
53
routes/notes.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import { Handlers, PageProps } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, DirectoryFile, FreshContextState } from '/lib/types.ts';
|
||||
import { getDirectories, getFiles } from '/lib/data/files.ts';
|
||||
import NotesWrapper from '/islands/notes/NotesWrapper.tsx';
|
||||
|
||||
interface Data {
|
||||
userDirectories: Directory[];
|
||||
userNotes: DirectoryFile[];
|
||||
currentPath: 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 searchParams = new URL(request.url).searchParams;
|
||||
|
||||
let currentPath = searchParams.get('path') || '/Notes/';
|
||||
|
||||
// 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}/`;
|
||||
}
|
||||
|
||||
const userDirectories = await getDirectories(context.state.user.id, currentPath);
|
||||
|
||||
const userFiles = await getFiles(context.state.user.id, currentPath);
|
||||
|
||||
const userNotes = userFiles.filter((file) => file.file_name.endsWith('.md'));
|
||||
|
||||
return await context.render({ userDirectories, userNotes, currentPath });
|
||||
},
|
||||
};
|
||||
|
||||
export default function FilesPage({ data }: PageProps<Data, FreshContextState>) {
|
||||
return (
|
||||
<main>
|
||||
<NotesWrapper
|
||||
initialDirectories={data.userDirectories}
|
||||
initialFiles={data.userNotes}
|
||||
initialPath={data.currentPath}
|
||||
/>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
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