Skip to content

Commit 461f448

Browse files
Make custom hook useuserprofile
1 parent 18140a6 commit 461f448

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

hooks/use-user-profile.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use client';
2+
3+
import { useState, useEffect } from 'react';
4+
import { UserProfile, UserUpdatePayload } from '@/types/user';
5+
import { useLocalStorage } from '@/hooks/use-local-storage';
6+
7+
const DEFAULT_USER: UserProfile = {
8+
id: '1',
9+
name: 'Guest User',
10+
11+
role: 'user',
12+
preferences: {
13+
theme: 'system',
14+
emojiStyle: 'native',
15+
notifications: true,
16+
},
17+
};
18+
19+
export function useUserProfile() {
20+
const [storedUser, setStoredUser] = useLocalStorage<UserProfile>(
21+
'user-profile',
22+
DEFAULT_USER
23+
);
24+
const [user, setUser] = useState<UserProfile>(storedUser);
25+
26+
useEffect(() => {
27+
setStoredUser(user);
28+
}, [user, setStoredUser]);
29+
30+
const updateProfile = (updates: UserUpdatePayload) => {
31+
setUser(prev => ({ ...prev, ...updates }));
32+
};
33+
34+
const updatePreferences = (updates: Partial<UserProfile['preferences']>) => {
35+
setUser(prev => ({
36+
...prev,
37+
preferences: { ...prev.preferences, ...updates },
38+
}));
39+
};
40+
41+
return {
42+
user,
43+
updateProfile,
44+
updatePreferences,
45+
};
46+
}

0 commit comments

Comments
 (0)