feat(ui): style sidebar and landing page (#27)
This commit is contained in:
80
packages/bolt/app/components/sidebar/HistoryItem.tsx
Normal file
80
packages/bolt/app/components/sidebar/HistoryItem.tsx
Normal file
@@ -0,0 +1,80 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
import { db, deleteId, type ChatHistory } from '~/lib/persistence';
|
||||
import { logger } from '~/utils/logger';
|
||||
|
||||
export function HistoryItem({ item, loadEntries }: { item: ChatHistory; loadEntries: () => void }) {
|
||||
const [requestingDelete, setRequestingDelete] = useState(false);
|
||||
const [hovering, setHovering] = useState(false);
|
||||
const hoverRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const deleteItem = useCallback((event: React.UIEvent) => {
|
||||
event.preventDefault();
|
||||
|
||||
if (db) {
|
||||
deleteId(db, item.id)
|
||||
.then(() => loadEntries())
|
||||
.catch((error) => {
|
||||
toast.error('Failed to delete conversation');
|
||||
logger.error(error);
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let timeout: NodeJS.Timeout | undefined;
|
||||
|
||||
function mouseEnter() {
|
||||
setHovering(true);
|
||||
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
function mouseLeave() {
|
||||
setHovering(false);
|
||||
|
||||
// wait for animation to finish before unsetting
|
||||
timeout = setTimeout(() => {
|
||||
setRequestingDelete(false);
|
||||
}, 200);
|
||||
}
|
||||
|
||||
hoverRef.current?.addEventListener('mouseenter', mouseEnter);
|
||||
hoverRef.current?.addEventListener('mouseleave', mouseLeave);
|
||||
|
||||
return () => {
|
||||
hoverRef.current?.removeEventListener('mouseenter', mouseEnter);
|
||||
hoverRef.current?.removeEventListener('mouseleave', mouseLeave);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={hoverRef}
|
||||
className="group rounded-md hover:bg-gray-100 overflow-hidden flex justify-between items-center px-2 py-1"
|
||||
>
|
||||
<a href={`/chat/${item.urlId}`} className="flex w-full relative truncate block">
|
||||
{item.description}
|
||||
<div className="absolute right-0 z-1 top-0 bottom-0 bg-gradient-to-l from-white group-hover:from-gray-100 to-transparent w-10 flex justify-end group-hover:w-15 group-hover:from-45%">
|
||||
{hovering && (
|
||||
<div className="flex items-center p-1">
|
||||
{requestingDelete ? (
|
||||
<button className="i-ph:check text-gray-600 hover:text-gray-1000 scale-110" onClick={deleteItem} />
|
||||
) : (
|
||||
<button
|
||||
className="i-ph:trash text-gray-600 hover:text-gray-1000 scale-110"
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
setRequestingDelete(true);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
114
packages/bolt/app/components/sidebar/Menu.client.tsx
Normal file
114
packages/bolt/app/components/sidebar/Menu.client.tsx
Normal file
@@ -0,0 +1,114 @@
|
||||
import { motion, type Variants } from 'framer-motion';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
import { IconButton } from '~/components/ui/IconButton';
|
||||
import { db, getAll, type ChatHistory } from '~/lib/persistence';
|
||||
import { cubicEasingFn } from '~/utils/easings';
|
||||
import { HistoryItem } from './HistoryItem';
|
||||
import { binDates } from './date-binning';
|
||||
|
||||
const menuVariants = {
|
||||
closed: {
|
||||
opacity: 0,
|
||||
left: '-150px',
|
||||
transition: {
|
||||
duration: 0.2,
|
||||
ease: cubicEasingFn,
|
||||
},
|
||||
},
|
||||
open: {
|
||||
opacity: 1,
|
||||
left: 0,
|
||||
transition: {
|
||||
duration: 0.2,
|
||||
ease: cubicEasingFn,
|
||||
},
|
||||
},
|
||||
} satisfies Variants;
|
||||
|
||||
export function Menu() {
|
||||
const menuRef = useRef<HTMLDivElement>(null);
|
||||
const [list, setList] = useState<ChatHistory[]>([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const loadEntries = useCallback(() => {
|
||||
if (db) {
|
||||
getAll(db)
|
||||
.then((list) => list.filter((item) => item.urlId && item.description))
|
||||
.then(setList)
|
||||
.catch((error) => toast.error(error.message));
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
loadEntries();
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
function onMouseMove(event: MouseEvent) {
|
||||
if (event.pageX < 80) {
|
||||
setOpen(true);
|
||||
}
|
||||
}
|
||||
|
||||
function onMouseLeave(_event: MouseEvent) {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
menuRef.current?.addEventListener('mouseleave', onMouseLeave);
|
||||
window.addEventListener('mousemove', onMouseMove);
|
||||
|
||||
return () => {
|
||||
menuRef.current?.removeEventListener('mouseleave', onMouseLeave);
|
||||
window.removeEventListener('mousemove', onMouseMove);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
ref={menuRef}
|
||||
initial="closed"
|
||||
animate={open ? 'open' : 'closed'}
|
||||
variants={menuVariants}
|
||||
className="flex flex-col side-menu fixed top-0 w-[350px] h-full bg-white border-r rounded-r-3xl border-gray-200 z-max shadow-xl text-sm"
|
||||
>
|
||||
<div className="flex items-center p-4 h-[var(--header-height)]">
|
||||
<img src="/logo_text.svg" width="60px" alt="Bolt Logo" />
|
||||
</div>
|
||||
<div className="flex-1 flex flex-col h-full w-full overflow-hidden">
|
||||
<div className="p-4">
|
||||
<a
|
||||
href="/"
|
||||
className="flex gap-2 items-center text-accent-600 rounded-md bg-accent-600/12 hover:bg-accent-600/15 p-2 font-medium"
|
||||
>
|
||||
<span className="inline-block i-blitz:chat scale-110" />
|
||||
Start new chat
|
||||
</a>
|
||||
</div>
|
||||
<div className="font-semibold pl-6 pr-5 my-2">Your Chats</div>
|
||||
<div className="flex-1 overflow-scroll pl-4 pr-5 pb-5">
|
||||
{list.length === 0 && <div className="pl-2 text-gray">No previous conversations</div>}
|
||||
{binDates(list).map(({ category, items }) => (
|
||||
<div key={category} className="mt-4 first:mt-0 space-y-1">
|
||||
<div className="text-gray sticky top-0 z-20 bg-white pl-2 pt-2 pb-1">{category}</div>
|
||||
{items.map((item) => (
|
||||
<HistoryItem key={item.id} item={item} loadEntries={loadEntries} />
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex items-center border-t p-4">
|
||||
<a href="/logout">
|
||||
<IconButton className="p-1.5 gap-1.5">
|
||||
<>
|
||||
Logout <span className="i-ph:sign-out text-lg" />
|
||||
</>
|
||||
</IconButton>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
59
packages/bolt/app/components/sidebar/date-binning.ts
Normal file
59
packages/bolt/app/components/sidebar/date-binning.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { format, isAfter, isThisWeek, isThisYear, isToday, isYesterday, subDays } from 'date-fns';
|
||||
import type { ChatHistory } from '~/lib/persistence';
|
||||
|
||||
type Bin = { category: string; items: ChatHistory[] };
|
||||
|
||||
export function binDates(_list: ChatHistory[]) {
|
||||
const list = _list.toSorted((a, b) => Date.parse(b.timestamp) - Date.parse(a.timestamp));
|
||||
|
||||
const binLookup: Record<string, Bin> = {};
|
||||
const bins: Array<Bin> = [];
|
||||
|
||||
list.forEach((item) => {
|
||||
const category = dateCategory(new Date(item.timestamp));
|
||||
|
||||
if (!(category in binLookup)) {
|
||||
const bin = {
|
||||
category,
|
||||
items: [item],
|
||||
};
|
||||
|
||||
binLookup[category] = bin;
|
||||
|
||||
bins.push(bin);
|
||||
} else {
|
||||
binLookup[category].items.push(item);
|
||||
}
|
||||
});
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
function dateCategory(date: Date) {
|
||||
if (isToday(date)) {
|
||||
return 'Today';
|
||||
}
|
||||
|
||||
if (isYesterday(date)) {
|
||||
return 'Yesterday';
|
||||
}
|
||||
|
||||
if (isThisWeek(date)) {
|
||||
// e.g., "Monday"
|
||||
return format(date, 'eeee');
|
||||
}
|
||||
|
||||
const thirtyDaysAgo = subDays(new Date(), 30);
|
||||
|
||||
if (isAfter(date, thirtyDaysAgo)) {
|
||||
return 'Last 30 Days';
|
||||
}
|
||||
|
||||
if (isThisYear(date)) {
|
||||
// e.g., "July"
|
||||
return format(date, 'MMMM');
|
||||
}
|
||||
|
||||
// e.g., "July 2023"
|
||||
return format(date, 'MMMM yyyy');
|
||||
}
|
||||
Reference in New Issue
Block a user