mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-07 16:34:26 +00:00
Merge pull request #9 from LordMathis/feat/last-opened-file
Feat/last opened file
This commit is contained in:
@@ -120,19 +120,19 @@ func DeleteFile(fs *filesystem.FileSystem) http.HandlerFunc {
|
|||||||
|
|
||||||
func GetLastOpenedFile(db *db.DB) http.HandlerFunc {
|
func GetLastOpenedFile(db *db.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, _, err := getUserAndWorkspaceIDs(r)
|
_, workspaceID, err := getUserAndWorkspaceIDs(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := db.GetUserByID(userID)
|
filePath, err := db.GetLastOpenedFile(workspaceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get user", http.StatusInternalServerError)
|
http.Error(w, "Failed to get last opened file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
respondJSON(w, map[string]string{"lastOpenedFile": user.LastOpenedFilePath})
|
respondJSON(w, map[string]string{"lastOpenedFilePath": filePath})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,14 +153,15 @@ func UpdateLastOpenedFile(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that the file path is valid within the workspace
|
// Validate the file path exists in the workspace
|
||||||
_, err = fs.ValidatePath(userID, workspaceID, requestBody.FilePath)
|
if requestBody.FilePath != "" {
|
||||||
if err != nil {
|
if _, err := fs.ValidatePath(userID, workspaceID, requestBody.FilePath); err != nil {
|
||||||
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
http.Error(w, "Invalid file path", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.UpdateLastOpenedFile(userID, requestBody.FilePath); err != nil {
|
if err := db.UpdateLastOpenedFile(workspaceID, requestBody.FilePath); err != nil {
|
||||||
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
|
http.Error(w, "Failed to update last opened file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ var migrations = []Migration{
|
|||||||
password_hash TEXT NOT NULL,
|
password_hash TEXT NOT NULL,
|
||||||
role TEXT NOT NULL CHECK(role IN ('admin', 'editor', 'viewer')),
|
role TEXT NOT NULL CHECK(role IN ('admin', 'editor', 'viewer')),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
last_workspace_id INTEGER,
|
last_workspace_id INTEGER
|
||||||
last_opened_file_path TEXT
|
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Create workspaces table with integrated settings
|
-- Create workspaces table with integrated settings
|
||||||
@@ -32,6 +31,7 @@ var migrations = []Migration{
|
|||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
last_opened_file_path TEXT,
|
||||||
-- Settings fields
|
-- Settings fields
|
||||||
theme TEXT NOT NULL DEFAULT 'light' CHECK(theme IN ('light', 'dark')),
|
theme TEXT NOT NULL DEFAULT 'light' CHECK(theme IN ('light', 'dark')),
|
||||||
auto_save BOOLEAN NOT NULL DEFAULT 0,
|
auto_save BOOLEAN NOT NULL DEFAULT 0,
|
||||||
|
|||||||
@@ -81,15 +81,13 @@ func (db *DB) createWorkspaceTx(tx *sql.Tx, workspace *models.Workspace) error {
|
|||||||
func (db *DB) GetUserByID(id int) (*models.User, error) {
|
func (db *DB) GetUserByID(id int) (*models.User, error) {
|
||||||
user := &models.User{}
|
user := &models.User{}
|
||||||
err := db.QueryRow(`
|
err := db.QueryRow(`
|
||||||
SELECT
|
SELECT
|
||||||
u.id, u.email, u.display_name, u.role, u.created_at,
|
id, email, display_name, role, created_at,
|
||||||
u.last_workspace_id, u.last_opened_file_path,
|
last_workspace_id
|
||||||
COALESCE(w.id, 0) as workspace_id
|
FROM users
|
||||||
FROM users u
|
WHERE id = ?`, id).
|
||||||
LEFT JOIN workspaces w ON w.id = u.last_workspace_id
|
|
||||||
WHERE u.id = ?`, id).
|
|
||||||
Scan(&user.ID, &user.Email, &user.DisplayName, &user.Role, &user.CreatedAt,
|
Scan(&user.ID, &user.Email, &user.DisplayName, &user.Role, &user.CreatedAt,
|
||||||
&user.LastWorkspaceID, &user.LastOpenedFilePath, &user.LastWorkspaceID)
|
&user.LastWorkspaceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,34 +96,27 @@ func (db *DB) GetUserByID(id int) (*models.User, error) {
|
|||||||
|
|
||||||
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
|
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
|
||||||
user := &models.User{}
|
user := &models.User{}
|
||||||
var lastOpenedFilePath sql.NullString
|
|
||||||
err := db.QueryRow(`
|
err := db.QueryRow(`
|
||||||
SELECT
|
SELECT
|
||||||
u.id, u.email, u.display_name, u.password_hash, u.role, u.created_at,
|
id, email, display_name, password_hash, role, created_at,
|
||||||
u.last_workspace_id, u.last_opened_file_path,
|
last_workspace_id
|
||||||
COALESCE(w.id, 0) as workspace_id
|
FROM users
|
||||||
FROM users u
|
WHERE email = ?`, email).
|
||||||
LEFT JOIN workspaces w ON w.id = u.last_workspace_id
|
|
||||||
WHERE u.email = ?`, email).
|
|
||||||
Scan(&user.ID, &user.Email, &user.DisplayName, &user.PasswordHash, &user.Role, &user.CreatedAt,
|
Scan(&user.ID, &user.Email, &user.DisplayName, &user.PasswordHash, &user.Role, &user.CreatedAt,
|
||||||
&user.LastWorkspaceID, &lastOpenedFilePath, &user.LastWorkspaceID)
|
&user.LastWorkspaceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if lastOpenedFilePath.Valid {
|
|
||||||
user.LastOpenedFilePath = lastOpenedFilePath.String
|
|
||||||
} else {
|
|
||||||
user.LastOpenedFilePath = ""
|
|
||||||
}
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) UpdateUser(user *models.User) error {
|
func (db *DB) UpdateUser(user *models.User) error {
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
UPDATE users
|
UPDATE users
|
||||||
SET email = ?, display_name = ?, role = ?, last_workspace_id = ?, last_opened_file_path = ?
|
SET email = ?, display_name = ?, role = ?, last_workspace_id = ?
|
||||||
WHERE id = ?`,
|
WHERE id = ?`,
|
||||||
user.Email, user.DisplayName, user.Role, user.LastWorkspaceID, user.LastOpenedFilePath, user.ID)
|
user.Email, user.DisplayName, user.Role, user.LastWorkspaceID, user.ID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,11 +125,6 @@ func (db *DB) UpdateLastWorkspace(userID, workspaceID int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) UpdateLastOpenedFile(userID int, filePath string) error {
|
|
||||||
_, err := db.Exec("UPDATE users SET last_opened_file_path = ? WHERE id = ?", filePath, userID)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) DeleteUser(id int) error {
|
func (db *DB) DeleteUser(id int) error {
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -160,3 +160,20 @@ func (db *DB) UpdateLastWorkspaceTx(tx *sql.Tx, userID, workspaceID int) error {
|
|||||||
_, err := tx.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?", workspaceID, userID)
|
_, err := tx.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?", workspaceID, userID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpdateLastOpenedFile(workspaceID int, filePath string) error {
|
||||||
|
_, err := db.Exec("UPDATE workspaces SET last_opened_file_path = ? WHERE id = ?", filePath, workspaceID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetLastOpenedFile(workspaceID int) (string, error) {
|
||||||
|
var filePath sql.NullString
|
||||||
|
err := db.QueryRow("SELECT last_opened_file_path FROM workspaces WHERE id = ?", workspaceID).Scan(&filePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !filePath.Valid {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return filePath.String, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,14 +17,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `json:"id" validate:"required,min=1"`
|
ID int `json:"id" validate:"required,min=1"`
|
||||||
Email string `json:"email" validate:"required,email"`
|
Email string `json:"email" validate:"required,email"`
|
||||||
DisplayName string `json:"displayName"`
|
DisplayName string `json:"displayName"`
|
||||||
PasswordHash string `json:"-"`
|
PasswordHash string `json:"-"`
|
||||||
Role UserRole `json:"role" validate:"required,oneof=admin editor viewer"`
|
Role UserRole `json:"role" validate:"required,oneof=admin editor viewer"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
LastWorkspaceID int `json:"lastWorkspaceId"`
|
LastWorkspaceID int `json:"lastWorkspaceId"`
|
||||||
LastOpenedFilePath string `json:"lastOpenedFilePath"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) Validate() error {
|
func (u *User) Validate() error {
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Workspace struct {
|
type Workspace struct {
|
||||||
ID int `json:"id" validate:"required,min=1"`
|
ID int `json:"id" validate:"required,min=1"`
|
||||||
UserID int `json:"userId" validate:"required,min=1"`
|
UserID int `json:"userId" validate:"required,min=1"`
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
LastOpenedFilePath string `json:"lastOpenedFilePath"`
|
||||||
|
|
||||||
// Integrated settings
|
// Integrated settings
|
||||||
Theme string `json:"theme" validate:"oneof=light dark"`
|
Theme string `json:"theme" validate:"oneof=light dark"`
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@@ -28,7 +29,7 @@ func (s *UserService) SetupAdminUser(adminEmail, adminPassword string) (*models.
|
|||||||
adminUser, err := s.DB.GetUserByEmail(adminEmail)
|
adminUser, err := s.DB.GetUserByEmail(adminEmail)
|
||||||
if adminUser != nil {
|
if adminUser != nil {
|
||||||
return adminUser, nil // Admin user already exists
|
return adminUser, nil // Admin user already exists
|
||||||
} else if err != nil {
|
} else if err != sql.ErrNoRows {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,28 @@
|
|||||||
import { useState, useCallback, useEffect } from 'react'; // Added useEffect
|
import { useState, useCallback, useEffect } from 'react';
|
||||||
import { notifications } from '@mantine/notifications';
|
import { notifications } from '@mantine/notifications';
|
||||||
import { lookupFileByName } from '../services/api';
|
import { lookupFileByName } from '../services/api';
|
||||||
import { DEFAULT_FILE } from '../utils/constants';
|
import { DEFAULT_FILE } from '../utils/constants';
|
||||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
|
import { useLastOpenedFile } from './useLastOpenedFile';
|
||||||
|
|
||||||
export const useFileNavigation = () => {
|
export const useFileNavigation = () => {
|
||||||
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
|
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
|
||||||
const [isNewFile, setIsNewFile] = useState(true);
|
const [isNewFile, setIsNewFile] = useState(true);
|
||||||
const { currentWorkspace } = useWorkspace();
|
const { currentWorkspace } = useWorkspace();
|
||||||
|
const { loadLastOpenedFile, saveLastOpenedFile } = useLastOpenedFile();
|
||||||
|
|
||||||
const handleFileSelect = useCallback((filePath) => {
|
const handleFileSelect = useCallback(
|
||||||
setSelectedFile(filePath || DEFAULT_FILE.path);
|
async (filePath) => {
|
||||||
setIsNewFile(filePath ? false : true);
|
const newPath = filePath || DEFAULT_FILE.path;
|
||||||
}, []);
|
setSelectedFile(newPath);
|
||||||
|
setIsNewFile(!filePath);
|
||||||
|
|
||||||
|
if (filePath) {
|
||||||
|
await saveLastOpenedFile(filePath);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[saveLastOpenedFile]
|
||||||
|
);
|
||||||
|
|
||||||
const handleLinkClick = useCallback(
|
const handleLinkClick = useCallback(
|
||||||
async (filename) => {
|
async (filename) => {
|
||||||
@@ -41,10 +51,19 @@ export const useFileNavigation = () => {
|
|||||||
[currentWorkspace, handleFileSelect]
|
[currentWorkspace, handleFileSelect]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reset to default file when workspace changes
|
// Load last opened file when workspace changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
handleFileSelect(null);
|
const initializeFile = async () => {
|
||||||
}, [currentWorkspace, handleFileSelect]);
|
const lastFile = await loadLastOpenedFile();
|
||||||
|
if (lastFile) {
|
||||||
|
handleFileSelect(lastFile);
|
||||||
|
} else {
|
||||||
|
handleFileSelect(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initializeFile();
|
||||||
|
}, [currentWorkspace, loadLastOpenedFile, handleFileSelect]);
|
||||||
|
|
||||||
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };
|
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };
|
||||||
};
|
};
|
||||||
|
|||||||
37
frontend/src/hooks/useLastOpenedFile.js
Normal file
37
frontend/src/hooks/useLastOpenedFile.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
import { getLastOpenedFile, updateLastOpenedFile } from '../services/api';
|
||||||
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
|
|
||||||
|
export const useLastOpenedFile = () => {
|
||||||
|
const { currentWorkspace } = useWorkspace();
|
||||||
|
|
||||||
|
const loadLastOpenedFile = useCallback(async () => {
|
||||||
|
if (!currentWorkspace) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await getLastOpenedFile(currentWorkspace.id);
|
||||||
|
return response.lastOpenedFilePath || null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load last opened file:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}, [currentWorkspace]);
|
||||||
|
|
||||||
|
const saveLastOpenedFile = useCallback(
|
||||||
|
async (filePath) => {
|
||||||
|
if (!currentWorkspace) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await updateLastOpenedFile(currentWorkspace.id, filePath);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to save last opened file:', error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[currentWorkspace]
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
loadLastOpenedFile,
|
||||||
|
saveLastOpenedFile,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user