Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/experiments' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 26, 2023
2 parents b852a8b + d740d59 commit 340ed8b
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 6 deletions.
157 changes: 157 additions & 0 deletions client/src/plus/ai-help/history.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import useSWR, { KeyedMutator } from "swr";
import { Button } from "../../ui/atoms/button";
import { useEffect } from "react";

function monthYearLabel(date: Date): string {
const formattedDate = date.toLocaleString(undefined, {
month: "short",
year: "numeric",
});
return formattedDate;
}

interface HistoryEntry {
chat_id: string;
label: string;
last: string;
}
interface HistoryEntries {
label: string;
entries: HistoryEntry[];
}

function groupHistory(history) {
const now = new Date();
const today = new Date(now.toDateString());
const yesterday = new Date(
structuredClone(today).setDate(today.getDate() - 1)
);
const last30Days = new Date(
structuredClone(today).setDate(today.getDate() - 30)
);
const groups = [
{ label: "Last 30 Days", d: last30Days },
{ label: "Yesterday", d: yesterday },
{ label: "Today", d: today },
];
const grouped: HistoryEntries[] = [];

let { label = "unknown", d } = groups.pop() || {};
let current: HistoryEntries = { label, entries: [] };
for (const entry of history) {
let last = new Date(entry.last);
while (!d || last < d) {
if (!d) {
label = monthYearLabel(last);
break;
} else if (last < d) {
({ label = "unknown", d } = groups.pop() || {});
continue;
}
break;
}
if (current.label !== label) {
grouped.push(current);
current = { label, entries: [entry] };
} else {
current.entries.push(entry);
}
}

if (current.entries.length) {
grouped.push(current);
}
return grouped;
}

function AIHelpHistorySubList({
currentChatId,
entries,
mutate,
}: {
currentChatId?: string;
entries: HistoryEntries;
mutate: KeyedMutator<any[]>;
}) {
return (
<>
<time>{entries.label}</time>
<ol>
{entries.entries.map(({ chat_id, last, label }, index) => {
return (
<li
key={index}
className={`${
chat_id === currentChatId ? "ai-help-history-active" : ""
}`}
>
<a href={`./ai-help?c=${chat_id}`} title={last}>
{label}
</a>
{chat_id === currentChatId && (
<Button
type="action"
icon="trash"
onClickHandler={async () => {
if (
window.confirm(
"Do you want to permanently delete this Topic?"
)
) {
await fetch(`/api/v1/plus/ai/help/history/${chat_id}`, {
method: "DELETE",
});
mutate();
}
}}
/>
)}
</li>
);
})}
</ol>
</>
);
}

export function AIHelpHistory({
currentChatId,
lastUpdate,
}: {
currentChatId?: string;
lastUpdate: Date;
}) {
const { data, mutate } = useSWR(
`/api/v1/plus/ai/help/history/list`,
async (url) => {
const res = await (await fetch(url)).json();
return Array.isArray(res) ? res : [];
},
{
fallbackData: [],
}
);

useEffect(() => {
mutate();
}, [lastUpdate, mutate]);

return (
<aside className="ai-help-history">
<header>History</header>
<ol>
{groupHistory(data).map((entries, index) => {
return (
<li key={index}>
<AIHelpHistorySubList
entries={entries}
currentChatId={currentChatId}
mutate={mutate}
/>
</li>
);
})}
</ol>
</aside>
);
}
10 changes: 6 additions & 4 deletions client/src/plus/ai-help/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -355,23 +355,25 @@
width: 100%;

&.role-user {
white-space: pre-wrap;
align-items: center;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
white-space: pre-wrap;

.ai-help-message-nav {
display: flex;
justify-content: space-between;

> span {
width: 4rem;
text-align: center;
width: 4rem;
}
}

.ai-help-user-message {
width: 100%;
padding: 0 1rem;
width: 100%;
}
}

Expand Down
1 change: 0 additions & 1 deletion client/src/plus/ai-help/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import ExpandingTextarea from "../../ui/atoms/form/expanding-textarea";
import React from "react";
import { SESSION_KEY } from "../../playground/utils";
import { PlayQueue } from "../../playground/queue";
import useSWR, { KeyedMutator } from "swr";
import { AIHelpHistory } from "./history";

type Category = "apis" | "css" | "html" | "http" | "js" | "learn";
Expand Down
1 change: 0 additions & 1 deletion client/src/plus/ai-help/use-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ export function useAiChat({
content: messageTemplate(query),
});

return;
const eventSource = new SSE(`/api/v1/plus/ai/help`, {
headers: {
"Content-Type": "application/json",
Expand Down

0 comments on commit 340ed8b

Please sign in to comment.