Skip to content

Commit

Permalink
Fix local storage hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Jan 17, 2025
1 parent 4cd9b0e commit 325818b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
21 changes: 12 additions & 9 deletions src/components/planner/sidebar/sessionController/CollabModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ const CollabModal = ({ isOpen, closeModal }: Props) => {
sessionsSocket.sessionId = sessionId;
sessionsSocket.connect('TheCreator')
.then(sessionsSocket => {
const newSession = {
id: sessionsSocket.sessionId,
name: Math.random().toString(36).substr(2, 9),
lastEdited: Date.now(),
expirationTime: new Date(sessionsSocket.sessionInfo['expiration_time']).getTime(),
currentUser: 'TheCreator',
link: `http://localhost:3100/planner?session=${sessionsSocket.sessionId}`,
participants: sessionsSocket.sessionInfo['participants'],
if (sessions.find(session => session.id === sessionsSocket.sessionId) === undefined) {
const newSession = {
id: sessionsSocket.sessionId,
name: Math.random().toString(36).substr(2, 9),
lastEdited: Date.now(),
expirationTime: new Date(sessionsSocket.sessionInfo['expiration_time']).getTime(),
currentUser: 'TheCreator',
link: `http://localhost:3100/planner?session=${sessionsSocket.sessionId}`,
participants: sessionsSocket.sessionInfo['participants'],
}

setSessions(prevSessions => [...prevSessions, newSession]);
}

addSocketListeners(sessionsSocket);
setCurrentSessionId(sessionsSocket.sessionId);
setSessions(prevSessions => [...prevSessions, newSession]);

toast({ title: 'Entrou na sessão', description: 'Convida mais amigos para se juntarem!'});
})
Expand Down
4 changes: 1 addition & 3 deletions src/hooks/useCollabSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { CollabSession } from "../@types";
import useLocalStorage from "./useLocalStorage";

const useCollabSessions = () => {
const [sessions, setSessions] = useLocalStorage('collab-sessions', [] as CollabSession[]);

return [sessions, setSessions];
return useLocalStorage('collab-sessions', [] as CollabSession[]);
}

export default useCollabSessions;
9 changes: 3 additions & 6 deletions src/hooks/useDarkMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { useEffect, useState } from 'react'
import useLocalStorage from './useLocalStorage'

const useDarkMode = () => {
const [enabled, setEnabled] = useLocalStorage('dark-theme')

// @ts-ignore
const isEnabled = typeof enabledState === 'undefined' && enabled
const [enabled, setEnabled] = useLocalStorage('dark-theme', false)

useEffect(() => {
const className = 'dark'
const bodyClass = window.document.body.classList

// eslint-disable-next-line
isEnabled ? bodyClass.add(className) : bodyClass.remove(className)
}, [enabled, isEnabled])
enabled ? bodyClass.add(className) : bodyClass.remove(className)
}, [enabled])

return [enabled, setEnabled]
}
Expand Down
31 changes: 13 additions & 18 deletions src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { useState } from "react"
import { useEffect, useState } from "react"

const useLocalStorage = (key: string, initialValue?: any) => {
const [storedValue, setStoredValue] = useState(() => {
const [value, setValue] = useState(() => {
try {
const item = window.localStorage.getItem(key)
return item ? JSON.parse(item) : initialValue
console.log(initialValue, window.localStorage.getItem(key) || JSON.stringify(initialValue))

Check failure on line 6 in src/hooks/useLocalStorage.ts

View workflow job for this annotation

GitHub Actions / Lint (21.x)

Unexpected console statement
return JSON.parse(
window.localStorage.getItem(key) || JSON.stringify(initialValue)
);
} catch (error) {
console.warn(error)
return initialValue
console.warn(error);
return initialValue;
}
})

const setValue = (value: any) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value

setStoredValue(valueToStore)

window.localStorage.setItem(key, JSON.stringify(valueToStore))
} catch (error) {
console.warn(error)
}
}
return [storedValue, setValue]
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);

return [value, setValue];
}

export default useLocalStorage;

0 comments on commit 325818b

Please sign in to comment.