|
| 1 | +import { WindowControls } from '#components'; |
| 2 | +import { locations } from '#constants'; |
| 3 | +import WindowWrapper from '#hoc/WindowWrapper'; |
| 4 | +import useLocationStore from '#store/location'; |
| 5 | +import useWindowStore from '#store/window'; |
| 6 | +import { clsx } from 'clsx'; |
| 7 | +import { Search } from 'lucide-react'; |
| 8 | +import { FinderItem, WindowID } from '#types'; |
| 9 | + |
| 10 | +const Finder = () => { |
| 11 | + const { openWindow } = useWindowStore(); |
| 12 | + const { activeLocation, setActiveLocation } = useLocationStore(); |
| 13 | + |
| 14 | + const openItem = (item: FinderItem) => { |
| 15 | + if (item.fileType === 'pdf') return openWindow('resume'); |
| 16 | + if (item.kind === 'folder') return setActiveLocation(item); |
| 17 | + if (['fig', 'url'].includes(item.fileType ?? '') && item.href) |
| 18 | + return window.open(item.href, '_blank'); |
| 19 | + |
| 20 | + const windowId = `${item.fileType}${item.kind}` as WindowID; |
| 21 | + openWindow(windowId, item); |
| 22 | + }; |
| 23 | + |
| 24 | + const renderList = (name: string, items: FinderItem[]) => ( |
| 25 | + <div> |
| 26 | + <h3>{name}</h3> |
| 27 | + |
| 28 | + <ul> |
| 29 | + {items.map((item) => ( |
| 30 | + <li |
| 31 | + key={item.id} |
| 32 | + onClick={() => setActiveLocation(item)} |
| 33 | + className={clsx( |
| 34 | + item.id === activeLocation?.id ? 'active' : 'not-active', |
| 35 | + )} |
| 36 | + > |
| 37 | + <img src={item.icon} className="w-4" alt={item.name} /> |
| 38 | + <p className="text-sm font-medium truncate">{item.name}</p> |
| 39 | + </li> |
| 40 | + ))} |
| 41 | + </ul> |
| 42 | + </div> |
| 43 | + ); |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <div id="window-header"> |
| 48 | + <WindowControls target="finder" /> |
| 49 | + <Search className="icon" /> |
| 50 | + </div>{' '} |
| 51 | + <div className="bg-white flex h-full"> |
| 52 | + <div className="sidebar"> |
| 53 | + <ul> |
| 54 | + {renderList('Favorites', Object.values(locations) as FinderItem[])} |
| 55 | + </ul> |
| 56 | + <ul> |
| 57 | + {renderList( |
| 58 | + 'Work', |
| 59 | + (locations.work.children as FinderItem[]) || [], |
| 60 | + )} |
| 61 | + </ul> |
| 62 | + </div> |
| 63 | + |
| 64 | + <ul className="content"> |
| 65 | + {(activeLocation?.children ?? []).map((item) => ( |
| 66 | + <li |
| 67 | + key={item.id} |
| 68 | + className={item.position} |
| 69 | + onClick={() => openItem(item)} |
| 70 | + > |
| 71 | + {' '} |
| 72 | + <img src={item.icon} alt={item.name} /> |
| 73 | + <p> {item.name} </p> |
| 74 | + </li> |
| 75 | + ))} |
| 76 | + </ul> |
| 77 | + </div> |
| 78 | + </> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +const FinderWindow = WindowWrapper(Finder, 'finder'); |
| 83 | + |
| 84 | +export default FinderWindow; |
0 commit comments