Skip to content

Commit

Permalink
Merge pull request #131 from StampyAI/quote-popups
Browse files Browse the repository at this point in the history
Show quotes
  • Loading branch information
mruwnik authored Nov 30, 2023
2 parents 4d602bf + 2920f73 commit 162ad10
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
12 changes: 6 additions & 6 deletions web/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ type State =
response: AssistantEntryType;
};

// smooth-scroll to the bottom of the window if we're already less than 30% a screen away
// smooth-scroll to the bottom of the window if we're already less than 10% a screen away
// note: finicky interaction with "smooth" - maybe fix later.
function scroll30() {
if (
document.documentElement.scrollHeight - window.scrollY >
window.innerHeight * 1.3
)
return;
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
document.documentElement.scrollHeight - window.scrollY <
window.innerHeight * 1.1
) {
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
}
}

const randomQuestion = () =>
Expand Down
48 changes: 36 additions & 12 deletions web/src/components/citations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,50 @@ export const ShowCitation: React.FC<{ citation: Citation }> = ({
);
};

const Popup = ({
children,
content,
}: {
content: string;
children: React.ReactElement;
}) => {
return (
<div className="popup-container">
{children}
<div className="popup">
{content.split("\n").map((v, i) => (
<div className="text-section" key={i}>
{v}
</div>
))}
</div>
</div>
);
};

export const CitationRef: React.FC<{ citation?: Citation }> = ({
citation,
}) => {
if (!citation) return null;

const split = citation.text.split('"""');
const text = split.length === 1 ? citation.text : split[1];
const url =
citation.url && citation.url !== ""
? citation.url
: `https://duckduckgo.com/?q=${encodeURIComponent(citation.title)}`;
return (
<A
className={
Colours[(citation.index - 1) % Colours.length] +
" ml-1 mr-0.5 w-min rounded border-2 px-0.5 pb-0.5 text-sm no-underline"
}
href={url}
>
[{citation.index}]
</A>
<Popup content={text || ""}>
<A
className={
Colours[(citation.index - 1) % Colours.length] +
" ml-1 mr-0.5 w-min rounded border-2 px-0.5 pb-0.5 text-sm no-underline"
}
href={url}
>
[{citation.index}]
</A>
</Popup>
);
};

Expand All @@ -125,8 +150,7 @@ export const CitationsBlock: React.FC<{
}> = ({ text, citations, textRenderer }) => {
const regex = /\[([a-z]+)\]/g;
return (
<p>
{" "}
<div className="text-section">
{text.split(regex).map((part, i) => {
// When splitting, the even parts are basic text sections, while the odd ones are
// citations
Expand All @@ -136,6 +160,6 @@ export const CitationsBlock: React.FC<{
return <CitationRef citation={citations.get(part)} key={i} />;
}
})}
</p>
</div>
);
};
50 changes: 50 additions & 0 deletions web/src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,53 @@ ol {
width: 100%;
border: black solid 1px;
}

/* Tooltip container */
.popup-container {
position: relative;
display: inline-block;
}

/* Tooltip text */
.popup-container .popup {
visibility: hidden;
width: 35em;
background-color: gray;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 10px;

/* Position the tooltip text */
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;

/* Fade in tooltip */
opacity: 0;
transition: opacity 0.3s;
}

/* Tooltip arrow */
.popup-container .popup::after {
content: "";
position: absolute;
top: 100%;
left: 3.8em;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: gray transparent transparent transparent;
}

/* Show the tooltip text when you mouse over the tooltip container */
.popup-container:hover .popup {
visibility: visible;
opacity: 1;
}

.text-section {
padding-bottom: 8px;
}
1 change: 1 addition & 0 deletions web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type Citation = {
date: string;
url: string;
index: number;
text: string;
};

export type Followup = {
Expand Down

0 comments on commit 162ad10

Please sign in to comment.