"use client";
import { useTranslations } from "next-intl";
import { ReactNode, useCallback, useEffect, useRef, useState } from "react";
import { showToast } from "../../utils/toast";
import { DataListRetriever, useDebounce } from "../../hooks";
import {
Button,
Command,
CommandDialog,
CommandEmpty,
CommandInput,
CommandList,
DialogDescription,
DialogHeader,
DialogTitle,
} from "../../shadcnui";
type CommonAssociationTriggerProps = {
sourceType: string;
destinationType: string;
hasDestination?: boolean;
onTrigger: () => void;
};
export function CommonAssociationTrigger({
sourceType,
destinationType,
hasDestination,
onTrigger,
}: CommonAssociationTriggerProps) {
const t = useTranslations();
if (hasDestination)
return (
Join
);
return (
);
}
type CommonAssociationCommandDialogProps = {
show: boolean;
setShow: (show: boolean) => void;
data: DataListRetriever;
source: string;
destination: string;
destinationName: string;
children: ReactNode;
};
export function CommonAssociationCommandDialog({
show,
setShow,
data,
source,
destination,
destinationName,
children,
}: CommonAssociationCommandDialogProps) {
const t = useTranslations();
const searchTermRef = useRef("");
const [searchTerm, setSearchTerm] = useState("");
const refreshList = useCallback(
async (searchedTerm: string) => {
if (searchedTerm === searchTermRef.current) return;
searchTermRef.current = searchedTerm;
await data.search(searchedTerm);
},
[searchTerm, data],
);
const updateSearchTerm = useDebounce(refreshList, 500);
useEffect(() => {
if (show) updateSearchTerm(searchTerm);
}, [show, searchTerm]);
return (
{t(`common.association.label`, {
source: source,
destination: destination,
})}
{t(`common.association.description`, {
source: source,
destination: destination,
destination_name: destinationName,
})}
{t(`ui.search.no_results`, { type: source })}
{children}
);
}
export const triggerAssociationToast = (params: {
t: any;
source: string;
destination: string;
source_name: string;
destination_name: string;
level?: string;
}) => {
if (params.level) {
showToast(
params.t(`common.association.label`, {
source: params.source,
destination: params.destination,
}),
{
description: params.t(`common.association.success_level`, {
source: params.source,
destination: params.destination,
source_name: params.source_name,
destination_name: params.destination_name,
level: params.level,
}),
},
);
} else {
showToast(
params.t(`common.association.label`, {
source: params.source,
destination: params.destination,
}),
{
description: params.t(`common.association.success`, {
source: params.source,
destination: params.destination,
source_name: params.source_name,
destination_name: params.destination_name,
}),
},
);
}
};