|
| 1 | +import type { FC } from "react"; |
| 2 | +import { useSearchParams } from "react-router-dom"; |
| 3 | +import { |
| 4 | + Filter, |
| 5 | + MenuSkeleton, |
| 6 | + SearchFieldSkeleton, |
| 7 | + useFilter, |
| 8 | +} from "components/Filter/filter"; |
| 9 | +import { useFilterMenu } from "components/Filter/menu"; |
| 10 | +import { |
| 11 | + SelectFilter, |
| 12 | + SelectFilterOption, |
| 13 | +} from "components/Filter/SelectFilter"; |
| 14 | +import { API } from "api/api"; |
| 15 | +import { UserAvatar } from "components/UserAvatar/UserAvatar"; |
| 16 | +import { docs } from "utils/docs"; |
| 17 | +import { Organization } from "api/typesGenerated"; |
| 18 | + |
| 19 | +export const TemplatesFilter: FC = () => { |
| 20 | + const searchParamsResult = useSearchParams(); |
| 21 | + const filter = useFilter({ |
| 22 | + fallbackFilter: "deprecated:false", |
| 23 | + searchParamsResult, |
| 24 | + onUpdate: () => {}, // reset pagination |
| 25 | + }); |
| 26 | + |
| 27 | + const organizationMenu = useFilterMenu({ |
| 28 | + onChange: (option) => |
| 29 | + filter.update({ ...filter.values, template: option?.value }), |
| 30 | + value: filter.values.organization, |
| 31 | + id: "status", |
| 32 | + getSelectedOption: async () => { |
| 33 | + if (!filter.values.organization) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const org = await API.getOrganization(filter.values.organization); |
| 38 | + return orgOption(org); |
| 39 | + }, |
| 40 | + getOptions: async () => { |
| 41 | + const orgs = await API.getMyOrganizations(); |
| 42 | + return orgs.map(orgOption); |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Filter |
| 48 | + presets={[ |
| 49 | + { query: "", name: "All templates" }, |
| 50 | + { query: "deprecated", name: "Deprecated templates" }, |
| 51 | + ]} |
| 52 | + learnMoreLink={docs("/templates#template-filtering")} |
| 53 | + isLoading={false} |
| 54 | + filter={filter} |
| 55 | + options={ |
| 56 | + <> |
| 57 | + <SelectFilter |
| 58 | + placeholder="All organizations" |
| 59 | + label="Select an organization" |
| 60 | + options={organizationMenu.searchOptions} |
| 61 | + selectedOption={organizationMenu.selectedOption ?? undefined} |
| 62 | + onSelect={organizationMenu.selectOption} |
| 63 | + /> |
| 64 | + </> |
| 65 | + } |
| 66 | + skeleton={ |
| 67 | + <> |
| 68 | + <SearchFieldSkeleton /> |
| 69 | + <MenuSkeleton /> |
| 70 | + </> |
| 71 | + } |
| 72 | + /> |
| 73 | + ); |
| 74 | +}; |
| 75 | + |
| 76 | +const orgOption = (org: Organization): SelectFilterOption => ({ |
| 77 | + label: org.display_name || org.name, |
| 78 | + value: org.name, |
| 79 | + startIcon: ( |
| 80 | + <UserAvatar |
| 81 | + key={org.id} |
| 82 | + size="sm" |
| 83 | + username={org.display_name} |
| 84 | + avatarURL={org.icon} |
| 85 | + /> |
| 86 | + ), |
| 87 | +}); |
0 commit comments