Skip to content

Commit

Permalink
Ensure metadata overrides file contents (#3512)
Browse files Browse the repository at this point in the history
* ensure metadata overrides file contents

* update more blocks
  • Loading branch information
pablonyx authored Dec 19, 2024
1 parent e9b10e8 commit 817fdc1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
12 changes: 2 additions & 10 deletions web/src/app/chat/documentSidebar/ChatDocumentDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DocumentUpdatedAtBadge } from "@/components/search/DocumentUpdatedAtBad
import { MetadataBadge } from "@/components/MetadataBadge";
import { WebResultIcon } from "@/components/WebResultIcon";
import { Dispatch, SetStateAction } from "react";
import { ValidSources } from "@/lib/types";
import { openDocument } from "@/lib/search/utils";

interface DocumentDisplayProps {
closeSidebar: () => void;
Expand Down Expand Up @@ -73,14 +73,6 @@ export function ChatDocumentDisplay({
return null;
}

const handleViewFile = async () => {
if (document.source_type == ValidSources.File && setPresentingDocument) {
setPresentingDocument(document);
} else if (document.link) {
window.open(document.link, "_blank");
}
};

const hasMetadata =
document.updated_at || Object.keys(document.metadata).length > 0;
return (
Expand All @@ -91,7 +83,7 @@ export function ChatDocumentDisplay({
}`}
>
<button
onClick={handleViewFile}
onClick={() => openDocument(document, setPresentingDocument)}
className="cursor-pointer text-left flex flex-col px-2 py-1.5"
>
<div className="line-clamp-1 mb-1 flex h-6 items-center gap-2 text-xs">
Expand Down
12 changes: 2 additions & 10 deletions web/src/components/chat_search/sources/SourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { WebResultIcon } from "@/components/WebResultIcon";
import { SourceIcon } from "@/components/SourceIcon";
import { OnyxDocument } from "@/lib/search/interfaces";
import { truncateString } from "@/lib/utils";
import { SetStateAction } from "react";
import { Dispatch } from "react";
import { ValidSources } from "@/lib/types";
import { openDocument } from "@/lib/search/utils";

export default function SourceCard({
doc,
Expand All @@ -16,13 +14,7 @@ export default function SourceCard({
return (
<div
key={doc.document_id}
onClick={() => {
if (doc.source_type == ValidSources.File && setPresentingDocument) {
setPresentingDocument(doc);
} else if (doc.link) {
window.open(doc.link, "_blank");
}
}}
onClick={() => openDocument(doc, setPresentingDocument)}
className="cursor-pointer text-left overflow-hidden flex flex-col gap-0.5 rounded-sm px-3 py-2.5 hover:bg-background-125 bg-background-100 w-[200px]"
>
<div className="line-clamp-1 font-semibold text-ellipsis text-text-900 flex h-6 items-center gap-2 text-sm">
Expand Down
11 changes: 4 additions & 7 deletions web/src/components/search/DocumentDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { WarningCircle } from "@phosphor-icons/react";
import TextView from "../chat_search/TextView";
import { SearchResultIcon } from "../SearchResultIcon";
import { ValidSources } from "@/lib/types";
import { openDocument } from "@/lib/search/utils";

export const buildDocumentSummaryDisplay = (
matchHighlights: string[],
Expand Down Expand Up @@ -428,19 +429,15 @@ export function CompactDocumentCard({
url,
updatePresentingDocument,
}: {
document: LoadedOnyxDocument;
document: OnyxDocument;
icon?: React.ReactNode;
url?: string;
updatePresentingDocument: (documentIndex: LoadedOnyxDocument) => void;
updatePresentingDocument: (document: OnyxDocument) => void;
}) {
return (
<div
onClick={() => {
if (document.source_type === ValidSources.File) {
updatePresentingDocument(document);
} else if (document.link) {
window.open(document.link, "_blank");
}
openDocument(document, updatePresentingDocument);
}}
className="max-w-[250px] cursor-pointer pb-0 pt-0 mt-0 flex gap-y-0 flex-col content-start items-start gap-0 "
>
Expand Down
15 changes: 4 additions & 11 deletions web/src/components/search/results/Citation.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ReactNode } from "react";
import { CompactDocumentCard } from "../DocumentDisplay";
import { LoadedOnyxDocument } from "@/lib/search/interfaces";
import { LoadedOnyxDocument, OnyxDocument } from "@/lib/search/interfaces";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { ValidSources } from "@/lib/types";
import { openDocument } from "@/lib/search/utils";

export function Citation({
children,
Expand All @@ -21,7 +22,7 @@ export function Citation({
link?: string;
children?: JSX.Element | string | null | ReactNode;
index?: number;
updatePresentingDocument: (documentIndex: LoadedOnyxDocument) => void;
updatePresentingDocument: (document: OnyxDocument) => void;
document: LoadedOnyxDocument;
icon?: React.ReactNode;
url?: string;
Expand All @@ -30,20 +31,12 @@ export function Citation({
? children?.toString().split("[")[1].split("]")[0]
: index;

const onClick = () => {
if (document.source_type == ValidSources.File) {
updatePresentingDocument(document);
} else {
window.open(link || document.link, "_blank");
}
};

return (
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<div
onMouseDown={onClick}
onClick={() => openDocument(document, updatePresentingDocument)}
className="inline-flex items-center cursor-pointer transition-all duration-200 ease-in-out"
>
<span className="flex items-center justify-center w-6 h-6 text-[11px] font-medium text-gray-700 bg-gray-100 rounded-full border border-gray-300 hover:bg-gray-200 hover:text-gray-900 shadow-sm">
Expand Down
22 changes: 20 additions & 2 deletions web/src/lib/search/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Tag } from "../types";
import { Filters, SourceMetadata } from "./interfaces";
import { Tag, ValidSources } from "../types";
import {
Filters,
LoadedOnyxDocument,
OnyxDocument,
SourceMetadata,
} from "./interfaces";
import { DateRangePickerValue } from "@/app/ee/admin/performance/DateRangeSelector";

export const buildFilters = (
Expand All @@ -22,3 +27,16 @@ export const buildFilters = (
export function endsWithLetterOrNumber(str: string) {
return /[a-zA-Z0-9]$/.test(str);
}

// If we have a link, open it in a new tab (including if it's a file)
// If above fails and we have a file, update the presenting document
export const openDocument = (
document: OnyxDocument,
updatePresentingDocument?: (document: OnyxDocument) => void
) => {
if (document.link) {
window.open(document.link, "_blank");
} else if (document.source_type === ValidSources.File) {
updatePresentingDocument?.(document);
}
};

0 comments on commit 817fdc1

Please sign in to comment.