Skip to content

Commit

Permalink
Implemented Saving Sidebar State with LocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed May 12, 2024
1 parent 97c939c commit 5e8cec3
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion code/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function App() {
}
/>
<Route
path="documents/:id"
path="/:id"
element={
<>
<Sidebar />
Expand Down
17 changes: 16 additions & 1 deletion code/client/src/ui/components/sidebar/hooks/useSidebarState.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import localStorage from '@/utils/localStorage';

function useSidebarState() {
const [isOpen, setIsOpen] = useState(false);
const [isLocked, setIsLocked] = useState(false);
const [justClosed, setJustClosed] = useState(false);

useEffect(() => {
const sidebarState = localStorage.getItem('sidebarState');
if (sidebarState !== null) {
setIsOpen(sidebarState);
setIsLocked(sidebarState);
}
}, []);

useEffect(() => {
if (isOpen !== isLocked) return;
localStorage.setItem('sidebarState', isOpen);
}, [isOpen, isLocked]);

const handleMouseEnter = () => {
if (justClosed) return;
setIsOpen(true);
Expand All @@ -14,6 +28,7 @@ function useSidebarState() {
setIsLocked(!isLocked && isOpen);
setIsOpen(!isLocked && !isOpen);
setJustClosed(isLocked);
localStorage.setItem('sidebarState', isOpen);
};

const handleMouseLeave = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function DocumentPreview({ document, onDelete, onRename, onDuplicate }: Document
);
return (
<DocumentContextMenu
item={isEditing ? DocumentView : <Link to={`/workspaces/${wid}/documents/${document.id}`}>{DocumentView}</Link>}
item={isEditing ? DocumentView : <Link to={`/workspaces/${wid}/${document.id}`}>{DocumentView}</Link>}
onRename={() => setIsEditing(true)}
onDuplicate={onDuplicate}
onDelete={onDelete}
Expand Down
17 changes: 17 additions & 0 deletions code/client/src/utils/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function setItem(key: string, value: any) {
localStorage.setItem(key, JSON.stringify(value));
}

function getItem(key: string) {
const item = localStorage.getItem(key);
if (!item) {
return null;
}
return JSON.parse(item);
}

function removeItem(key: string) {
localStorage.removeItem(key);
}

export default { setItem, getItem, removeItem };
4 changes: 1 addition & 3 deletions code/server/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ module.exports = {
transform: {},
moduleNameMapper: pathsToModuleNameMapper(
{
/*Controllers*/
'@/*': ['./*'],
'@controllers/*': ['./src/ts/controllers/*'],
/*Databases*/
'@databases/*': ['./src/ts/databases/*'],
/*Others*/
'@src/*': ['./src/ts/*'],
'@domain/*': ['./src/ts/domain/*'],
'@services/*': ['./src/ts/services/*'],
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/databases/ProductionDatabases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgresResourcesDB } from '@databases/resources/postgres/PostgresResourcesDB';
import { PostgresWorkspacesDB } from '@databases/resources/postgres/PostgresWorkspacesDB';
import { PostgresResourcesDB } from '@databases/workspaces/postgres/PostgresResourcesDB';
import { PostgresWorkspacesDB } from '@databases/workspaces/postgres/PostgresWorkspacesDB';
import { DocumentsRepository, Databases, ResourcesRepository, WorkspacesRepository } from '@databases/types';
import { FirestoreDocumentsDB } from '@databases/documents/firestore/FirestoreDocumentsDB';

Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/databases/TestDatabases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocumentsRepository, Databases, ResourcesRepository, WorkspacesRepository } from '@databases/types';
import { MemoryResourcesDB } from '@databases/resources/memory/MemoryResourcesDB';
import { MemoryWorkspacesDB } from '@databases/resources/memory/MemoryWorkspacesDB';
import { MemoryResourcesDB } from '@databases/workspaces/memory/MemoryResourcesDB';
import { MemoryWorkspacesDB } from '@databases/workspaces/memory/MemoryWorkspacesDB';
import { MemoryDocumentsDB } from '@databases/documents/memory/MemoryDocumentsDB';

export class TestDatabases implements Databases {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ResourcesRepository } from '@databases/types';
import { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { memoryDB } from '@databases/resources/memory/Memory';
import { memoryDB } from '@databases/workspaces/memory/Memory';

export class MemoryResourcesDB implements ResourcesRepository {
async createResource(wid: string, name: string, type: ResourceType, parent?: string): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WorkspacesRepository } from '@databases/types';
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace';
import { memoryDB } from '@databases/resources/memory/Memory';
import { memoryDB } from '@databases/workspaces/memory/Memory';
import { WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';

export class MemoryWorkspacesDB implements WorkspacesRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { ResourcesRepository } from '@databases/types';
import { InvalidParameterError, NotFoundError } from '@domain/errors/errors';
import sql from '@databases/resources/postgres/config';
import { isEmpty } from 'lodash';
import sql from '@databases/workspaces/postgres/config';

export class PostgresResourcesDB implements ResourcesRepository {
async createResource(wid: string, name: string, type: ResourceType, parent?: string): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { WorkspaceResource } from '@notespace/shared/src/workspace/types/resource';
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace';
import { NotFoundError } from '@domain/errors/errors';
import sql from '@databases/resources/postgres/config';
import { WorkspacesRepository } from '@databases/types';
import { isEmpty } from 'lodash';
import sql from '@databases/workspaces/postgres/config';

export class PostgresWorkspacesDB implements WorkspacesRepository {
async createWorkspace(name: string): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions code/server/test/testServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import http = require('http');
import cors = require('cors');
import { Server } from 'socket.io';
import { DocumentsService } from '../src/ts/services/DocumentsService';
import eventsInit from '../src/ts/controllers/ws/events';
import router from '../src/ts/controllers/http/router';
import { Services } from '../src/ts/services/Services';
import { TestDatabases } from '../src/ts/databases/TestDatabases';
import config from '../src/ts/config';
import eventsInit from '../src/ts/controllers/ws/events';
import router from '../src/ts/controllers/http/router';
import initSocketEvents from '../src/ts/controllers/ws/initSocketEvents';

function setup() {
Expand Down

0 comments on commit 5e8cec3

Please sign in to comment.