-
Notifications
You must be signed in to change notification settings - Fork 0
Knowledge Base: Preview functionality #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ayush8923
wants to merge
8
commits into
main
Choose a base branch
from
feat/collection-document-preview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fdd3668
feat(document): collection document preview handling for preview
Ayush8923 551b22c
fix(scrollbar): use the primary color in scrollbar
Ayush8923 8e99eb7
fix(*): remove the unwanted js comments
Ayush8923 9402baf
fix(*): move the stt type inside the types folder
Ayush8923 81b8f38
fix(document): code clenaups and type define
Ayush8923 36a4f5e
fix(clenaups): type define and js comment
Ayush8923 042a0de
Update app/components/knowledge-base/CsvPreview.tsx
Ayush8923 e08ca6d
fix(collection): fix the eslint rule
Ayush8923 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { apiClient } from "@/app/lib/apiClient"; | ||
| import { DocumentDetailEnvelope } from "@/app/lib/types/document"; | ||
|
|
||
| export async function GET( | ||
| request: Request, | ||
| { params }: { params: Promise<{ document_id: string }> }, | ||
| ) { | ||
| const { document_id } = await params; | ||
| try { | ||
| const { data } = await apiClient( | ||
| request, | ||
| `/api/v1/documents/${document_id}?include_url=true`, | ||
| ); | ||
| const detail = (data as DocumentDetailEnvelope) || {}; | ||
| const signedUrl = detail.data?.signed_url || detail.signed_url; | ||
| if (!signedUrl) { | ||
| return NextResponse.json( | ||
| { error: "Document has no signed URL" }, | ||
| { status: 404 }, | ||
| ); | ||
| } | ||
|
|
||
| const upstream = await fetch(signedUrl); | ||
|
Ayush8923 marked this conversation as resolved.
|
||
| if (!upstream.ok) { | ||
| return NextResponse.json( | ||
| { error: `Failed to fetch document (status ${upstream.status})` }, | ||
| { status: upstream.status }, | ||
| ); | ||
| } | ||
|
|
||
| const contentType = | ||
| upstream.headers.get("Content-Type") || "application/octet-stream"; | ||
| return new Response(upstream.body, { | ||
| status: 200, | ||
| headers: { | ||
| "Content-Type": contentType, | ||
| "Cache-Control": "private, max-age=300", | ||
| }, | ||
| }); | ||
| } catch (error: unknown) { | ||
| return NextResponse.json( | ||
| { | ||
| success: false, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useState } from "react"; | ||
| import { Loader } from "@/app/components/ui"; | ||
| import { useAuth } from "@/app/lib/context/AuthContext"; | ||
| import { apiFetchResponse } from "@/app/lib/apiClient"; | ||
| import { parseCsv } from "@/app/lib/utils/csv"; | ||
| import { CsvPreviewProps, ParsedCsv } from "@/app/lib/types/document"; | ||
|
|
||
| export default function CsvPreview({ url }: CsvPreviewProps) { | ||
| const { activeKey } = useAuth(); | ||
| const apiKey = activeKey?.key ?? ""; | ||
| const [data, setData] = useState<ParsedCsv | null>(null); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
| setLoading(true); | ||
| setError(null); | ||
| setData(null); | ||
| apiFetchResponse(url, apiKey) | ||
| .then((r) => { | ||
| if (!r.ok) throw new Error(`Server returned ${r.status}`); | ||
| return r.text(); | ||
| }) | ||
| .then((text) => { | ||
| if (!cancelled) setData(parseCsv(text)); | ||
| }) | ||
| .catch((e: Error) => { | ||
| if (!cancelled) setError(e.message || "Couldn't load CSV"); | ||
| }) | ||
| .finally(() => { | ||
| if (!cancelled) setLoading(false); | ||
| }); | ||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [url, apiKey]); | ||
|
|
||
| if (loading) { | ||
| return ( | ||
| <div className="flex items-center justify-center h-full"> | ||
| <Loader size="md" message="Loading CSV…" /> | ||
| </div> | ||
| ); | ||
| } | ||
| if (error) { | ||
| return ( | ||
| <div className="flex flex-col items-center justify-center h-full gap-2 px-6 text-center"> | ||
| <p className="text-sm text-text-secondary"> | ||
| Couldn't load CSV preview. | ||
| </p> | ||
| <p className="text-xs text-text-secondary">{error}</p> | ||
| </div> | ||
| ); | ||
| } | ||
| if (!data || data.headers.length === 0) { | ||
| return ( | ||
| <div className="flex items-center justify-center h-full"> | ||
| <p className="text-sm text-text-secondary">CSV is empty.</p> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="h-full overflow-auto bg-bg-primary"> | ||
| <table className="w-full text-sm border-collapse"> | ||
| <thead className="sticky top-0 z-10 bg-bg-secondary border-b border-border"> | ||
| <tr> | ||
| {data.headers.map((h, i) => ( | ||
| <th | ||
| key={i} | ||
| className="text-left px-3 py-2 font-semibold text-text-primary border-r border-border last:border-r-0 whitespace-nowrap" | ||
| > | ||
| {h || `Column ${i + 1}`} | ||
| </th> | ||
| ))} | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {data.rows.map((row, ri) => ( | ||
| <tr | ||
| key={ri} | ||
| className="border-b border-border last:border-b-0 hover:bg-bg-secondary/40" | ||
| > | ||
| {data.headers.map((_, ci) => ( | ||
| <td | ||
| key={ci} | ||
| className="px-3 py-2 text-text-secondary border-r border-border last:border-r-0 align-top" | ||
| > | ||
| {row[ci] ?? ""} | ||
| </td> | ||
| ))} | ||
| </tr> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.