File size: 2,473 Bytes
58d09df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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);