Spaces:
Running
Running
| // Initialize Supabase | |
| const supabaseUrl = 'YOUR_SUPABASE_URL'; | |
| const supabaseKey = 'YOUR_SUPABASE_KEY'; | |
| const supabase = supabase.createClient(supabaseUrl, supabaseKey); | |
| // DOM Elements | |
| const newNoteBtn = document.querySelector('.new-note-btn'); | |
| const noteContent = document.querySelector('.note-content'); | |
| const searchInput = document.querySelector('.search-bar input'); | |
| // Event Listeners | |
| newNoteBtn.addEventListener('click', createNewNote); | |
| searchInput.addEventListener('input', handleSearch); | |
| // Functions | |
| async function createNewNote() { | |
| try { | |
| const { data, error } = await supabase | |
| .from('notes') | |
| .insert([{ | |
| title: 'Untitled Note', | |
| content: '', | |
| created_at: new Date() | |
| }]) | |
| .select(); | |
| if (error) throw error; | |
| // Clear editor and focus | |
| noteContent.innerHTML = '<h1>Untitled Note</h1><p></p>'; | |
| noteContent.focus(); | |
| console.log('New note created:', data); | |
| } catch (error) { | |
| console.error('Error creating note:', error); | |
| } | |
| } | |
| function handleSearch(e) { | |
| const query = e.target.value.trim(); | |
| if (query.length > 2) { | |
| searchNotes(query); | |
| } | |
| } | |
| async function searchNotes(query) { | |
| try { | |
| const { data, error } = await supabase | |
| .from('notes') | |
| .select() | |
| .textSearch('content', query); | |
| if (error) throw error; | |
| console.log('Search results:', data); | |
| // TODO: Display search results | |
| } catch (error) { | |
| console.error('Error searching notes:', error); | |
| } | |
| } | |
| // Initialize the app | |
| async function initApp() { | |
| // Check auth status | |
| const { data: { user } } = await supabase.auth.getUser(); | |
| if (!user) { | |
| // Redirect to login if not authenticated | |
| window.location.href = '/login.html'; | |
| } else { | |
| // Load user's notes | |
| loadNotes(); | |
| } | |
| } | |
| async function loadNotes() { | |
| try { | |
| const { data, error } = await supabase | |
| .from('notes') | |
| .select() | |
| .order('created_at', { ascending: false }); | |
| if (error) throw error; | |
| console.log('Loaded notes:', data); | |
| // TODO: Display notes in sidebar | |
| } catch (error) { | |
| console.error('Error loading notes:', error); | |
| } | |
| } | |
| // Start the app | |
| document.addEventListener('DOMContentLoaded', initApp); |