|
| 1 | +import { useRef } from 'react' |
| 2 | +import { RiFileDownloadFill, RiFileUploadFill } from 'react-icons/ri' |
| 3 | +import toast from 'react-simple-toasts' |
| 4 | +import { SettingsContentLayout } from 'src/components/Layout/SettingsContentLayout' |
| 5 | +import { User } from 'src/features/auth' |
| 6 | +import { BookmarkedPost } from 'src/features/bookmarks' |
| 7 | +import { useBookmarks } from 'src/stores/bookmarks' |
| 8 | +import { useAuth } from 'src/stores/user' |
| 9 | +import { BookmarkItem } from './BookmarkSettings' |
| 10 | +import './bookmarkSettings/bookmarkSettings.css' |
| 11 | + |
| 12 | +interface UserInfoProps { |
| 13 | + user: User |
| 14 | +} |
| 15 | + |
| 16 | +const UserInfo = ({ user }: UserInfoProps) => { |
| 17 | + const { logout } = useAuth() |
| 18 | + |
| 19 | + return ( |
| 20 | + <div className="userCard"> |
| 21 | + {user?.imageURL && <img src={user.imageURL} className="userImage"></img>} |
| 22 | + <div className="userInfos"> |
| 23 | + <div className="userName">{user.name}</div> |
| 24 | + <div className="userEmail">{user?.email}</div> |
| 25 | + </div> |
| 26 | + <div className="actions"> |
| 27 | + <button className="logoutBtn" onClick={logout}> |
| 28 | + Logout |
| 29 | + </button> |
| 30 | + <button className="logoutBtn" onClick={() => {}}> |
| 31 | + Delete account |
| 32 | + </button> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +export const ProfileSettings = () => { |
| 39 | + const { user } = useAuth() |
| 40 | + |
| 41 | + const inputFile = useRef<HTMLInputElement | null>(null) |
| 42 | + const { initState, userBookmarks } = useBookmarks() |
| 43 | + |
| 44 | + const importBookmarks = () => { |
| 45 | + inputFile.current?.click() |
| 46 | + } |
| 47 | + |
| 48 | + const exportBookmarks = () => { |
| 49 | + const blob = new Blob([JSON.stringify(userBookmarks, null, 2)], { |
| 50 | + type: 'application/json', |
| 51 | + }) |
| 52 | + const downloadURL = URL.createObjectURL(blob) |
| 53 | + const link = document.createElement('a') |
| 54 | + link.href = downloadURL |
| 55 | + link.download = 'hackertabBookmarks' |
| 56 | + link.click() |
| 57 | + } |
| 58 | + |
| 59 | + const handleFileChange = (event: any) => { |
| 60 | + const file = event.target.files?.[0] |
| 61 | + if (file) { |
| 62 | + const reader = new FileReader() |
| 63 | + reader.onload = () => { |
| 64 | + const importData: BookmarkedPost[] = JSON.parse(reader.result as string) |
| 65 | + const validatedData = importData |
| 66 | + .filter( |
| 67 | + (data: BookmarkedPost) => |
| 68 | + data.title && |
| 69 | + data.url && |
| 70 | + !userBookmarks.some((bm) => bm.title === data.title && bm.url === data.url) |
| 71 | + ) |
| 72 | + .map((data) => ({ |
| 73 | + title: data.title, |
| 74 | + url: data.url, |
| 75 | + source: data.source || '', |
| 76 | + sourceType: data.sourceType || '', |
| 77 | + })) |
| 78 | + initState({ |
| 79 | + userBookmarks: [...userBookmarks, ...validatedData], |
| 80 | + }) |
| 81 | + toast('Your bookmarks have been successfully imported', { theme: 'defaultToast' }) |
| 82 | + } |
| 83 | + reader.readAsText(file) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + <div className="container"> |
| 89 | + {user != null && <UserInfo user={user} />} |
| 90 | + <SettingsContentLayout |
| 91 | + title="Bookmarks" |
| 92 | + description="Find all your bookmarks here. You can remove a bookmark by clicking on the remove icon." |
| 93 | + actions={ |
| 94 | + <> |
| 95 | + <input |
| 96 | + type="file" |
| 97 | + id="file" |
| 98 | + ref={inputFile} |
| 99 | + accept="application/json" |
| 100 | + className="hidden" |
| 101 | + onChange={handleFileChange} |
| 102 | + /> |
| 103 | + <button className="extraBtn extraTextBtn" onClick={() => importBookmarks()}> |
| 104 | + <RiFileUploadFill /> |
| 105 | + Import |
| 106 | + </button> |
| 107 | + <button className="extraBtn" onClick={() => exportBookmarks()}> |
| 108 | + <RiFileDownloadFill /> |
| 109 | + </button> |
| 110 | + </> |
| 111 | + }> |
| 112 | + <div className="bookmarks"> |
| 113 | + {userBookmarks.map((bm) => ( |
| 114 | + <BookmarkItem item={bm} key={bm.url} /> |
| 115 | + ))} |
| 116 | + </div> |
| 117 | + </SettingsContentLayout> |
| 118 | + </div> |
| 119 | + ) |
| 120 | +} |
0 commit comments