diff --git a/package-lock.json b/package-lock.json index 2c38379..296ad77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,6 @@ "framer-motion": "^10.16.16", "mdast-util-find-and-replace": "^3.0.1", "reablocks": "^8.4.0", - "react-cool-dimensions": "^3.0.1", "react-markdown": "^9.0.1", "react-syntax-highlighter": "^15.5.0", "reakeys": "^2.0.3", @@ -14936,14 +14935,6 @@ "react-dom": ">=16.8.0" } }, - "node_modules/react-cool-dimensions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/react-cool-dimensions/-/react-cool-dimensions-3.0.1.tgz", - "integrity": "sha512-DUsDB5WUN1Qh6fJJlBtqFKCktrZCPRYcVn8NTeM6hP/5AhZNjDOa2sC2Dg0EM3WUObPDNV5nFLA34vHQfahUeg==", - "peerDependencies": { - "react": ">= 16.8.0" - } - }, "node_modules/react-docgen": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/react-docgen/-/react-docgen-7.0.3.tgz", diff --git a/package.json b/package.json index 74f6543..c364874 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reachat", - "version": "1.0.1", + "version": "1.0.3", "description": "Chat UI for Building LLMs", "scripts": { "build-storybook": "storybook build", @@ -42,7 +42,6 @@ "framer-motion": "^10.16.16", "mdast-util-find-and-replace": "^3.0.1", "reablocks": "^8.4.0", - "react-cool-dimensions": "^3.0.1", "react-markdown": "^9.0.1", "react-syntax-highlighter": "^15.5.0", "reakeys": "^2.0.3", diff --git a/src/Chat.tsx b/src/Chat.tsx index b0d5e2e..952de3b 100644 --- a/src/Chat.tsx +++ b/src/Chat.tsx @@ -13,8 +13,8 @@ import { Session } from './types'; import { ChatTheme, chatTheme } from './theme'; import { ChatContext } from './ChatContext'; import { PluggableList } from 'react-markdown/lib'; -import useDimensions from 'react-cool-dimensions'; import { AnimatePresence } from 'framer-motion'; +import { useDimensions } from './utils/useDimensions'; export interface ChatProps extends PropsWithChildren { /** diff --git a/src/SessionsList/SessionGroups.tsx b/src/SessionsList/SessionGroups.tsx index ff7e30b..ace8b85 100644 --- a/src/SessionsList/SessionGroups.tsx +++ b/src/SessionsList/SessionGroups.tsx @@ -1,5 +1,5 @@ import { FC, ReactNode, useContext, useMemo } from 'react'; -import { GroupedSessions, groupSessionsByDate } from '@/utils'; +import { GroupedSessions, groupSessionsByDate } from '@/utils/grouping'; import { ChatContext } from '@/ChatContext'; export interface SessionGroupsProps { diff --git a/src/utils.spec.ts b/src/utils/grouping.spec.ts similarity index 97% rename from src/utils.spec.ts rename to src/utils/grouping.spec.ts index 68ffd47..ea12193 100644 --- a/src/utils.spec.ts +++ b/src/utils/grouping.spec.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest'; -import { groupSessionsByDate } from './utils'; -import { Session } from './types'; +import { groupSessionsByDate } from './grouping'; +import { Session } from '@/types'; import { subDays } from 'date-fns'; describe('groupSessionsByDate', () => { diff --git a/src/utils.ts b/src/utils/grouping.ts similarity index 98% rename from src/utils.ts rename to src/utils/grouping.ts index dc33304..9c65922 100644 --- a/src/utils.ts +++ b/src/utils/grouping.ts @@ -1,5 +1,5 @@ import { format, isToday, isYesterday, isThisWeek, isThisMonth, isThisYear, parseISO } from 'date-fns'; -import { Session } from './types'; +import { Session } from '@/types'; export interface GroupedSessions { heading: string; diff --git a/src/utils/useDimensions.ts b/src/utils/useDimensions.ts new file mode 100644 index 0000000..ec0ff4d --- /dev/null +++ b/src/utils/useDimensions.ts @@ -0,0 +1,28 @@ +import { useCallback, useEffect, useState } from 'react'; + +export const useDimensions = () => { + const [ref, setRef] = useState(null); + const [width, setWidth] = useState(undefined); + + const observe = useCallback((element: HTMLElement | null) => { + if (element) setRef(element); + }, []); + + useEffect(() => { + if (!ref) return; + + const resizeObserver = new ResizeObserver(entries => { + for (let entry of entries) { + setWidth(entry.contentRect.width); + } + }); + + resizeObserver.observe(ref); + + return () => { + resizeObserver.disconnect(); + }; + }, [ref]); + + return { width, observe }; +};