From 33dda857c78ac8ffd5b3e452391d3f0f6d542b1b Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Tue, 13 May 2025 16:10:26 +0300 Subject: [PATCH] fix: add create for partial matches --- .../components/external-id-field.tsx | 135 ++++++++++++------ 1 file changed, 91 insertions(+), 44 deletions(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx b/apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx index e94465f796..6e939d8213 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/_components/create-key/components/external-id-field.tsx @@ -21,14 +21,7 @@ export const ExternalIdField = ({ disabled = false, }: ExternalIdFieldProps) => { const [searchValue, setSearchValue] = useState(""); - const { identities, isFetchingNextPage, hasNextPage, loadMore } = useFetchIdentities(); - const identityOptions = createIdentityOptions({ - identities, - hasNextPage, - isFetchingNextPage, - loadMore, - }); const createIdentity = useCreateIdentity((data) => { onChange(data.identityId); @@ -43,17 +36,66 @@ export const ExternalIdField = ({ } }; + const exactMatch = identities.some( + (id) => id.externalId.toLowerCase() === searchValue.toLowerCase().trim(), + ); + + const filteredIdentities = searchValue.trim() + ? identities.filter((identity) => + identity.externalId.toLowerCase().includes(searchValue.toLowerCase().trim()), + ) + : identities; + + const hasPartialMatches = filteredIdentities.length > 0; + + const baseOptions = createIdentityOptions({ + identities: filteredIdentities, + hasNextPage, + isFetchingNextPage, + loadMore, + }); + + const createOption = + searchValue.trim() && !exactMatch && hasPartialMatches + ? { + label: ( +
+
+ +
+ + Create "{searchValue.trim()}" + +
+ ), + value: "__create_new__", + selectedLabel: <>, + searchValue: searchValue.trim(), + } + : null; + + const options = createOption ? [createOption, ...baseOptions] : baseOptions; + return ( setSearchValue(e.currentTarget.value)} onSelect={(val) => { + if (val === "__create_new__") { + handleCreateIdentity(); + return; + } const identity = identities.find((id) => id.id === val); onChange(identity?.id || null); }} @@ -64,44 +106,49 @@ export const ExternalIdField = ({ } searchPlaceholder="Search External ID..." emptyMessage={ -
-
-
-
- -
-
- External ID not found + searchValue.trim() && !exactMatch ? ( +
+
+
+
+ +
+
+ External ID not found +
+
+
+
+
+ You can create a new identity with this{" "} + External ID and connect it{" "} + immediately. +
+
+ +
-
-
-
-
- You can create a new identity with this External ID{" "} - and connect it immediately. -
-
- -
-
+ ) : ( +
No results found
+ ) } variant="default" error={error}