Skip to content

Commit

Permalink
feat: top left or top right
Browse files Browse the repository at this point in the history
  • Loading branch information
weilirs committed Jul 18, 2024
1 parent a92ead0 commit 5679cc6
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef } from "react";
import React, { useState, useEffect, useRef, useLayoutEffect } from "react";

import { MessageStreamEvent } from "@anthropic-ai/sdk/resources";
import Button from "@mui/material/Button";
Expand Down Expand Up @@ -32,6 +32,7 @@ const WritingAssistant: React.FC<WritingAssistantProps> = ({
const [customPrompt, setCustomPrompt] = useState<string>("");
const [isOptionsVisible, setIsOptionsVisible] = useState<boolean>(false);
const [prevPrompt, setPrevPrompt] = useState<string>("");
const [positionStyle, setPositionStyle] = useState({ top: 0, left: 0 });
const markdownContainerRef = useRef(null);
const optionsContainerRef = useRef(null);
const hasValidMessages = currentChatHistory?.displayableChatHistory.some(
Expand All @@ -54,6 +55,43 @@ const WritingAssistant: React.FC<WritingAssistantProps> = ({
}
}, [hasValidMessages]);

useLayoutEffect(() => {
if (!isOptionsVisible) return;

const calculatePosition = () => {
if (!optionsContainerRef.current) {
console.log("Ref not attached yet");
return;
}

const screenHeight = window.innerHeight;
const elementHeight = optionsContainerRef.current.offsetHeight;

Check failure on line 68 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Property 'offsetHeight' does not exist on type 'never'.
const spaceBelow = screenHeight - highlightData.position.top;

Check failure on line 69 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

'highlightData.position' is possibly 'null'.
const isSpaceEnough = spaceBelow >= elementHeight;

console.log("Screen Height:", screenHeight);
console.log("Element Height:", elementHeight);
console.log("Space Below:", spaceBelow);
console.log("Is Space Enough:", isSpaceEnough);

if (isSpaceEnough) {
console.log("space enough");
setPositionStyle({
top: highlightData.position.top,

Check failure on line 80 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

'highlightData.position' is possibly 'null'.
left: highlightData.position.left,

Check failure on line 81 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

'highlightData.position' is possibly 'null'.
});
} else {
console.log("space not enough");
setPositionStyle({
top: highlightData.position.top - elementHeight,

Check failure on line 86 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

'highlightData.position' is possibly 'null'.
left: highlightData.position.left,

Check failure on line 87 in src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

'highlightData.position' is possibly 'null'.
});
}
};

calculatePosition();
}, [isOptionsVisible, highlightData.position]);

const copyToClipboard = () => {
if (
!editor ||
Expand Down Expand Up @@ -335,8 +373,8 @@ Write a markdown list (using dashes) of key takeaways from my notes. Write at le
<div
ref={optionsContainerRef}
style={{
top: highlightData.position.top,
left: highlightData.position.left,
top: positionStyle.top,
left: positionStyle.left,
}}
className="absolute bg-white border border-gray-300 p-2.5 z-50 w-96 rounded-md"
>
Expand Down Expand Up @@ -384,8 +422,8 @@ Write a markdown list (using dashes) of key takeaways from my notes. Write at le
ref={markdownContainerRef}
className="absolute bg-white border border-gray-300 rounded-lg shadow-md p-2.5 z-50"
style={{
top: highlightData.position.top,
left: highlightData.position.left,
top: positionStyle.top,
left: positionStyle.left,
width: "385px",
}}
>
Expand Down

0 comments on commit 5679cc6

Please sign in to comment.