Files
twenty/front/src/modules/companies/services/select.ts
Félix Malfait 7959308e0b Add search to cmd bar (#667)
* Move useFilteredSearchEntityQuery from relation picker to search module

* refactor duplicated code with useFilteredSearchCompanyQuery

* Implement similar pattern for people than for companies with useFilteredSearchEntityQuery

* Fix warning from a previous PR

* Enable search from menu

* Add companies to search

* Fix ESLint

* Refactor

* Fix according to peer review

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2023-07-15 15:23:37 -07:00

81 lines
2.0 KiB
TypeScript

import { gql } from '@apollo/client';
import { CommentableEntityForSelect } from '@/comments/types/CommentableEntityForSelect';
import { SelectedSortType } from '@/lib/filters-and-sorts/interfaces/sorts/interface';
import { useFilteredSearchEntityQuery } from '@/search/hooks/useFilteredSearchEntityQuery';
import { getLogoUrlFromDomainName } from '@/utils/utils';
import {
CommentableType,
CompanyOrderByWithRelationInput as Companies_Order_By,
CompanyWhereInput as Companies_Bool_Exp,
SortOrder as Order_By,
useGetCompaniesQuery,
useSearchCompanyQuery,
} from '~/generated/graphql';
export type CompaniesSelectedSortType = SelectedSortType<Companies_Order_By>;
export const GET_COMPANIES = gql`
query GetCompanies(
$orderBy: [CompanyOrderByWithRelationInput!]
$where: CompanyWhereInput
) {
companies: findManyCompany(orderBy: $orderBy, where: $where) {
id
domainName
name
createdAt
address
employees
_commentThreadCount
accountOwner {
id
email
displayName
firstName
lastName
}
}
}
`;
export function useCompaniesQuery(
orderBy: Companies_Order_By[],
where: Companies_Bool_Exp,
) {
return useGetCompaniesQuery({ variables: { orderBy, where } });
}
export function useFilteredSearchCompanyQuery({
searchFilter,
selectedIds = [],
limit,
}: {
searchFilter: string;
selectedIds?: string[];
limit?: number;
}) {
return useFilteredSearchEntityQuery({
queryHook: useSearchCompanyQuery,
searchOnFields: ['name'],
orderByField: 'name',
selectedIds: selectedIds,
mappingFunction: (company) =>
({
id: company.id,
entityType: CommentableType.Company,
name: company.name,
avatarUrl: getLogoUrlFromDomainName(company.domainName),
avatarType: 'squared',
} as CommentableEntityForSelect),
searchFilter,
limit,
});
}
export const defaultOrderBy: Companies_Order_By[] = [
{
createdAt: Order_By.Desc,
},
];