mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 13:25:11 +00:00
Add support for showing files outside of conversation view and linking people to manage files in settings
This commit is contained in:
@@ -169,10 +169,7 @@ interface FilesMenuProps {
|
|||||||
|
|
||||||
function FilesMenu(props: FilesMenuProps) {
|
function FilesMenu(props: FilesMenuProps) {
|
||||||
// Use SWR to fetch files
|
// Use SWR to fetch files
|
||||||
const { data: files, error } = useSWR<string[]>(
|
const { data: files, error } = useSWR<string[]>("/api/content/computer", fetcher);
|
||||||
props.conversationId ? "/api/content/computer" : null,
|
|
||||||
fetcher,
|
|
||||||
);
|
|
||||||
const { data: selectedFiles, error: selectedFilesError } = useSWR(
|
const { data: selectedFiles, error: selectedFilesError } = useSWR(
|
||||||
props.conversationId ? `/api/chat/conversation/file-filters/${props.conversationId}` : null,
|
props.conversationId ? `/api/chat/conversation/file-filters/${props.conversationId}` : null,
|
||||||
fetcher,
|
fetcher,
|
||||||
@@ -180,6 +177,7 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [unfilteredFiles, setUnfilteredFiles] = useState<string[]>([]);
|
const [unfilteredFiles, setUnfilteredFiles] = useState<string[]>([]);
|
||||||
const [addedFiles, setAddedFiles] = useState<string[]>([]);
|
const [addedFiles, setAddedFiles] = useState<string[]>([]);
|
||||||
|
const usingConversationContext = props.conversationId !== null;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!files) return;
|
if (!files) return;
|
||||||
@@ -189,7 +187,6 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
let sortedFiles = files;
|
let sortedFiles = files;
|
||||||
|
|
||||||
if (addedFiles) {
|
if (addedFiles) {
|
||||||
console.log("addedFiles in useeffect hook", addedFiles);
|
|
||||||
sortedFiles = addedFiles.concat(
|
sortedFiles = addedFiles.concat(
|
||||||
sortedFiles.filter((filename: string) => !addedFiles.includes(filename)),
|
sortedFiles.filter((filename: string) => !addedFiles.includes(filename)),
|
||||||
);
|
);
|
||||||
@@ -223,12 +220,10 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!props.conversationId) return <></>;
|
|
||||||
|
|
||||||
if (error) return <div>Failed to load files</div>;
|
if (error) return <div>Failed to load files</div>;
|
||||||
if (selectedFilesError) return <div>Failed to load selected files</div>;
|
if (selectedFilesError) return <div>Failed to load selected files</div>;
|
||||||
if (!files) return <InlineLoading />;
|
if (!files) return <InlineLoading />;
|
||||||
if (!selectedFiles) return <InlineLoading />;
|
if (!selectedFiles && props.conversationId) return <InlineLoading />;
|
||||||
|
|
||||||
const FilesMenuCommandBox = () => {
|
const FilesMenuCommandBox = () => {
|
||||||
return (
|
return (
|
||||||
@@ -238,6 +233,7 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
<CommandEmpty>
|
<CommandEmpty>
|
||||||
No results found. <Link href="/search">Try advanced search</Link>.
|
No results found. <Link href="/search">Try advanced search</Link>.
|
||||||
</CommandEmpty>
|
</CommandEmpty>
|
||||||
|
{usingConversationContext && (
|
||||||
<CommandGroup heading="Quick">
|
<CommandGroup heading="Quick">
|
||||||
<CommandItem
|
<CommandItem
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
@@ -256,7 +252,20 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
<span>Select all</span>
|
<span>Select all</span>
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
<CommandGroup heading="Configure files">
|
)}
|
||||||
|
<CommandGroup heading="Manage files">
|
||||||
|
<CommandItem>
|
||||||
|
<Link href="/settings">Settings</Link>
|
||||||
|
</CommandItem>
|
||||||
|
</CommandGroup>
|
||||||
|
<CommandGroup
|
||||||
|
heading={`${usingConversationContext ? "Configure files" : "Synced files"}`}
|
||||||
|
>
|
||||||
|
{addedFiles.length == 0 && (
|
||||||
|
<CommandItem>
|
||||||
|
<Link href="/settings">Upload documents</Link>
|
||||||
|
</CommandItem>
|
||||||
|
)}
|
||||||
{unfilteredFiles.map((filename: string) =>
|
{unfilteredFiles.map((filename: string) =>
|
||||||
addedFiles && addedFiles.includes(filename) ? (
|
addedFiles && addedFiles.includes(filename) ? (
|
||||||
<CommandItem
|
<CommandItem
|
||||||
@@ -264,6 +273,8 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
value={filename}
|
value={filename}
|
||||||
className="bg-accent text-accent-foreground mb-1"
|
className="bg-accent text-accent-foreground mb-1"
|
||||||
onSelect={(value) => {
|
onSelect={(value) => {
|
||||||
|
if (!usingConversationContext) return;
|
||||||
|
|
||||||
modifyFileFilterForConversation(
|
modifyFileFilterForConversation(
|
||||||
props.conversationId,
|
props.conversationId,
|
||||||
[value],
|
[value],
|
||||||
@@ -281,6 +292,8 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
className="mb-1"
|
className="mb-1"
|
||||||
value={filename}
|
value={filename}
|
||||||
onSelect={(value) => {
|
onSelect={(value) => {
|
||||||
|
if (!usingConversationContext) return;
|
||||||
|
|
||||||
modifyFileFilterForConversation(
|
modifyFileFilterForConversation(
|
||||||
props.conversationId,
|
props.conversationId,
|
||||||
[value],
|
[value],
|
||||||
@@ -310,7 +323,9 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
<DrawerHeader>
|
<DrawerHeader>
|
||||||
<DrawerTitle>Files</DrawerTitle>
|
<DrawerTitle>Files</DrawerTitle>
|
||||||
<DrawerDescription>
|
<DrawerDescription>
|
||||||
Manage files for this conversation
|
{usingConversationContext
|
||||||
|
? "Manage files for this conversation"
|
||||||
|
: "Shared files"}
|
||||||
</DrawerDescription>
|
</DrawerDescription>
|
||||||
</DrawerHeader>
|
</DrawerHeader>
|
||||||
<div className={`${styles.panelWrapper}`}>
|
<div className={`${styles.panelWrapper}`}>
|
||||||
@@ -334,13 +349,25 @@ function FilesMenu(props: FilesMenuProps) {
|
|||||||
<div className="w-auto bg-background border border-muted p-4 drop-shadow-sm rounded-2xl my-8">
|
<div className="w-auto bg-background border border-muted p-4 drop-shadow-sm rounded-2xl my-8">
|
||||||
<div className="flex items-center justify-between space-x-4">
|
<div className="flex items-center justify-between space-x-4">
|
||||||
<h4 className="text-sm font-semibold">
|
<h4 className="text-sm font-semibold">
|
||||||
Manage Context
|
{usingConversationContext ? "Mange Context" : "Files"}
|
||||||
<p>
|
<p>
|
||||||
|
{usingConversationContext ? (
|
||||||
<span className="text-muted-foreground text-xs">
|
<span className="text-muted-foreground text-xs">
|
||||||
Using{" "}
|
Using{" "}
|
||||||
{addedFiles.length == 0 ? files.length : addedFiles.length}{" "}
|
{addedFiles.length == 0
|
||||||
|
? files.length
|
||||||
|
: addedFiles.length}{" "}
|
||||||
files
|
files
|
||||||
</span>
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-muted-foreground text-xs">
|
||||||
|
Shared{" "}
|
||||||
|
{addedFiles.length == 0
|
||||||
|
? files.length
|
||||||
|
: addedFiles.length}{" "}
|
||||||
|
files
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</p>
|
</p>
|
||||||
</h4>
|
</h4>
|
||||||
<Button variant="ghost" size="sm" className="w-9 p-0">
|
<Button variant="ghost" size="sm" className="w-9 p-0">
|
||||||
|
|||||||
Reference in New Issue
Block a user