Skip to content

Commit

Permalink
Feat(widget: search): Add file viewer component
Browse files Browse the repository at this point in the history
  • Loading branch information
leoank committed Jun 3, 2024
1 parent 0181809 commit 382f0da
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 6 deletions.
37 changes: 37 additions & 0 deletions cpgweb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cpgweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@duckdb/react-duckdb": "1.28.1-dev106.0",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-scroll-area": "^1.0.5",
Expand Down
7 changes: 3 additions & 4 deletions cpgweb/src/app/search/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Button } from "../../components/ui/button";
import { Download, Eye, Loader2 } from "lucide-react";
import { get_key_file } from "../../lib/cpg_ops";
import React from "react";
import { FileViewer } from "./file-viewer"

// export const columns: ColumnDef<THit>[] = [
// {
Expand Down Expand Up @@ -93,7 +94,7 @@ export const columns: ColumnDef<THit>[] = [
},
{
accessorKey: "count",
header: "Count",
header: "Hits",
cell: ({ row }) => <div>{row.getValue("count")}</div>,
},
{
Expand Down Expand Up @@ -141,9 +142,7 @@ function ProjectKeyActions(props: TProjectKeyActions) {

return (
<div>
<Button variant="ghost">
<Eye className="mr-2 h-4 w-4" /> View
</Button>
<FileViewer fileName={filename} />
<Button
variant="ghost"
disabled={isDownloadLoading}
Expand Down
48 changes: 48 additions & 0 deletions cpgweb/src/app/search/file-viewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Button } from "@/src/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/src/components/ui/dialog"
import { get_key_file } from "@/src/lib/cpg_ops";
import { Eye } from "lucide-react"
import React from "react";

interface FileViewerProps {
fileName: string,

}

export function FileViewer(props: FileViewerProps) {
const { fileName } = props;
const [viewContent, setViewContent] = React.useState("");

async function fetchData() {
const data = await get_key_file(fileName);
return data || "";
}
return (
<Dialog>
<DialogTrigger asChild>
<Button variant="ghost" onClick={async () => { const data = await fetchData(); setViewContent(data.toString()) }}>
<Eye className="mr-2 h-4 w-4" /> View
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>{fileName.split("/").pop()}</DialogTitle>
<DialogDescription>
{fileName}
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
{viewContent}
</div>
</DialogContent>
</Dialog>
)
}
2 changes: 1 addition & 1 deletion cpgweb/src/app/search/project-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "@/src/components/ui/table";
import { DataTableViewOptions } from "./view";
import { DownloadSearchResult } from "./download";
import Search from "../../components/ui/search/search";
import Search from "@/src/components/ui/search/search";
import { DataTablePagination } from "./pagination";

export type TProjectTableProps = {
Expand Down
7 changes: 6 additions & 1 deletion cpgweb/src/app/search/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
DropdownMenuSeparator,
} from "@/src/components/ui/dropdown-menu";

const viewMap: { [id: string]: string } = {
"value": "project",
"count": "hits"
}

interface DataTableViewOptionsProps<TData> {
table: Table<TData>;
}
Expand Down Expand Up @@ -45,7 +50,7 @@ export function DataTableViewOptions<TData>({
checked={column.getIsVisible()}
onCheckedChange={(value) => column.toggleVisibility(!!value)}
>
{column.id.slice(0, 10)}
{viewMap[column.id.slice(0, 10)]}
</DropdownMenuCheckboxItem>
);
})}
Expand Down
122 changes: 122 additions & 0 deletions cpgweb/src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"use client"

import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"

import { cn } from "@/src/lib/utils"

const Dialog = DialogPrimitive.Root

const DialogTrigger = DialogPrimitive.Trigger

const DialogPortal = DialogPrimitive.Portal

const DialogClose = DialogPrimitive.Close

const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName

const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName

const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"

const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"

const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName

const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName

export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}

0 comments on commit 382f0da

Please sign in to comment.