diff --git a/backend/open_webui/routers/notes.py b/backend/open_webui/routers/notes.py index ed373e1b5..a78953bb4 100644 --- a/backend/open_webui/routers/notes.py +++ b/backend/open_webui/routers/notes.py @@ -6,7 +6,7 @@ from typing import Optional from fastapi import APIRouter, Depends, HTTPException, Request, status, BackgroundTasks from pydantic import BaseModel -from open_webui.models.users import Users, UserNameResponse +from open_webui.models.users import Users, UserResponse from open_webui.models.notes import Notes, NoteModel, NoteForm, NoteUserResponse from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT @@ -33,9 +33,7 @@ async def get_notes(user=Depends(get_verified_user)): NoteUserResponse( **{ **note.model_dump(), - "user": UserNameResponse( - **Users.get_user_by_id(note.user_id).model_dump() - ), + "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()), } ) for note in Notes.get_notes_by_user_id(user.id, "write") @@ -50,9 +48,7 @@ async def get_note_list(user=Depends(get_verified_user)): NoteUserResponse( **{ **note.model_dump(), - "user": UserNameResponse( - **Users.get_user_by_id(note.user_id).model_dump() - ), + "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()), } ) for note in Notes.get_notes_by_user_id(user.id, "read") diff --git a/src/lib/apis/notes/index.ts b/src/lib/apis/notes/index.ts index 445697fd3..23bec36f2 100644 --- a/src/lib/apis/notes/index.ts +++ b/src/lib/apis/notes/index.ts @@ -1,8 +1,10 @@ import { WEBUI_API_BASE_URL } from '$lib/constants'; +import { getTimeRange } from '$lib/utils'; type NoteItem = { title: string; - content: string; + data: object; + meta?: null | object; access_control?: null | object; }; @@ -65,7 +67,24 @@ export const getNotes = async (token: string = '') => { throw error; } - return res; + if (!Array.isArray(res)) { + return {}; // or throw new Error("Notes response is not an array") + } + + // Build the grouped object + const grouped: Record = {}; + for (const note of res) { + const timeRange = getTimeRange(note.updated_at / 1000000000); + if (!grouped[timeRange]) { + grouped[timeRange] = []; + } + grouped[timeRange].push({ + ...note, + timeRange + }); + } + + return grouped; }; export const getNoteById = async (token: string, id: string) => { diff --git a/src/lib/components/notes/Notes.svelte b/src/lib/components/notes/Notes.svelte index acbacf48e..629e8b7b3 100644 --- a/src/lib/components/notes/Notes.svelte +++ b/src/lib/components/notes/Notes.svelte @@ -3,11 +3,32 @@ import fileSaver from 'file-saver'; const { saveAs } = fileSaver; + import dayjs from '$lib/dayjs'; + import duration from 'dayjs/plugin/duration'; + import relativeTime from 'dayjs/plugin/relativeTime'; + + dayjs.extend(duration); + dayjs.extend(relativeTime); + + async function loadLocale(locales) { + for (const locale of locales) { + try { + dayjs.locale(locale); + break; // Stop after successfully loading the first available locale + } catch (error) { + console.error(`Could not load locale '${locale}':`, error); + } + } + } + + // Assuming $i18n.languages is an array of language codes + $: loadLocale($i18n.languages); + import { goto } from '$app/navigation'; import { onMount, getContext } from 'svelte'; import { WEBUI_NAME, config, prompts as _prompts, user } from '$lib/stores'; - import { getNotes } from '$lib/apis/notes'; + import { createNewNote, getNotes } from '$lib/apis/notes'; import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte'; import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; @@ -31,8 +52,24 @@ const init = async () => { notes = await getNotes(localStorage.token); + }; - console.log(notes); + const createNoteHandler = async () => { + const res = await createNewNote(localStorage.token, { + title: $i18n.t('New Note'), + data: { + content: '' + }, + meta: null, + access_control: null + }).catch((error) => { + toast.error(`${error}`); + return null; + }); + + if (res) { + goto(`/notes/${res.id}`); + } }; onMount(async () => { @@ -58,61 +95,55 @@ - {#if notes.length > 0} -
- -
-
-
- -
- -
-
-
+ {#each notes[timeRange] as note, idx (note.id)} +
+
+
+ +
+
{note.title}
+
-
+ {/each} {:else}
@@ -133,7 +164,9 @@ diff --git a/src/routes/(app)/notes/+layout.svelte b/src/routes/(app)/notes/+layout.svelte index 512cb2415..23a3ab72c 100644 --- a/src/routes/(app)/notes/+layout.svelte +++ b/src/routes/(app)/notes/+layout.svelte @@ -85,7 +85,7 @@
-
+
diff --git a/src/routes/(app)/notes/[id]/+page.svelte b/src/routes/(app)/notes/[id]/+page.svelte new file mode 100644 index 000000000..262d8555a --- /dev/null +++ b/src/routes/(app)/notes/[id]/+page.svelte @@ -0,0 +1,5 @@ + + +{$page.params.id}