Skip to content

Commit

Permalink
hotfix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiryous committed Sep 28, 2024
1 parent 55c7726 commit 034dfc0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
88 changes: 61 additions & 27 deletions keep-ui/app/topology/ui/TopologySearchAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,49 @@ import { useTopology } from "utils/hooks/useTopology";
import { useTopologyApplications } from "utils/hooks/useApplications";
import { AutocompleteInput } from "@/components/ui";
import { MagnifyingGlassIcon } from "@heroicons/react/24/solid";
import { AutocompleteInputProps } from "@/components/ui/AutocompleteInput";
import { TopologyServiceMinimal, TopologyApplication } from "../models";
import {
AutocompleteInputProps,
Option,
} from "@/components/ui/AutocompleteInput";
import {
TopologyServiceMinimal,
TopologyApplication,
TopologyApplicationMinimal,
} from "../models";

type TopologySearchAutocompleteWithApplicationsProps = Omit<
AutocompleteInputProps<TopologyServiceMinimal | TopologyApplicationMinimal>,
"options" | "getId"
> & {
includeApplications: true;
providerId?: string;
service?: string;
environment?: string;
};

type TopologySearchAutocompleteWithoutApplicationsProps = Omit<
AutocompleteInputProps<TopologyServiceMinimal>,
"options" | "getId"
> & {
includeApplications: false;
providerId?: string;
service?: string;
environment?: string;
};

type TopologySearchAutocompleteProps =
| (Omit<
AutocompleteInputProps<TopologyServiceMinimal | TopologyApplication>,
"options" | "getId"
> & {
includeApplications: true;
providerId?: string;
service?: string;
environment?: string;
})
| ({
includeApplications: false;
providerId?: string;
service?: string;
environment?: string;
} & Omit<
AutocompleteInputProps<TopologyServiceMinimal>,
"options" | "getId"
>);
| TopologySearchAutocompleteWithApplicationsProps
| TopologySearchAutocompleteWithoutApplicationsProps;

export function TopologySearchAutocomplete({
includeApplications = true,
includeApplications,
providerId,
service,
environment,
onSelect,
...props
}: Omit<TopologySearchAutocompleteProps, "options">) {
const { topologyData } = useTopology(providerId, service, environment);
const { topologyData } = useTopology({ providerId, service, environment });
const { applications } = useTopologyApplications();
const searchOptions = useMemo(() => {
const options: {
Expand Down Expand Up @@ -62,15 +74,37 @@ export function TopologySearchAutocomplete({
return options;
}, [topologyData, includeApplications, applications]);

if (includeApplications) {
return (
<AutocompleteInput<TopologyServiceMinimal | TopologyApplication>
icon={MagnifyingGlassIcon}
options={searchOptions}
getId={(option) => {
return option.value.id.toString();
}}
onSelect={(option) => {
// Type guard to check if the option is a TopologyServiceMinimal
if ("service" in option.value) {
onSelect(option as Option<TopologyServiceMinimal>);
} else {
// TODO: Fix type
// @ts-ignore
onSelect(option as Option<TopologyApplicationMinimal>);
}
}}
{...props}
/>
);
}

return (
<AutocompleteInput<TopologyServiceMinimal | TopologyApplication>
<AutocompleteInput<TopologyServiceMinimal>
icon={MagnifyingGlassIcon}
// TODO: Fix type
// @ts-ignore
options={searchOptions}
getId={(option) => {
if ("service" in option.value) {
return option.value.service;
}
return option.value.id;
return option.value.service;
}}
{...props}
/>
Expand Down
3 changes: 1 addition & 2 deletions keep-ui/app/topology/ui/applications/applications-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import { ServiceSearchContext } from "../../service-search-context";
export function ApplicationsList({
applications: initialApplications,
}: {
applications: TopologyApplication[];
applications?: TopologyApplication[];
}) {
console.log("rendering ApplicationsList");
const { applications, addApplication, removeApplication, updateApplication } =
useTopologyApplications({
initialData: initialApplications,
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/components/ui/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef } from "react";
import { TextInput } from "./TextInput";
import { cn } from "utils/helpers";

type Option<T> = {
export type Option<T> = {
label: string;
value: T;
};
Expand Down

0 comments on commit 034dfc0

Please sign in to comment.