summaryrefslogtreecommitdiff
path: root/lib/vendor-candidates
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-candidates')
-rw-r--r--lib/vendor-candidates/service.ts360
-rw-r--r--lib/vendor-candidates/table/add-candidates-dialog.tsx327
-rw-r--r--lib/vendor-candidates/table/candidates-table-columns.tsx193
-rw-r--r--lib/vendor-candidates/table/candidates-table-floating-bar.tsx337
-rw-r--r--lib/vendor-candidates/table/candidates-table-toolbar-actions.tsx93
-rw-r--r--lib/vendor-candidates/table/candidates-table.tsx173
-rw-r--r--lib/vendor-candidates/table/delete-candidates-dialog.tsx149
-rw-r--r--lib/vendor-candidates/table/excel-template-download.tsx94
-rw-r--r--lib/vendor-candidates/table/feature-flags-provider.tsx108
-rw-r--r--lib/vendor-candidates/table/feature-flags.tsx96
-rw-r--r--lib/vendor-candidates/table/import-button.tsx211
-rw-r--r--lib/vendor-candidates/table/invite-candidates-dialog.tsx150
-rw-r--r--lib/vendor-candidates/table/update-candidate-sheet.tsx339
-rw-r--r--lib/vendor-candidates/utils.ts40
-rw-r--r--lib/vendor-candidates/validations.ts84
15 files changed, 2754 insertions, 0 deletions
diff --git a/lib/vendor-candidates/service.ts b/lib/vendor-candidates/service.ts
new file mode 100644
index 00000000..68971f18
--- /dev/null
+++ b/lib/vendor-candidates/service.ts
@@ -0,0 +1,360 @@
+"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)
+
+import { vendorCandidates} from "@/db/schema/vendors"
+import { asc, desc, ilike, inArray, and, or, gte, lte, eq, isNull, count } from "drizzle-orm";
+import { revalidateTag, unstable_noStore } from "next/cache";
+import { filterColumns } from "@/lib/filter-columns";
+import { unstable_cache } from "@/lib/unstable-cache";
+import { getErrorMessage } from "@/lib/handle-error";
+import db from "@/db/db";
+import { sendEmail } from "../mail/sendEmail";
+import { CreateVendorCandidateSchema, createVendorCandidateSchema, GetVendorsCandidateSchema, RemoveCandidatesInput, removeCandidatesSchema, updateVendorCandidateSchema, UpdateVendorCandidateSchema } from "./validations";
+import { PgTransaction } from "drizzle-orm/pg-core";
+
+export async function getVendorCandidates(input: GetVendorsCandidateSchema) {
+ return unstable_cache(
+ async () => {
+ try {
+ const offset = (input.page - 1) * input.perPage
+
+ // 1) Advanced filters
+ const advancedWhere = filterColumns({
+ table: vendorCandidates,
+ filters: input.filters,
+ joinOperator: input.joinOperator,
+ })
+
+ // 2) Global search
+ let globalWhere
+ if (input.search) {
+ const s = `%${input.search}%`
+ globalWhere = or(
+ ilike(vendorCandidates.companyName, s),
+ ilike(vendorCandidates.contactEmail, s),
+ ilike(vendorCandidates.contactPhone, s),
+ ilike(vendorCandidates.country, s),
+ ilike(vendorCandidates.source, s),
+ ilike(vendorCandidates.status, s),
+ // etc.
+ )
+ }
+
+ // 3) Combine finalWhere
+ // Example: Only show vendorStatus = "PQ_SUBMITTED"
+ const finalWhere = and(
+ advancedWhere,
+ globalWhere,
+ )
+
+
+
+ // 5) Sorting
+ const orderBy =
+ input.sort && input.sort.length > 0
+ ? input.sort.map((item) =>
+ item.desc
+ ? desc(vendorCandidates[item.id])
+ : asc(vendorCandidates[item.id])
+ )
+ : [desc(vendorCandidates.createdAt)]
+
+ // 6) Query & count
+ const { data, total } = await db.transaction(async (tx) => {
+ // a) Select from the view
+ const candidatesData = await tx
+ .select()
+ .from(vendorCandidates)
+ .where(finalWhere)
+ .orderBy(...orderBy)
+ .offset(offset)
+ .limit(input.perPage)
+
+ // b) Count total
+ const resCount = await tx
+ .select({ count: count() })
+ .from(vendorCandidates)
+ .where(finalWhere)
+
+ return { data: candidatesData, total: resCount[0]?.count }
+ })
+
+ // 7) Calculate pageCount
+ const pageCount = Math.ceil(total / input.perPage)
+
+ // Now 'data' already contains JSON arrays of contacts & items
+ // thanks to the subqueries in the view definition!
+ return { data, pageCount }
+ } catch (err) {
+ console.error(err)
+ return { data: [], pageCount: 0 }
+ }
+ },
+ // Cache key
+ [JSON.stringify(input)],
+ {
+ revalidate: 3600,
+ tags: ["vendor-candidates"],
+ }
+ )()
+}
+
+export async function createVendorCandidate(input: CreateVendorCandidateSchema) {
+ try {
+ // Validate input
+ const validated = createVendorCandidateSchema.parse(input);
+
+ // Insert into database
+ const [newCandidate] = await db
+ .insert(vendorCandidates)
+ .values({
+ companyName: validated.companyName,
+ contactEmail: validated.contactEmail,
+ contactPhone: validated.contactPhone || null,
+ country: validated.country || null,
+ source: validated.source || null,
+ status: validated.status,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ })
+ .returning();
+
+ // Invalidate cache
+ revalidateTag("vendor-candidates");
+
+ return { success: true, data: newCandidate };
+ } catch (error) {
+ console.error("Failed to create vendor candidate:", error);
+ return { success: false, error: getErrorMessage(error) };
+ }
+}
+
+
+// Helper function to group vendor candidates by status
+async function groupVendorCandidatesByStatus( tx: PgTransaction<any, any, any>,) {
+ return tx
+ .select({
+ status: vendorCandidates.status,
+ count: count(),
+ })
+ .from(vendorCandidates)
+ .groupBy(vendorCandidates.status);
+}
+
+/**
+ * Get count of vendor candidates grouped by status
+ */
+export async function getVendorCandidateCounts() {
+ return unstable_cache(
+ async () => {
+ try {
+ // Initialize counts object with all possible statuses set to 0
+ const initial: Record<"COLLECTED" | "INVITED" | "DISCARDED", number> = {
+ COLLECTED: 0,
+ INVITED: 0,
+ DISCARDED: 0,
+ };
+
+ // Execute query within transaction and transform results
+ const result = await db.transaction(async (tx) => {
+ const rows = await groupVendorCandidatesByStatus(tx);
+ return rows.reduce<Record<string, number>>((acc, { status, count }) => {
+ if (status in acc) {
+ acc[status] = count;
+ }
+ return acc;
+ }, initial);
+ });
+
+ return result;
+ } catch (err) {
+ console.error("Failed to get vendor candidate counts:", err);
+ return {
+ COLLECTED: 0,
+ INVITED: 0,
+ DISCARDED: 0,
+ };
+ }
+ },
+ ["vendor-candidate-status-counts"], // Cache key
+ {
+ revalidate: 3600, // Revalidate every hour
+ // tags: ["vendor-candidates"], // Use the same tag as other vendor candidate functions
+ }
+ )();
+}
+
+
+/**
+ * Update a vendor candidate
+ */
+export async function updateVendorCandidate(input: UpdateVendorCandidateSchema) {
+ try {
+ // Validate input
+ const validated = updateVendorCandidateSchema.parse(input);
+
+ // Prepare update data (excluding id)
+ const { id, ...updateData } = validated;
+
+ // Add updatedAt timestamp
+ const dataToUpdate = {
+ ...updateData,
+ updatedAt: new Date(),
+ };
+
+ // Update database
+ const [updatedCandidate] = await db
+ .update(vendorCandidates)
+ .set(dataToUpdate)
+ .where(eq(vendorCandidates.id, id))
+ .returning();
+
+ // If status was updated to "INVITED", send email
+ if (validated.status === "INVITED" && updatedCandidate.contactEmail) {
+ await sendEmail({
+ to: updatedCandidate.contactEmail,
+ subject: "Invitation to Register as a Vendor",
+ template: "vendor-invitation",
+ context: {
+ companyName: updatedCandidate.companyName,
+ language: "en",
+ registrationLink: `${process.env.NEXT_PUBLIC_APP_URL}/en/partners`,
+ }
+ });
+ }
+
+ // Invalidate cache
+ revalidateTag("vendor-candidates");
+
+ return { success: true, data: updatedCandidate };
+ } catch (error) {
+ console.error("Failed to update vendor candidate:", error);
+ return { success: false, error: getErrorMessage(error) };
+ }
+}
+
+/**
+ * Update status of multiple vendor candidates at once
+ */
+export async function bulkUpdateVendorCandidateStatus({
+ ids,
+ status
+}: {
+ ids: number[],
+ status: "COLLECTED" | "INVITED" | "DISCARDED"
+}) {
+ try {
+ // Validate inputs
+ if (!ids.length) {
+ return { success: false, error: "No IDs provided" };
+ }
+
+ if (!["COLLECTED", "INVITED", "DISCARDED"].includes(status)) {
+ return { success: false, error: "Invalid status" };
+ }
+
+ // Get current data of candidates (needed for email sending)
+ const candidatesBeforeUpdate = await db
+ .select()
+ .from(vendorCandidates)
+ .where(inArray(vendorCandidates.id, ids));
+
+ // Update all records
+ const updatedCandidates = await db
+ .update(vendorCandidates)
+ .set({
+ status,
+ updatedAt: new Date(),
+ })
+ .where(inArray(vendorCandidates.id, ids))
+ .returning();
+
+ // If status is "INVITED", send emails to all updated candidates
+ if (status === "INVITED") {
+ const emailPromises = updatedCandidates
+ .filter(candidate => candidate.contactEmail)
+ .map(candidate =>
+ sendEmail({
+ to: candidate.contactEmail!,
+ subject: "Invitation to Register as a Vendor",
+ template: "vendor-invitation",
+ context: {
+ companyName: candidate.companyName,
+ language: "en",
+ registrationLink: `${process.env.NEXT_PUBLIC_APP_URL}/en/partners`,
+ }
+ })
+ );
+
+ // Wait for all emails to be sent
+ await Promise.all(emailPromises);
+ }
+
+ // Invalidate cache
+ revalidateTag("vendor-candidates");
+
+ return {
+ success: true,
+ data: updatedCandidates,
+ count: updatedCandidates.length
+ };
+ } catch (error) {
+ console.error("Failed to bulk update vendor candidates:", error);
+ return { success: false, error: getErrorMessage(error) };
+ }
+}
+
+
+
+
+/**
+ * Remove multiple vendor candidates by their IDs
+ */
+export async function removeCandidates(input: RemoveCandidatesInput) {
+ try {
+ // Validate input
+ const validated = removeCandidatesSchema.parse(input);
+
+ // Get candidates before deletion (for logging purposes)
+ const candidatesBeforeDelete = await db
+ .select({
+ id: vendorCandidates.id,
+ companyName: vendorCandidates.companyName,
+ })
+ .from(vendorCandidates)
+ .where(inArray(vendorCandidates.id, validated.ids));
+
+ // Delete the candidates
+ const deletedCandidates = await db
+ .delete(vendorCandidates)
+ .where(inArray(vendorCandidates.id, validated.ids))
+ .returning({ id: vendorCandidates.id });
+
+ // If no candidates were deleted, return an error
+ if (!deletedCandidates.length) {
+ return {
+ success: false,
+ error: "No candidates were found with the provided IDs",
+ };
+ }
+
+ // Log deletion for audit purposes
+ console.log(
+ `Deleted ${deletedCandidates.length} vendor candidates:`,
+ candidatesBeforeDelete.map(c => `${c.id} (${c.companyName})`)
+ );
+
+ // Invalidate cache
+ revalidateTag("vendor-candidates");
+ revalidateTag("vendor-candidate-status-counts");
+ revalidateTag("vendor-candidate-total-count");
+
+ return {
+ success: true,
+ count: deletedCandidates.length,
+ deletedIds: deletedCandidates.map(c => c.id),
+ };
+ } catch (error) {
+ console.error("Failed to remove vendor candidates:", error);
+ return { success: false, error: getErrorMessage(error) };
+ }
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/add-candidates-dialog.tsx b/lib/vendor-candidates/table/add-candidates-dialog.tsx
new file mode 100644
index 00000000..db475064
--- /dev/null
+++ b/lib/vendor-candidates/table/add-candidates-dialog.tsx
@@ -0,0 +1,327 @@
+"use client"
+
+import * as React from "react"
+import { useForm } from "react-hook-form"
+import { zodResolver } from "@hookform/resolvers/zod"
+import { Check, ChevronsUpDown } from "lucide-react"
+import i18nIsoCountries from "i18n-iso-countries"
+import enLocale from "i18n-iso-countries/langs/en.json"
+import koLocale from "i18n-iso-countries/langs/ko.json"
+import { cn } from "@/lib/utils"
+
+import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog"
+import { Button } from "@/components/ui/button"
+import { Input } from "@/components/ui/input"
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/ui/popover"
+import {
+ Command,
+ CommandEmpty,
+ CommandGroup,
+ CommandInput,
+ CommandItem,
+ CommandList,
+} from "@/components/ui/command"
+
+// react-hook-form + shadcn/ui Form
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form"
+
+// shadcn/ui Select
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select"
+
+import { createVendorCandidateSchema, CreateVendorCandidateSchema } from "../validations"
+import { createVendorCandidate } from "../service"
+import { vendorCandidates } from "@/db/schema/vendors"
+
+// Register locales for countries
+i18nIsoCountries.registerLocale(enLocale)
+i18nIsoCountries.registerLocale(koLocale)
+
+// Generate country array
+const locale = "ko"
+const countryMap = i18nIsoCountries.getNames(locale, { select: "official" })
+const countryArray = Object.entries(countryMap).map(([code, label]) => ({
+ code,
+ label,
+}))
+
+export function AddCandidateDialog() {
+ const [open, setOpen] = React.useState(false)
+ const [isSubmitting, setIsSubmitting] = React.useState(false)
+
+ // react-hook-form 세팅
+ const form = useForm<CreateVendorCandidateSchema>({
+ resolver: zodResolver(createVendorCandidateSchema),
+ defaultValues: {
+ companyName: "",
+ contactEmail: "",
+ contactPhone: "",
+ country: "",
+ source: "",
+ status: "COLLECTED", // Default status set to COLLECTED
+ },
+ })
+
+ async function onSubmit(data: CreateVendorCandidateSchema) {
+ setIsSubmitting(true)
+ try {
+ const result = await createVendorCandidate(data)
+ if (result.error) {
+ alert(`에러: ${result.error}`)
+ return
+ }
+ // 성공 시 모달 닫고 폼 리셋
+ form.reset()
+ setOpen(false)
+ } catch (error) {
+ console.error("Failed to create vendor candidate:", error)
+ alert("An unexpected error occurred")
+ } finally {
+ setIsSubmitting(false)
+ }
+ }
+
+ function handleDialogOpenChange(nextOpen: boolean) {
+ if (!nextOpen) {
+ form.reset()
+ }
+ setOpen(nextOpen)
+ }
+
+ return (
+ <Dialog open={open} onOpenChange={handleDialogOpenChange}>
+ {/* 모달을 열기 위한 버튼 */}
+ <DialogTrigger asChild>
+ <Button variant="default" size="sm">
+ Add Vendor Candidate
+ </Button>
+ </DialogTrigger>
+
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader>
+ <DialogTitle>Create New Vendor Candidate</DialogTitle>
+ <DialogDescription>
+ 새 Vendor Candidate 정보를 입력하고 <b>Create</b> 버튼을 누르세요.
+ </DialogDescription>
+ </DialogHeader>
+
+ {/* shadcn/ui Form을 이용해 react-hook-form과 연결 */}
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)}>
+ <div className="space-y-4 py-4">
+ {/* Company Name 필드 */}
+ <FormField
+ control={form.control}
+ name="companyName"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel className="after:content-['*'] after:ml-0.5 after:text-red-500">
+ Company Name
+ </FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Enter company name"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Contact Email 필드 */}
+ <FormField
+ control={form.control}
+ name="contactEmail"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel className="after:content-['*'] after:ml-0.5 after:text-red-500">
+ Contact Email
+ </FormLabel>
+ <FormControl>
+ <Input
+ placeholder="email@example.com"
+ type="email"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Contact Phone 필드 */}
+ <FormField
+ control={form.control}
+ name="contactPhone"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Contact Phone</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="+82-10-1234-5678"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Country 필드 */}
+ <FormField
+ control={form.control}
+ name="country"
+ render={({ field }) => {
+ const selectedCountry = countryArray.find(
+ (c) => c.code === field.value
+ )
+ return (
+ <FormItem>
+ <FormLabel>Country</FormLabel>
+ <Popover>
+ <PopoverTrigger asChild>
+ <FormControl>
+ <Button
+ variant="outline"
+ role="combobox"
+ className={cn(
+ "w-full justify-between",
+ !field.value && "text-muted-foreground"
+ )}
+ disabled={isSubmitting}
+ >
+ {selectedCountry
+ ? selectedCountry.label
+ : "Select a country"}
+ <ChevronsUpDown className="ml-2 h-4 w-4 opacity-50" />
+ </Button>
+ </FormControl>
+ </PopoverTrigger>
+ <PopoverContent className="w-[300px] p-0">
+ <Command>
+ <CommandInput placeholder="Search country..." />
+ <CommandList>
+ <CommandEmpty>No country found.</CommandEmpty>
+ <CommandGroup className="max-h-[300px] overflow-y-auto">
+ {countryArray.map((country) => (
+ <CommandItem
+ key={country.code}
+ value={country.label}
+ onSelect={() =>
+ field.onChange(country.code)
+ }
+ >
+ <Check
+ className={cn(
+ "mr-2 h-4 w-4",
+ country.code === field.value
+ ? "opacity-100"
+ : "opacity-0"
+ )}
+ />
+ {country.label}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
+ <FormMessage />
+ </FormItem>
+ )
+ }}
+ />
+
+ {/* Source 필드 */}
+ <FormField
+ control={form.control}
+ name="source"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Source</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Where this candidate was found"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Status 필드 */}
+ {/* <FormField
+ control={form.control}
+ name="status"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Status</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ disabled={isSubmitting}
+ >
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="Select status" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectGroup>
+ {vendorCandidates.status.enumValues.map((status) => (
+ <SelectItem key={status} value={status}>
+ {status}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ /> */}
+ </div>
+
+ <DialogFooter>
+ <Button
+ type="button"
+ variant="outline"
+ onClick={() => setOpen(false)}
+ disabled={isSubmitting}
+ >
+ Cancel
+ </Button>
+ <Button type="submit" disabled={isSubmitting}>
+ {isSubmitting ? "Creating..." : "Create"}
+ </Button>
+ </DialogFooter>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/candidates-table-columns.tsx b/lib/vendor-candidates/table/candidates-table-columns.tsx
new file mode 100644
index 00000000..dc014d4e
--- /dev/null
+++ b/lib/vendor-candidates/table/candidates-table-columns.tsx
@@ -0,0 +1,193 @@
+"use client"
+
+import * as React from "react"
+import { type DataTableRowAction } from "@/types/table"
+import { type ColumnDef } from "@tanstack/react-table"
+import { Ellipsis } from "lucide-react"
+import { toast } from "sonner"
+
+import { getErrorMessage } from "@/lib/handle-error"
+import { formatDate } from "@/lib/utils"
+import { Badge } from "@/components/ui/badge"
+import { Button } from "@/components/ui/button"
+import { Checkbox } from "@/components/ui/checkbox"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuRadioGroup,
+ DropdownMenuRadioItem,
+ DropdownMenuSeparator,
+ DropdownMenuShortcut,
+ DropdownMenuSub,
+ DropdownMenuSubContent,
+ DropdownMenuSubTrigger,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+import { DataTableColumnHeader } from "@/components/data-table/data-table-column-header"
+
+import { VendorCandidates, vendorCandidates } from "@/db/schema/vendors"
+import { getCandidateStatusIcon } from "@/lib/vendor-candidates/utils"
+import { candidateColumnsConfig } from "@/config/candidatesColumnsConfig"
+
+interface GetColumnsProps {
+ setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<VendorCandidates> | null>>
+}
+
+/**
+ * tanstack table 컬럼 정의 (중첩 헤더 버전)
+ */
+export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<VendorCandidates>[] {
+ // ----------------------------------------------------------------
+ // 1) select 컬럼 (체크박스)
+ // ----------------------------------------------------------------
+ const selectColumn: ColumnDef<VendorCandidates> = {
+ id: "select",
+ header: ({ table }) => (
+ <Checkbox
+ checked={
+ table.getIsAllPageRowsSelected() ||
+ (table.getIsSomePageRowsSelected() && "indeterminate")
+ }
+ onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
+ aria-label="Select all"
+ className="translate-y-0.5"
+ />
+ ),
+ cell: ({ row }) => (
+ <Checkbox
+ checked={row.getIsSelected()}
+ onCheckedChange={(value) => row.toggleSelected(!!value)}
+ aria-label="Select row"
+ className="translate-y-0.5"
+ />
+ ),
+ size:40,
+ enableSorting: false,
+ enableHiding: false,
+ }
+
+ // ----------------------------------------------------------------
+ // 2) actions 컬럼 (Dropdown 메뉴)
+ // ----------------------------------------------------------------
+ const actionsColumn: ColumnDef<VendorCandidates> = {
+ id: "actions",
+ enableHiding: false,
+ cell: function Cell({ row }) {
+ const [isUpdatePending, startUpdateTransition] = React.useTransition()
+
+ return (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ aria-label="Open menu"
+ variant="ghost"
+ className="flex size-8 p-0 data-[state=open]:bg-muted"
+ >
+ <Ellipsis className="size-4" aria-hidden="true" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-40">
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "update" })}
+ >
+ Edit
+ </DropdownMenuItem>
+
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "delete" })}
+ >
+ Delete
+ <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )
+ },
+ size: 40,
+ }
+
+ // ----------------------------------------------------------------
+ // 3) 일반 컬럼들을 "그룹"별로 묶어 중첩 columns 생성
+ // ----------------------------------------------------------------
+ // 3-1) groupMap: { [groupName]: ColumnDef<VendorCandidates>[] }
+ const groupMap: Record<string, ColumnDef<VendorCandidates>[]> = {}
+
+ candidateColumnsConfig.forEach((cfg) => {
+ // 만약 group가 없으면 "_noGroup" 처리
+ const groupName = cfg.group || "_noGroup"
+
+ if (!groupMap[groupName]) {
+ groupMap[groupName] = []
+ }
+
+ // child column 정의
+ const childCol: ColumnDef<VendorCandidates> = {
+ accessorKey: cfg.id,
+ enableResizing: true,
+ header: ({ column }) => (
+ <DataTableColumnHeader column={column} title={cfg.label} />
+ ),
+ meta: {
+ excelHeader: cfg.excelHeader,
+ group: cfg.group,
+ type: cfg.type,
+ },
+ cell: ({ row, cell }) => {
+
+ if (cfg.id === "status") {
+ const statusVal = row.original.status
+ if (!statusVal) return null
+ const Icon = getCandidateStatusIcon(statusVal)
+ return (
+ <div className="flex w-[6.25rem] items-center">
+ <Icon className="mr-2 size-4 text-muted-foreground" aria-hidden="true" />
+ <span className="capitalize">{statusVal}</span>
+ </div>
+ )
+ }
+
+
+ if (cfg.id === "createdAt") {
+ const dateVal = cell.getValue() as Date
+ return formatDate(dateVal)
+ }
+
+ // code etc...
+ return row.getValue(cfg.id) ?? ""
+ },
+ }
+
+ groupMap[groupName].push(childCol)
+ })
+
+ // ----------------------------------------------------------------
+ // 3-2) groupMap에서 실제 상위 컬럼(그룹)을 만들기
+ // ----------------------------------------------------------------
+ const nestedColumns: ColumnDef<VendorCandidates>[] = []
+
+ // 순서를 고정하고 싶다면 group 순서를 미리 정의하거나 sort해야 함
+ // 여기서는 그냥 Object.entries 순서
+ Object.entries(groupMap).forEach(([groupName, colDefs]) => {
+ if (groupName === "_noGroup") {
+ // 그룹 없음 → 그냥 최상위 레벨 컬럼
+ nestedColumns.push(...colDefs)
+ } else {
+ // 상위 컬럼
+ nestedColumns.push({
+ id: groupName,
+ header: groupName, // "Basic Info", "Metadata" 등
+ columns: colDefs,
+ })
+ }
+ })
+
+ // ----------------------------------------------------------------
+ // 4) 최종 컬럼 배열: select, nestedColumns, actions
+ // ----------------------------------------------------------------
+ return [
+ selectColumn,
+ ...nestedColumns,
+ actionsColumn,
+ ]
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/candidates-table-floating-bar.tsx b/lib/vendor-candidates/table/candidates-table-floating-bar.tsx
new file mode 100644
index 00000000..2696292d
--- /dev/null
+++ b/lib/vendor-candidates/table/candidates-table-floating-bar.tsx
@@ -0,0 +1,337 @@
+"use client"
+
+import * as React from "react"
+import { SelectTrigger } from "@radix-ui/react-select"
+import { type Table } from "@tanstack/react-table"
+import {
+ ArrowUp,
+ CheckCircle2,
+ Download,
+ Loader,
+ Trash2,
+ X,
+ Mail,
+} from "lucide-react"
+import { toast } from "sonner"
+
+import { exportTableToExcel } from "@/lib/export"
+import { Button } from "@/components/ui/button"
+import { Portal } from "@/components/ui/portal"
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+} from "@/components/ui/select"
+import { Separator } from "@/components/ui/separator"
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip"
+import { Kbd } from "@/components/kbd"
+
+import { ActionConfirmDialog } from "@/components/ui/action-dialog"
+import { vendorCandidates, VendorCandidates } from "@/db/schema/vendors"
+import { bulkUpdateVendorCandidateStatus, removeCandidates, updateVendorCandidate } from "../service"
+
+interface CandidatesTableFloatingBarProps {
+ table: Table<VendorCandidates>
+}
+
+export function VendorCandidateTableFloatingBar({ table }: CandidatesTableFloatingBarProps) {
+ const rows = table.getFilteredSelectedRowModel().rows
+
+ const [isPending, startTransition] = React.useTransition()
+ const [action, setAction] = React.useState<
+ "update-status" | "export" | "delete" | "invite"
+ >()
+ const [popoverOpen, setPopoverOpen] = React.useState(false)
+
+ // Clear selection on Escape key press
+ React.useEffect(() => {
+ function handleKeyDown(event: KeyboardEvent) {
+ if (event.key === "Escape") {
+ table.toggleAllRowsSelected(false)
+ }
+ }
+
+ window.addEventListener("keydown", handleKeyDown)
+ return () => window.removeEventListener("keydown", handleKeyDown)
+ }, [table])
+
+ // 공용 confirm dialog state
+ const [confirmDialogOpen, setConfirmDialogOpen] = React.useState(false)
+ const [confirmProps, setConfirmProps] = React.useState<{
+ title: string
+ description?: string
+ onConfirm: () => Promise<void> | void
+ }>({
+ title: "",
+ description: "",
+ onConfirm: () => { },
+ })
+
+ // 1) "삭제" Confirm 열기
+ function handleDeleteConfirm() {
+ setAction("delete")
+ setConfirmProps({
+ title: `Delete ${rows.length} user${rows.length > 1 ? "s" : ""}?`,
+ description: "This action cannot be undone.",
+ onConfirm: async () => {
+ startTransition(async () => {
+ const { error } = await removeCandidates({
+ ids: rows.map((row) => row.original.id),
+ })
+ if (error) {
+ toast.error(error)
+ return
+ }
+ toast.success("Users deleted")
+ table.toggleAllRowsSelected(false)
+ setConfirmDialogOpen(false)
+ })
+ },
+ })
+ setConfirmDialogOpen(true)
+ }
+
+ // 2) 상태 업데이트
+ function handleSelectStatus(newStatus: VendorCandidates["status"]) {
+ setAction("update-status")
+
+ setConfirmProps({
+ title: `Update ${rows.length} candidate${rows.length > 1 ? "s" : ""} with status: ${newStatus}?`,
+ description: "This action will override their current status.",
+ onConfirm: async () => {
+ startTransition(async () => {
+ const { error } = await bulkUpdateVendorCandidateStatus({
+ ids: rows.map((row) => row.original.id),
+ status: newStatus,
+ })
+ if (error) {
+ toast.error(error)
+ return
+ }
+ toast.success("Candidates updated")
+ setConfirmDialogOpen(false)
+ })
+ },
+ })
+ setConfirmDialogOpen(true)
+ }
+
+ // 3) 초대하기 (INVITED 상태로 바꾸고 이메일 전송)
+ function handleInvite() {
+ setAction("invite")
+ setConfirmProps({
+ title: `Invite ${rows.length} candidate${rows.length > 1 ? "s" : ""}?`,
+ description: "This will change their status to INVITED and send invitation emails.",
+ onConfirm: async () => {
+ startTransition(async () => {
+ const { error } = await bulkUpdateVendorCandidateStatus({
+ ids: rows.map((row) => row.original.id),
+ status: "INVITED",
+ })
+ if (error) {
+ toast.error(error)
+ return
+ }
+ toast.success("Invitation emails sent successfully")
+ table.toggleAllRowsSelected(false)
+ setConfirmDialogOpen(false)
+ })
+ },
+ })
+ setConfirmDialogOpen(true)
+ }
+
+ return (
+ <Portal >
+ <div className="fixed inset-x-0 bottom-10 z-50 mx-auto w-fit px-2.5" style={{ bottom: '1.5rem' }}>
+ <div className="w-full overflow-x-auto">
+ <div className="mx-auto flex w-fit items-center gap-2 rounded-md border bg-background p-2 text-foreground shadow">
+ <div className="flex h-7 items-center rounded-md border border-dashed pl-2.5 pr-1">
+ <span className="whitespace-nowrap text-xs">
+ {rows.length} selected
+ </span>
+ <Separator orientation="vertical" className="ml-2 mr-1" />
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="size-5 hover:border"
+ onClick={() => table.toggleAllRowsSelected(false)}
+ >
+ <X className="size-3.5 shrink-0" aria-hidden="true" />
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent className="flex items-center border bg-accent px-2 py-1 font-semibold text-foreground dark:bg-zinc-900">
+ <p className="mr-2">Clear selection</p>
+ <Kbd abbrTitle="Escape" variant="outline">
+ Esc
+ </Kbd>
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ <Separator orientation="vertical" className="hidden h-5 sm:block" />
+ <div className="flex items-center gap-1.5">
+ {/* 초대하기 버튼 (새로 추가) */}
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="secondary"
+ size="sm"
+ className="h-7 border"
+ onClick={handleInvite}
+ disabled={isPending}
+ >
+ {isPending && action === "invite" ? (
+ <Loader
+ className="mr-1 size-3.5 animate-spin"
+ aria-hidden="true"
+ />
+ ) : (
+ <Mail className="mr-1 size-3.5" aria-hidden="true" />
+ )}
+ <span>Invite</span>
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent className="border bg-accent font-semibold text-foreground dark:bg-zinc-900">
+ <p>Send invitation emails</p>
+ </TooltipContent>
+ </Tooltip>
+
+ <Select
+ onValueChange={(value: VendorCandidates["status"]) => {
+ handleSelectStatus(value)
+ }}
+ >
+ <Tooltip>
+ <SelectTrigger asChild>
+ <TooltipTrigger asChild>
+ <Button
+ variant="secondary"
+ size="icon"
+ className="size-7 border data-[state=open]:bg-accent data-[state=open]:text-accent-foreground"
+ disabled={isPending}
+ >
+ {isPending && action === "update-status" ? (
+ <Loader
+ className="size-3.5 animate-spin"
+ aria-hidden="true"
+ />
+ ) : (
+ <CheckCircle2
+ className="size-3.5"
+ aria-hidden="true"
+ />
+ )}
+ </Button>
+ </TooltipTrigger>
+ </SelectTrigger>
+ <TooltipContent className="border bg-accent font-semibold text-foreground dark:bg-zinc-900">
+ <p>Update status</p>
+ </TooltipContent>
+ </Tooltip>
+ <SelectContent align="center">
+ <SelectGroup>
+ {vendorCandidates.status.enumValues.map((status) => (
+ <SelectItem
+ key={status}
+ value={status}
+ className="capitalize"
+ >
+ {status}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="secondary"
+ size="icon"
+ className="size-7 border"
+ onClick={() => {
+ setAction("export")
+
+ startTransition(() => {
+ exportTableToExcel(table, {
+ excludeColumns: ["select", "actions"],
+ onlySelected: true,
+ })
+ })
+ }}
+ disabled={isPending}
+ >
+ {isPending && action === "export" ? (
+ <Loader
+ className="size-3.5 animate-spin"
+ aria-hidden="true"
+ />
+ ) : (
+ <Download className="size-3.5" aria-hidden="true" />
+ )}
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent className="border bg-accent font-semibold text-foreground dark:bg-zinc-900">
+ <p>Export candidates</p>
+ </TooltipContent>
+ </Tooltip>
+
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="secondary"
+ size="icon"
+ className="size-7 border"
+ onClick={handleDeleteConfirm}
+ disabled={isPending}
+ >
+ {isPending && action === "delete" ? (
+ <Loader
+ className="size-3.5 animate-spin"
+ aria-hidden="true"
+ />
+ ) : (
+ <Trash2 className="size-3.5" aria-hidden="true" />
+ )}
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent className="border bg-accent font-semibold text-foreground dark:bg-zinc-900">
+ <p>Delete candidates</p>
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ {/* 공용 Confirm Dialog */}
+ <ActionConfirmDialog
+ open={confirmDialogOpen}
+ onOpenChange={setConfirmDialogOpen}
+ title={confirmProps.title}
+ description={confirmProps.description}
+ onConfirm={confirmProps.onConfirm}
+ isLoading={isPending && (action === "delete" || action === "update-status" || action === "invite")}
+ confirmLabel={
+ action === "delete"
+ ? "Delete"
+ : action === "update-status"
+ ? "Update"
+ : action === "invite"
+ ? "Invite"
+ : "Confirm"
+ }
+ confirmVariant={
+ action === "delete" ? "destructive" : "default"
+ }
+ />
+ </Portal>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/candidates-table-toolbar-actions.tsx b/lib/vendor-candidates/table/candidates-table-toolbar-actions.tsx
new file mode 100644
index 00000000..a2229a54
--- /dev/null
+++ b/lib/vendor-candidates/table/candidates-table-toolbar-actions.tsx
@@ -0,0 +1,93 @@
+"use client"
+
+import * as React from "react"
+import { type Table } from "@tanstack/react-table"
+import { Download, FileDown, Upload } from "lucide-react"
+
+import { exportTableToExcel } from "@/lib/export"
+import { Button } from "@/components/ui/button"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+
+import { AddCandidateDialog } from "./add-candidates-dialog"
+import { VendorCandidates } from "@/db/schema/vendors"
+import { DeleteCandidatesDialog } from "./delete-candidates-dialog"
+import { InviteCandidatesDialog } from "./invite-candidates-dialog"
+import { ImportVendorCandidatesButton } from "./import-button"
+import { exportVendorCandidateTemplate } from "./excel-template-download"
+
+
+interface CandidatesTableToolbarActionsProps {
+ table: Table<VendorCandidates>
+}
+
+export function CandidatesTableToolbarActions({ table }: CandidatesTableToolbarActionsProps) {
+ const selectedRows = table.getFilteredSelectedRowModel().rows
+ const hasSelection = selectedRows.length > 0
+ const [refreshKey, setRefreshKey] = React.useState(0)
+
+ // Handler to refresh the table after import
+ const handleImportSuccess = () => {
+ // Trigger a refresh of the table data
+ setRefreshKey(prev => prev + 1)
+ }
+
+ return (
+ <div className="flex items-center gap-2">
+ {/* Show actions only when rows are selected */}
+ {hasSelection ? (
+ <>
+ {/* Invite dialog - new addition */}
+ <InviteCandidatesDialog
+ candidates={selectedRows.map((row) => row.original)}
+ onSuccess={() => table.toggleAllRowsSelected(false)}
+ />
+
+ {/* Delete dialog */}
+ <DeleteCandidatesDialog
+ candidates={selectedRows.map((row) => row.original)}
+ onSuccess={() => table.toggleAllRowsSelected(false)}
+ />
+ </>
+ ) : null}
+
+ {/* Add new candidate dialog */}
+ <AddCandidateDialog />
+
+ {/* Import Excel button */}
+ <ImportVendorCandidatesButton onSuccess={handleImportSuccess} />
+
+ {/* Export dropdown menu */}
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Download className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Export</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() => {
+ exportTableToExcel(table, {
+ filename: "vendor-candidates",
+ excludeColumns: ["select", "actions"],
+ useGroupHeader: false,
+ })
+ }}
+ >
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>Export Current Data</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem onClick={exportVendorCandidateTemplate}>
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>Download Template</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/candidates-table.tsx b/lib/vendor-candidates/table/candidates-table.tsx
new file mode 100644
index 00000000..2c01733c
--- /dev/null
+++ b/lib/vendor-candidates/table/candidates-table.tsx
@@ -0,0 +1,173 @@
+"use client"
+
+import * as React from "react"
+import type {
+ DataTableAdvancedFilterField,
+ DataTableFilterField,
+ DataTableRowAction,
+} from "@/types/table"
+
+import { toSentenceCase } from "@/lib/utils"
+import { useDataTable } from "@/hooks/use-data-table"
+import { DataTable } from "@/components/data-table/data-table"
+import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
+import { DataTableToolbar } from "@/components/data-table/data-table-toolbar"
+
+import { useFeatureFlags } from "./feature-flags-provider"
+import { getVendorCandidateCounts, getVendorCandidates } from "../service"
+import { VendorCandidates, vendorCandidates } from "@/db/schema/vendors"
+import { VendorCandidateTableFloatingBar } from "./candidates-table-floating-bar"
+import { getColumns } from "./candidates-table-columns"
+import { CandidatesTableToolbarActions } from "./candidates-table-toolbar-actions"
+import { DeleteCandidatesDialog } from "./delete-candidates-dialog"
+import { UpdateCandidateSheet } from "./update-candidate-sheet"
+import { getCandidateStatusIcon } from "@/lib/vendor-candidates/utils"
+
+interface VendorCandidatesTableProps {
+ promises: Promise<
+ [
+ Awaited<ReturnType<typeof getVendorCandidates>>,
+ Awaited<ReturnType<typeof getVendorCandidateCounts>>,
+ ]
+ >
+}
+
+export function VendorCandidateTable({ promises }: VendorCandidatesTableProps) {
+ const { featureFlags } = useFeatureFlags()
+
+ const [{ data, pageCount }, statusCounts] =
+ React.use(promises)
+
+
+
+ const [rowAction, setRowAction] =
+ React.useState<DataTableRowAction<VendorCandidates> | null>(null)
+
+ const columns = React.useMemo(
+ () => getColumns({ setRowAction }),
+ [setRowAction]
+ )
+
+ /**
+ * This component can render either a faceted filter or a search filter based on the `options` prop.
+ *
+ * @prop options - An array of objects, each representing a filter option. If provided, a faceted filter is rendered. If not, a search filter is rendered.
+ *
+ * Each `option` object has the following properties:
+ * @prop {string} label - The label for the filter option.
+ * @prop {string} value - The value for the filter option.
+ * @prop {React.ReactNode} [icon] - An optional icon to display next to the label.
+ * @prop {boolean} [withCount] - An optional boolean to display the count of the filter option.
+ */
+ const filterFields: DataTableFilterField<VendorCandidates>[] = [
+
+ {
+ id: "status",
+ label: "Status",
+ options: vendorCandidates.status.enumValues.map((status) => ({
+ label: toSentenceCase(status),
+ value: status,
+ count: statusCounts[status],
+ })),
+ },
+
+ ]
+
+ /**
+ * Advanced filter fields for the data table.
+ * These fields provide more complex filtering options compared to the regular filterFields.
+ *
+ * Key differences from regular filterFields:
+ * 1. More field types: Includes 'text', 'multi-select', 'date', and 'boolean'.
+ * 2. Enhanced flexibility: Allows for more precise and varied filtering options.
+ * 3. Used with DataTableAdvancedToolbar: Enables a more sophisticated filtering UI.
+ * 4. Date and boolean types: Adds support for filtering by date ranges and boolean values.
+ */
+ const advancedFilterFields: DataTableAdvancedFilterField<VendorCandidates>[] = [
+ {
+ id: "companyName",
+ label: "Company Name",
+ type: "text",
+ },
+ {
+ id: "contactEmail",
+ label: "Contact Email",
+ type: "text",
+ },
+ {
+ id: "contactPhone",
+ label: "Contact Phone",
+ type: "text",
+ },
+ {
+ id: "source",
+ label: "source",
+ type: "text",
+ },
+ {
+ id: "status",
+ label: "Status",
+ type: "multi-select",
+ options: vendorCandidates.status.enumValues.map((status) => ({
+ label: toSentenceCase(status),
+ value: status,
+ icon: getCandidateStatusIcon(status),
+ count: statusCounts[status],
+ })),
+ },
+
+ {
+ id: "createdAt",
+ label: "Created at",
+ type: "date",
+ },
+ ]
+
+
+ const { table } = useDataTable({
+ data,
+ columns,
+ pageCount,
+ filterFields,
+ enablePinning: true,
+ enableAdvancedFilter: true,
+ initialState: {
+ sorting: [{ id: "createdAt", desc: true }],
+ columnPinning: { right: ["actions"] },
+ },
+ getRowId: (originalRow) => String(originalRow.id),
+ shallow: false,
+ clearOnDefault: true,
+ })
+
+ return (
+ <>
+ <DataTable
+ table={table}
+ floatingBar={<VendorCandidateTableFloatingBar table={table} />}
+ >
+
+ <DataTableAdvancedToolbar
+ table={table}
+ filterFields={advancedFilterFields}
+ shallow={false}
+ >
+ <CandidatesTableToolbarActions table={table} />
+ </DataTableAdvancedToolbar>
+
+ </DataTable>
+ <UpdateCandidateSheet
+ open={rowAction?.type === "update"}
+ onOpenChange={() => setRowAction(null)}
+ candidate={rowAction?.row.original ?? null}
+ />
+ <DeleteCandidatesDialog
+ open={rowAction?.type === "delete"}
+ onOpenChange={() => setRowAction(null)}
+ candidates={rowAction?.row.original ? [rowAction?.row.original] : []}
+ showTrigger={false}
+ onSuccess={() => rowAction?.row.toggleSelected(false)}
+ />
+ </>
+ )
+}
diff --git a/lib/vendor-candidates/table/delete-candidates-dialog.tsx b/lib/vendor-candidates/table/delete-candidates-dialog.tsx
new file mode 100644
index 00000000..e9fabf76
--- /dev/null
+++ b/lib/vendor-candidates/table/delete-candidates-dialog.tsx
@@ -0,0 +1,149 @@
+"use client"
+
+import * as React from "react"
+import { type Row } from "@tanstack/react-table"
+import { Loader, Trash } from "lucide-react"
+import { toast } from "sonner"
+
+import { useMediaQuery } from "@/hooks/use-media-query"
+import { Button } from "@/components/ui/button"
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog"
+import {
+ Drawer,
+ DrawerClose,
+ DrawerContent,
+ DrawerDescription,
+ DrawerFooter,
+ DrawerHeader,
+ DrawerTitle,
+ DrawerTrigger,
+} from "@/components/ui/drawer"
+
+import { VendorCandidates } from "@/db/schema/vendors"
+import { removeCandidates } from "../service"
+
+interface DeleteCandidatesDialogProps
+ extends React.ComponentPropsWithoutRef<typeof Dialog> {
+ candidates: Row<VendorCandidates>["original"][]
+ showTrigger?: boolean
+ onSuccess?: () => void
+}
+
+export function DeleteCandidatesDialog({
+ candidates,
+ showTrigger = true,
+ onSuccess,
+ ...props
+}: DeleteCandidatesDialogProps) {
+ const [isDeletePending, startDeleteTransition] = React.useTransition()
+ const isDesktop = useMediaQuery("(min-width: 640px)")
+
+ function onDelete() {
+ startDeleteTransition(async () => {
+ const { error } = await removeCandidates({
+ ids: candidates.map((candidate) => candidate.id),
+ })
+
+ if (error) {
+ toast.error(error)
+ return
+ }
+
+ props.onOpenChange?.(false)
+ toast.success("Candidates deleted")
+ onSuccess?.()
+ })
+ }
+
+ if (isDesktop) {
+ return (
+ <Dialog {...props}>
+ {showTrigger ? (
+ <DialogTrigger asChild>
+ <Button variant="outline" size="sm">
+ <Trash className="mr-2 size-4" aria-hidden="true" />
+ Delete ({candidates.length})
+ </Button>
+ </DialogTrigger>
+ ) : null}
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Are you absolutely sure?</DialogTitle>
+ <DialogDescription>
+ This action cannot be undone. This will permanently delete your{" "}
+ <span className="font-medium">{candidates.length}</span>
+ {candidates.length === 1 ? " candidate" : " candidates"} from our servers.
+ </DialogDescription>
+ </DialogHeader>
+ <DialogFooter className="gap-2 sm:space-x-0">
+ <DialogClose asChild>
+ <Button variant="outline">Cancel</Button>
+ </DialogClose>
+ <Button
+ aria-label="Delete selected rows"
+ variant="destructive"
+ onClick={onDelete}
+ disabled={isDeletePending}
+ >
+ {isDeletePending && (
+ <Loader
+ className="mr-2 size-4 animate-spin"
+ aria-hidden="true"
+ />
+ )}
+ Delete
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ )
+ }
+
+ return (
+ <Drawer {...props}>
+ {showTrigger ? (
+ <DrawerTrigger asChild>
+ <Button variant="outline" size="sm">
+ <Trash className="mr-2 size-4" aria-hidden="true" />
+ Delete ({candidates.length})
+ </Button>
+ </DrawerTrigger>
+ ) : null}
+ <DrawerContent>
+ <DrawerHeader>
+ <DrawerTitle>Are you absolutely sure?</DrawerTitle>
+ <DrawerDescription>
+ This action cannot be undone. This will permanently delete your{" "}
+ <span className="font-medium">{candidates.length}</span>
+ {candidates.length === 1 ? " candidate" : " candidates"} from our servers.
+ </DrawerDescription>
+ </DrawerHeader>
+ <DrawerFooter className="gap-2 sm:space-x-0">
+ <DrawerClose asChild>
+ <Button variant="outline">Cancel</Button>
+ </DrawerClose>
+ <Button
+ aria-label="Delete selected rows"
+ variant="destructive"
+ onClick={onDelete}
+ disabled={isDeletePending}
+ >
+ {isDeletePending && (
+ <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
+ )}
+ Delete
+ </Button>
+ </DrawerFooter>
+ </DrawerContent>
+ </Drawer>
+ )
+}
diff --git a/lib/vendor-candidates/table/excel-template-download.tsx b/lib/vendor-candidates/table/excel-template-download.tsx
new file mode 100644
index 00000000..b69ab821
--- /dev/null
+++ b/lib/vendor-candidates/table/excel-template-download.tsx
@@ -0,0 +1,94 @@
+"use client"
+
+import { type Table } from "@tanstack/react-table"
+import ExcelJS from "exceljs"
+import { VendorCandidates } from "@/db/schema/vendors"
+
+/**
+ * Export an empty template for vendor candidates with column headers
+ * matching the expected import format
+ */
+export async function exportVendorCandidateTemplate() {
+ // Create a new workbook and worksheet
+ const workbook = new ExcelJS.Workbook()
+ const worksheet = workbook.addWorksheet("Vendor Candidates")
+
+ // Define the columns with expected headers
+ const columns = [
+ { header: "Company Name", key: "companyName", width: 30 },
+ { header: "Contact Email", key: "contactEmail", width: 30 },
+ { header: "Contact Phone", key: "contactPhone", width: 20 },
+ { header: "Country", key: "country", width: 20 },
+ { header: "Source", key: "source", width: 20 },
+ { header: "Status", key: "status", width: 15 },
+ ]
+
+ // Add columns to the worksheet
+ worksheet.columns = columns
+
+ // Style the header row
+ const headerRow = worksheet.getRow(1)
+ headerRow.font = { bold: true }
+ headerRow.alignment = { horizontal: "center" }
+ headerRow.eachCell((cell) => {
+ cell.fill = {
+ type: "pattern",
+ pattern: "solid",
+ fgColor: { argb: "FFCCCCCC" },
+ }
+ })
+
+ // Add example data rows
+ const exampleData = [
+ {
+ companyName: "ABC Corporation",
+ contactEmail: "contact@abc.com",
+ contactPhone: "+1-123-456-7890",
+ country: "US",
+ source: "Website",
+ status: "COLLECTED",
+ },
+ {
+ companyName: "XYZ Ltd.",
+ contactEmail: "info@xyz.com",
+ contactPhone: "+44-987-654-3210",
+ country: "GB",
+ source: "Referral",
+ status: "COLLECTED",
+ },
+ ]
+
+ // Add the example rows to the worksheet
+ exampleData.forEach((data) => {
+ worksheet.addRow(data)
+ })
+
+ // Add data validation for Status column
+ const statusValues = ["COLLECTED", "INVITED", "DISCARDED"]
+ for (let i = 2; i <= 100; i++) { // Apply to rows 2-100
+ worksheet.getCell(`F${i}`).dataValidation = {
+ type: 'list',
+ allowBlank: true,
+ formulae: [`"${statusValues.join(',')}"`]
+ }
+ }
+
+ // Add instructions row
+ worksheet.insertRow(1, ["Please fill in the data below. Required fields: Company Name, Contact Email"])
+ worksheet.mergeCells("A1:F1")
+ const instructionRow = worksheet.getRow(1)
+ instructionRow.font = { bold: true, color: { argb: "FF0000FF" } }
+ instructionRow.alignment = { horizontal: "center" }
+
+ // Download the workbook
+ const buffer = await workbook.xlsx.writeBuffer()
+ const blob = new Blob([buffer], {
+ type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ })
+ const url = URL.createObjectURL(blob)
+ const link = document.createElement("a")
+ link.href = url
+ link.download = "vendor-candidates-template.xlsx"
+ link.click()
+ URL.revokeObjectURL(url)
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/feature-flags-provider.tsx b/lib/vendor-candidates/table/feature-flags-provider.tsx
new file mode 100644
index 00000000..81131894
--- /dev/null
+++ b/lib/vendor-candidates/table/feature-flags-provider.tsx
@@ -0,0 +1,108 @@
+"use client"
+
+import * as React from "react"
+import { useQueryState } from "nuqs"
+
+import { dataTableConfig, type DataTableConfig } from "@/config/data-table"
+import { cn } from "@/lib/utils"
+import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip"
+
+type FeatureFlagValue = DataTableConfig["featureFlags"][number]["value"]
+
+interface FeatureFlagsContextProps {
+ featureFlags: FeatureFlagValue[]
+ setFeatureFlags: (value: FeatureFlagValue[]) => void
+}
+
+const FeatureFlagsContext = React.createContext<FeatureFlagsContextProps>({
+ featureFlags: [],
+ setFeatureFlags: () => {},
+})
+
+export function useFeatureFlags() {
+ const context = React.useContext(FeatureFlagsContext)
+ if (!context) {
+ throw new Error(
+ "useFeatureFlags must be used within a FeatureFlagsProvider"
+ )
+ }
+ return context
+}
+
+interface FeatureFlagsProviderProps {
+ children: React.ReactNode
+}
+
+export function FeatureFlagsProvider({ children }: FeatureFlagsProviderProps) {
+ const [featureFlags, setFeatureFlags] = useQueryState<FeatureFlagValue[]>(
+ "flags",
+ {
+ defaultValue: [],
+ parse: (value) => value.split(",") as FeatureFlagValue[],
+ serialize: (value) => value.join(","),
+ eq: (a, b) =>
+ a.length === b.length && a.every((value, index) => value === b[index]),
+ clearOnDefault: true,
+ shallow: false,
+ }
+ )
+
+ return (
+ <FeatureFlagsContext.Provider
+ value={{
+ featureFlags,
+ setFeatureFlags: (value) => void setFeatureFlags(value),
+ }}
+ >
+ <div className="w-full overflow-x-auto">
+ <ToggleGroup
+ type="multiple"
+ variant="outline"
+ size="sm"
+ value={featureFlags}
+ onValueChange={(value: FeatureFlagValue[]) => setFeatureFlags(value)}
+ className="w-fit gap-0"
+ >
+ {dataTableConfig.featureFlags.map((flag, index) => (
+ <Tooltip key={flag.value}>
+ <ToggleGroupItem
+ value={flag.value}
+ className={cn(
+ "gap-2 whitespace-nowrap rounded-none px-3 text-xs data-[state=on]:bg-accent/70 data-[state=on]:hover:bg-accent/90",
+ {
+ "rounded-l-sm border-r-0": index === 0,
+ "rounded-r-sm":
+ index === dataTableConfig.featureFlags.length - 1,
+ }
+ )}
+ asChild
+ >
+ <TooltipTrigger>
+ <flag.icon className="size-3.5 shrink-0" aria-hidden="true" />
+ {flag.label}
+ </TooltipTrigger>
+ </ToggleGroupItem>
+ <TooltipContent
+ align="start"
+ side="bottom"
+ sideOffset={6}
+ className="flex max-w-60 flex-col space-y-1.5 border bg-background py-2 font-semibold text-foreground"
+ >
+ <div>{flag.tooltipTitle}</div>
+ <div className="text-xs text-muted-foreground">
+ {flag.tooltipDescription}
+ </div>
+ </TooltipContent>
+ </Tooltip>
+ ))}
+ </ToggleGroup>
+ </div>
+ {children}
+ </FeatureFlagsContext.Provider>
+ )
+}
diff --git a/lib/vendor-candidates/table/feature-flags.tsx b/lib/vendor-candidates/table/feature-flags.tsx
new file mode 100644
index 00000000..aaae6af2
--- /dev/null
+++ b/lib/vendor-candidates/table/feature-flags.tsx
@@ -0,0 +1,96 @@
+"use client"
+
+import * as React from "react"
+import { useQueryState } from "nuqs"
+
+import { dataTableConfig, type DataTableConfig } from "@/config/data-table"
+import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip"
+
+type FeatureFlagValue = DataTableConfig["featureFlags"][number]["value"]
+
+interface TasksTableContextProps {
+ featureFlags: FeatureFlagValue[]
+ setFeatureFlags: (value: FeatureFlagValue[]) => void
+}
+
+const TasksTableContext = React.createContext<TasksTableContextProps>({
+ featureFlags: [],
+ setFeatureFlags: () => {},
+})
+
+export function useTasksTable() {
+ const context = React.useContext(TasksTableContext)
+ if (!context) {
+ throw new Error("useTasksTable must be used within a TasksTableProvider")
+ }
+ return context
+}
+
+export function TasksTableProvider({ children }: React.PropsWithChildren) {
+ const [featureFlags, setFeatureFlags] = useQueryState<FeatureFlagValue[]>(
+ "featureFlags",
+ {
+ defaultValue: [],
+ parse: (value) => value.split(",") as FeatureFlagValue[],
+ serialize: (value) => value.join(","),
+ eq: (a, b) =>
+ a.length === b.length && a.every((value, index) => value === b[index]),
+ clearOnDefault: true,
+ }
+ )
+
+ return (
+ <TasksTableContext.Provider
+ value={{
+ featureFlags,
+ setFeatureFlags: (value) => void setFeatureFlags(value),
+ }}
+ >
+ <div className="w-full overflow-x-auto">
+ <ToggleGroup
+ type="multiple"
+ variant="outline"
+ size="sm"
+ value={featureFlags}
+ onValueChange={(value: FeatureFlagValue[]) => setFeatureFlags(value)}
+ className="w-fit"
+ >
+ {dataTableConfig.featureFlags.map((flag) => (
+ <Tooltip key={flag.value}>
+ <ToggleGroupItem
+ value={flag.value}
+ className="whitespace-nowrap px-3 text-xs"
+ asChild
+ >
+ <TooltipTrigger>
+ <flag.icon
+ className="mr-2 size-3.5 shrink-0"
+ aria-hidden="true"
+ />
+ {flag.label}
+ </TooltipTrigger>
+ </ToggleGroupItem>
+ <TooltipContent
+ align="start"
+ side="bottom"
+ sideOffset={6}
+ className="flex max-w-60 flex-col space-y-1.5 border bg-background py-2 font-semibold text-foreground"
+ >
+ <div>{flag.tooltipTitle}</div>
+ <div className="text-xs text-muted-foreground">
+ {flag.tooltipDescription}
+ </div>
+ </TooltipContent>
+ </Tooltip>
+ ))}
+ </ToggleGroup>
+ </div>
+ {children}
+ </TasksTableContext.Provider>
+ )
+}
diff --git a/lib/vendor-candidates/table/import-button.tsx b/lib/vendor-candidates/table/import-button.tsx
new file mode 100644
index 00000000..1a2a4f7c
--- /dev/null
+++ b/lib/vendor-candidates/table/import-button.tsx
@@ -0,0 +1,211 @@
+"use client"
+
+import React, { useRef } from 'react'
+import ExcelJS from 'exceljs'
+import { toast } from 'sonner'
+import { Button } from '@/components/ui/button'
+import { Upload, Loader } from 'lucide-react'
+import { createVendorCandidate } from '../service'
+import { Input } from '@/components/ui/input'
+
+interface ImportExcelProps {
+ onSuccess?: () => void
+}
+
+export function ImportVendorCandidatesButton({ onSuccess }: ImportExcelProps) {
+ const fileInputRef = useRef<HTMLInputElement>(null)
+ const [isImporting, setIsImporting] = React.useState(false)
+
+ // Helper function to get cell value as string
+ const getCellValueAsString = (cell: ExcelJS.Cell): string => {
+ if (!cell || cell.value === undefined || cell.value === null) return '';
+
+ if (typeof cell.value === 'string') return cell.value.trim();
+ if (typeof cell.value === 'number') return cell.value.toString();
+
+ // Handle rich text
+ if (typeof cell.value === 'object' && 'richText' in cell.value) {
+ return cell.value.richText.map((rt: any) => rt.text).join('');
+ }
+
+ // Handle dates
+ if (cell.value instanceof Date) {
+ return cell.value.toISOString().split('T')[0];
+ }
+
+ // Fallback
+ return String(cell.value);
+ }
+
+ const handleImport = async (event: React.ChangeEvent<HTMLInputElement>) => {
+ const file = event.target.files?.[0]
+ if (!file) return
+
+ setIsImporting(true)
+
+ try {
+ // Read the Excel file using ExcelJS
+ const data = await file.arrayBuffer()
+ const workbook = new ExcelJS.Workbook()
+ await workbook.xlsx.load(data)
+
+ // Get the first worksheet
+ const worksheet = workbook.getWorksheet(1)
+ if (!worksheet) {
+ toast.error("No worksheet found in the spreadsheet")
+ return
+ }
+
+ // Check if there's an instruction row
+ const hasInstructionRow = worksheet.getRow(1).getCell(1).value !== null &&
+ worksheet.getRow(1).getCell(2).value === null;
+
+ // Get header row index (row 2 if there's an instruction row, otherwise row 1)
+ const headerRowIndex = hasInstructionRow ? 2 : 1;
+
+ // Get column headers and their indices
+ const headerRow = worksheet.getRow(headerRowIndex);
+ const headers: Record<number, string> = {};
+ const columnIndices: Record<string, number> = {};
+
+ headerRow.eachCell((cell, colNumber) => {
+ const header = getCellValueAsString(cell);
+ headers[colNumber] = header;
+ columnIndices[header] = colNumber;
+ });
+
+ // Process data rows
+ const rows: any[] = [];
+ const startRow = headerRowIndex + 1;
+
+ for (let i = startRow; i <= worksheet.rowCount; i++) {
+ const row = worksheet.getRow(i);
+
+ // Skip empty rows
+ if (row.cellCount === 0) continue;
+
+ // Check if this is likely an example row
+ const isExample = i === startRow && worksheet.getRow(i+1).values?.length === 0;
+ if (isExample) continue;
+
+ const rowData: Record<string, any> = {};
+ let hasData = false;
+
+ // Map the data using header indices
+ Object.entries(columnIndices).forEach(([header, colIndex]) => {
+ const value = getCellValueAsString(row.getCell(colIndex));
+ if (value) {
+ rowData[header] = value;
+ hasData = true;
+ }
+ });
+
+ if (hasData) {
+ rows.push(rowData);
+ }
+ }
+
+ if (rows.length === 0) {
+ toast.error("No data found in the spreadsheet")
+ setIsImporting(false)
+ return
+ }
+
+ // Process each row
+ let successCount = 0;
+ let errorCount = 0;
+
+ // Create promises for all vendor candidate creation operations
+ const promises = rows.map(async (row) => {
+ try {
+ // Map Excel columns to our data model
+ const candidateData = {
+ companyName: String(row['Company Name'] || ''),
+ contactEmail: String(row['Contact Email'] || ''),
+ contactPhone: String(row['Contact Phone'] || ''),
+ country: String(row['Country'] || ''),
+ source: String(row['Source'] || ''),
+ // Default to COLLECTED if not specified
+ status: (row['Status'] || 'COLLECTED') as "COLLECTED" | "INVITED" | "DISCARDED"
+ };
+
+ // Validate required fields
+ if (!candidateData.companyName || !candidateData.contactEmail) {
+ console.error("Missing required fields", candidateData);
+ errorCount++;
+ return null;
+ }
+
+ // Create the vendor candidate
+ const result = await createVendorCandidate(candidateData);
+
+ if (result.error) {
+ console.error(`Failed to import row: ${result.error}`, candidateData);
+ errorCount++;
+ return null;
+ }
+
+ successCount++;
+ return result.data;
+ } catch (error) {
+ console.error("Error processing row:", error, row);
+ errorCount++;
+ return null;
+ }
+ });
+
+ // Wait for all operations to complete
+ await Promise.all(promises);
+
+ // Show results
+ if (successCount > 0) {
+ toast.success(`Successfully imported ${successCount} vendor candidates`);
+ if (errorCount > 0) {
+ toast.warning(`Failed to import ${errorCount} rows due to errors`);
+ }
+ // Call the success callback to refresh data
+ onSuccess?.();
+ } else if (errorCount > 0) {
+ toast.error(`Failed to import all ${errorCount} rows due to errors`);
+ }
+
+ } catch (error) {
+ console.error("Import error:", error);
+ toast.error("Error importing data. Please check file format.");
+ } finally {
+ setIsImporting(false);
+ // Reset the file input
+ if (fileInputRef.current) {
+ fileInputRef.current.value = '';
+ }
+ }
+ }
+
+ return (
+ <>
+ <Input
+ type="file"
+ ref={fileInputRef}
+ onChange={handleImport}
+ accept=".xlsx,.xls"
+ className="hidden"
+ />
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => fileInputRef.current?.click()}
+ disabled={isImporting}
+ className="gap-2"
+ >
+ {isImporting ? (
+ <Loader className="size-4 animate-spin" aria-hidden="true" />
+ ) : (
+ <Upload className="size-4" aria-hidden="true" />
+ )}
+ <span className="hidden sm:inline">
+ {isImporting ? "Importing..." : "Import"}
+ </span>
+ </Button>
+ </>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/invite-candidates-dialog.tsx b/lib/vendor-candidates/table/invite-candidates-dialog.tsx
new file mode 100644
index 00000000..366b6f45
--- /dev/null
+++ b/lib/vendor-candidates/table/invite-candidates-dialog.tsx
@@ -0,0 +1,150 @@
+"use client"
+
+import * as React from "react"
+import { type Row } from "@tanstack/react-table"
+import { Loader, Mail } from "lucide-react"
+import { toast } from "sonner"
+
+import { useMediaQuery } from "@/hooks/use-media-query"
+import { Button } from "@/components/ui/button"
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog"
+import {
+ Drawer,
+ DrawerClose,
+ DrawerContent,
+ DrawerDescription,
+ DrawerFooter,
+ DrawerHeader,
+ DrawerTitle,
+ DrawerTrigger,
+} from "@/components/ui/drawer"
+
+import { VendorCandidates } from "@/db/schema/vendors"
+import { bulkUpdateVendorCandidateStatus } from "../service"
+
+interface InviteCandidatesDialogProps
+ extends React.ComponentPropsWithoutRef<typeof Dialog> {
+ candidates: Row<VendorCandidates>["original"][]
+ showTrigger?: boolean
+ onSuccess?: () => void
+}
+
+export function InviteCandidatesDialog({
+ candidates,
+ showTrigger = true,
+ onSuccess,
+ ...props
+}: InviteCandidatesDialogProps) {
+ const [isInvitePending, startInviteTransition] = React.useTransition()
+ const isDesktop = useMediaQuery("(min-width: 640px)")
+
+ function onInvite() {
+ startInviteTransition(async () => {
+ const { error } = await bulkUpdateVendorCandidateStatus({
+ ids: candidates.map((candidate) => candidate.id),
+ status: "INVITED",
+ })
+
+ if (error) {
+ toast.error(error)
+ return
+ }
+
+ props.onOpenChange?.(false)
+ toast.success("Invitation emails sent")
+ onSuccess?.()
+ })
+ }
+
+ if (isDesktop) {
+ return (
+ <Dialog {...props}>
+ {showTrigger ? (
+ <DialogTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Mail className="size-4" aria-hidden="true" />
+ Invite ({candidates.length})
+ </Button>
+ </DialogTrigger>
+ ) : null}
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Send invitations?</DialogTitle>
+ <DialogDescription>
+ This will send invitation emails to{" "}
+ <span className="font-medium">{candidates.length}</span>
+ {candidates.length === 1 ? " candidate" : " candidates"} and change their status to INVITED.
+ </DialogDescription>
+ </DialogHeader>
+ <DialogFooter className="gap-2 sm:space-x-0">
+ <DialogClose asChild>
+ <Button variant="outline">Cancel</Button>
+ </DialogClose>
+ <Button
+ aria-label="Invite selected vendors"
+ variant="default"
+ onClick={onInvite}
+ disabled={isInvitePending}
+ >
+ {isInvitePending && (
+ <Loader
+ className="mr-2 size-4 animate-spin"
+ aria-hidden="true"
+ />
+ )}
+ Send Invitations
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ )
+ }
+
+ return (
+ <Drawer {...props}>
+ {showTrigger ? (
+ <DrawerTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Mail className="size-4" aria-hidden="true" />
+ Invite ({candidates.length})
+ </Button>
+ </DrawerTrigger>
+ ) : null}
+ <DrawerContent>
+ <DrawerHeader>
+ <DrawerTitle>Send invitations?</DrawerTitle>
+ <DrawerDescription>
+ This will send invitation emails to{" "}
+ <span className="font-medium">{candidates.length}</span>
+ {candidates.length === 1 ? " candidate" : " candidates"} and change their status to INVITED.
+ </DrawerDescription>
+ </DrawerHeader>
+ <DrawerFooter className="gap-2 sm:space-x-0">
+ <DrawerClose asChild>
+ <Button variant="outline">Cancel</Button>
+ </DrawerClose>
+ <Button
+ aria-label="Invite selected vendors"
+ variant="default"
+ onClick={onInvite}
+ disabled={isInvitePending}
+ >
+ {isInvitePending && (
+ <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
+ )}
+ Send Invitations
+ </Button>
+ </DrawerFooter>
+ </DrawerContent>
+ </Drawer>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/table/update-candidate-sheet.tsx b/lib/vendor-candidates/table/update-candidate-sheet.tsx
new file mode 100644
index 00000000..c475210b
--- /dev/null
+++ b/lib/vendor-candidates/table/update-candidate-sheet.tsx
@@ -0,0 +1,339 @@
+"use client"
+
+import * as React from "react"
+import { vendorCandidates, VendorCandidates } from "@/db/schema/vendors"
+import { zodResolver } from "@hookform/resolvers/zod"
+import { Check, ChevronsUpDown, Loader } from "lucide-react"
+import { useForm } from "react-hook-form"
+import { toast } from "sonner"
+import i18nIsoCountries from "i18n-iso-countries"
+import enLocale from "i18n-iso-countries/langs/en.json"
+import koLocale from "i18n-iso-countries/langs/ko.json"
+import { cn } from "@/lib/utils"
+
+import { Button } from "@/components/ui/button"
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form"
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select"
+import {
+ Sheet,
+ SheetClose,
+ SheetContent,
+ SheetDescription,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+} from "@/components/ui/sheet"
+import { Input } from "@/components/ui/input"
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/ui/popover"
+import {
+ Command,
+ CommandEmpty,
+ CommandGroup,
+ CommandInput,
+ CommandItem,
+ CommandList,
+} from "@/components/ui/command"
+
+import { updateVendorCandidateSchema, UpdateVendorCandidateSchema } from "../validations"
+import { updateVendorCandidate } from "../service"
+
+// Register locales for countries
+i18nIsoCountries.registerLocale(enLocale)
+i18nIsoCountries.registerLocale(koLocale)
+
+// Generate country array
+const locale = "ko"
+const countryMap = i18nIsoCountries.getNames(locale, { select: "official" })
+const countryArray = Object.entries(countryMap).map(([code, label]) => ({
+ code,
+ label,
+}))
+
+interface UpdateCandidateSheetProps
+ extends React.ComponentPropsWithRef<typeof Sheet> {
+ candidate: VendorCandidates | null
+}
+
+export function UpdateCandidateSheet({ candidate, ...props }: UpdateCandidateSheetProps) {
+ const [isUpdatePending, startUpdateTransition] = React.useTransition()
+
+ // Set default values from candidate data when the component receives a new candidate
+ React.useEffect(() => {
+ if (candidate) {
+ form.reset({
+ id: candidate.id,
+ companyName: candidate.companyName,
+ contactEmail: candidate.contactEmail,
+ contactPhone: candidate.contactPhone || "",
+ country: candidate.country || "",
+ source: candidate.source || "",
+ status: candidate.status,
+ })
+ }
+ }, [candidate])
+
+ const form = useForm<UpdateVendorCandidateSchema>({
+ resolver: zodResolver(updateVendorCandidateSchema),
+ defaultValues: {
+ id: candidate?.id || 0,
+ companyName: candidate?.companyName || "",
+ contactEmail: candidate?.contactEmail || "",
+ contactPhone: candidate?.contactPhone || "",
+ country: candidate?.country || "",
+ source: candidate?.source || "",
+ status: candidate?.status || "COLLECTED",
+ },
+ })
+
+ function onSubmit(input: UpdateVendorCandidateSchema) {
+ startUpdateTransition(async () => {
+ if (!candidate) return
+
+ const { error } = await updateVendorCandidate({
+ ...input,
+ })
+
+ if (error) {
+ toast.error(error)
+ return
+ }
+
+ form.reset()
+ props.onOpenChange?.(false)
+ toast.success("Vendor candidate updated")
+ })
+ }
+
+ return (
+ <Sheet {...props}>
+ <SheetContent className="flex flex-col gap-6 sm:max-w-md">
+ <SheetHeader className="text-left">
+ <SheetTitle>Update Vendor Candidate</SheetTitle>
+ <SheetDescription>
+ Update the vendor candidate details and save the changes
+ </SheetDescription>
+ </SheetHeader>
+ <Form {...form}>
+ <form
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="flex flex-col gap-4"
+ >
+ {/* Company Name Field */}
+ <FormField
+ control={form.control}
+ name="companyName"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Company Name</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Enter company name"
+ {...field}
+ disabled={isUpdatePending}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Contact Email Field */}
+ <FormField
+ control={form.control}
+ name="contactEmail"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Contact Email</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="email@example.com"
+ type="email"
+ {...field}
+ disabled={isUpdatePending}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Contact Phone Field */}
+ <FormField
+ control={form.control}
+ name="contactPhone"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Contact Phone</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="+82-10-1234-5678"
+ {...field}
+ disabled={isUpdatePending}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Country Field */}
+ <FormField
+ control={form.control}
+ name="country"
+ render={({ field }) => {
+ const selectedCountry = countryArray.find(
+ (c) => c.code === field.value
+ )
+ return (
+ <FormItem>
+ <FormLabel>Country</FormLabel>
+ <Popover>
+ <PopoverTrigger asChild>
+ <FormControl>
+ <Button
+ variant="outline"
+ role="combobox"
+ className={cn(
+ "w-full justify-between",
+ !field.value && "text-muted-foreground"
+ )}
+ disabled={isUpdatePending}
+ >
+ {selectedCountry
+ ? selectedCountry.label
+ : "Select a country"}
+ <ChevronsUpDown className="ml-2 h-4 w-4 opacity-50" />
+ </Button>
+ </FormControl>
+ </PopoverTrigger>
+ <PopoverContent className="w-[300px] p-0">
+ <Command>
+ <CommandInput placeholder="Search country..." />
+ <CommandList>
+ <CommandEmpty>No country found.</CommandEmpty>
+ <CommandGroup className="max-h-[300px] overflow-y-auto">
+ {countryArray.map((country) => (
+ <CommandItem
+ key={country.code}
+ value={country.label}
+ onSelect={() =>
+ field.onChange(country.code)
+ }
+ >
+ <Check
+ className={cn(
+ "mr-2 h-4 w-4",
+ country.code === field.value
+ ? "opacity-100"
+ : "opacity-0"
+ )}
+ />
+ {country.label}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
+ <FormMessage />
+ </FormItem>
+ )
+ }}
+ />
+
+ {/* Source Field */}
+ <FormField
+ control={form.control}
+ name="source"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Source</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Where this candidate was found"
+ {...field}
+ disabled={isUpdatePending}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* Status Field */}
+ <FormField
+ control={form.control}
+ name="status"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Status</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ disabled={isUpdatePending}
+ >
+ <FormControl>
+ <SelectTrigger className="capitalize">
+ <SelectValue placeholder="Select a status" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectGroup>
+ {vendorCandidates.status.enumValues.map((item) => (
+ <SelectItem
+ key={item}
+ value={item}
+ className="capitalize"
+ >
+ {item}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ <SheetFooter className="gap-2 pt-2 sm:space-x-0">
+ <SheetClose asChild>
+ <Button type="button" variant="outline" disabled={isUpdatePending}>
+ Cancel
+ </Button>
+ </SheetClose>
+ <Button disabled={isUpdatePending}>
+ {isUpdatePending && (
+ <Loader
+ className="mr-2 size-4 animate-spin"
+ aria-hidden="true"
+ />
+ )}
+ Save
+ </Button>
+ </SheetFooter>
+ </form>
+ </Form>
+ </SheetContent>
+ </Sheet>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-candidates/utils.ts b/lib/vendor-candidates/utils.ts
new file mode 100644
index 00000000..8973d557
--- /dev/null
+++ b/lib/vendor-candidates/utils.ts
@@ -0,0 +1,40 @@
+import {
+ Activity,
+ AlertCircle,
+ AlertTriangle,
+ ArrowDownIcon,
+ ArrowRightIcon,
+ ArrowUpIcon,
+ AwardIcon,
+ BadgeCheck,
+ CheckCircle2,
+ CircleHelp,
+ CircleIcon,
+ CircleX,
+ ClipboardCheck,
+ ClipboardList,
+ FileCheck2,
+ FilePenLine,
+ FileX2,
+ MailCheck,
+ PencilIcon,
+ SearchIcon,
+ SendIcon,
+ Timer,
+ Trash2,
+ XCircle,
+} from "lucide-react"
+
+import { VendorCandidates } from "@/db/schema/vendors"
+
+
+export function getCandidateStatusIcon(status: VendorCandidates["status"]) {
+ const statusIcons = {
+ COLLECTED: ClipboardList, // Data collection icon
+ INVITED: MailCheck, // Email sent and checked icon
+ DISCARDED: Trash2, // Trashed/discarded icon
+ }
+
+ return statusIcons[status] || CircleIcon
+}
+
diff --git a/lib/vendor-candidates/validations.ts b/lib/vendor-candidates/validations.ts
new file mode 100644
index 00000000..0abb568e
--- /dev/null
+++ b/lib/vendor-candidates/validations.ts
@@ -0,0 +1,84 @@
+import { vendorCandidates } from "@/db/schema/vendors"
+import {
+ createSearchParamsCache,
+ parseAsArrayOf,
+ parseAsInteger,
+ parseAsString,
+ parseAsStringEnum,
+} from "nuqs/server"
+import * as z from "zod"
+import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
+
+export const searchParamsCandidateCache = createSearchParamsCache({
+ // Common flags
+ flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault([]),
+
+ // Paging
+ page: parseAsInteger.withDefault(1),
+ perPage: parseAsInteger.withDefault(10),
+
+ // Sorting - adjusting for vendorInvestigationsView
+ sort: getSortingStateParser<typeof vendorCandidates.$inferSelect>().withDefault([
+ { id: "createdAt", desc: true },
+ ]),
+
+ // Advanced filter
+ filters: getFiltersStateParser().withDefault([]),
+ joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
+
+ // Global search
+ search: parseAsString.withDefault(""),
+
+ // -----------------------------------------------------------------
+ // Fields specific to vendor investigations
+ // -----------------------------------------------------------------
+
+ // investigationStatus: PLANNED, IN_PROGRESS, COMPLETED, CANCELED
+ status: parseAsStringEnum(["COLLECTED", "INVITED", "DISCARDED"]),
+
+ // In case you also want to filter by vendorName, vendorCode, etc.
+ companyName: parseAsString.withDefault(""),
+ contactEmail: parseAsString.withDefault(""),
+ contactPhone: parseAsString.withDefault(""),
+ country: parseAsString.withDefault(""),
+ source: parseAsString.withDefault(""),
+
+
+})
+
+// Finally, export the type you can use in your server action:
+export type GetVendorsCandidateSchema = Awaited<ReturnType<typeof searchParamsCandidateCache.parse>>
+
+
+// Updated version of the updateVendorCandidateSchema
+export const updateVendorCandidateSchema = z.object({
+ id: z.number(),
+ companyName: z.string().min(1).max(255).optional(),
+ contactEmail: z.string().email().max(255).optional(),
+ contactPhone: z.string().max(50).optional(),
+ country: z.string().max(100).optional(),
+ source: z.string().max(100).optional(),
+ status: z.enum(["COLLECTED", "INVITED", "DISCARDED"]).optional(),
+ updatedAt: z.date().optional().default(() => new Date()),
+});
+
+// Create schema for vendor candidates
+export const createVendorCandidateSchema = z.object({
+ companyName: z.string().min(1).max(255),
+ contactEmail: z.string().email().max(255),
+ contactPhone: z.string().max(50).optional(),
+ country: z.string().max(100).optional(),
+ source: z.string().max(100).optional(),
+ status: z.enum(["COLLECTED", "INVITED", "DISCARDED"]).default("COLLECTED"),
+});
+
+// Export types for both schemas
+export type UpdateVendorCandidateSchema = z.infer<typeof updateVendorCandidateSchema>;
+export type CreateVendorCandidateSchema = z.infer<typeof createVendorCandidateSchema>;
+
+
+export const removeCandidatesSchema = z.object({
+ ids: z.array(z.number()).min(1, "At least one candidate ID must be provided"),
+});
+
+export type RemoveCandidatesInput = z.infer<typeof removeCandidatesSchema>; \ No newline at end of file