Skip to content

Commit

Permalink
List objects under templates for type
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Feb 10, 2025
1 parent a08e0d5 commit caa1667
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/components/TemplateList.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { List, showToast, Toast } from "@raycast/api";
import { useState } from "react";
import { getMaskForObject } from "../helpers/icon";
import { Template } from "../helpers/schemas";
import { pluralize } from "../helpers/strings";
import { useSearch } from "../hooks/useSearch";
import { useTemplates } from "../hooks/useTemplates";
import EmptyView from "./EmptyView";
import ObjectActions from "./ObjectActions";
import ObjectListItem from "./ObjectListItem";

type TemplatesListProps = {
spaceId: string;
Expand All @@ -19,21 +22,30 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
spaceId,
typeId,
);
const { objects, objectsError, isLoadingObjects, mutateObjects, objectsPagination } = useSearch(spaceId, searchText, [
typeId,
]);

if (templatesError) {
showToast(Toast.Style.Failure, "Failed to fetch templates", templatesError.message);
}

if (objectsError) {
showToast(Toast.Style.Failure, "Failed to fetch objects", objectsError.message);
}

const filteredTemplates = templates?.filter((template: Template) =>
template.name.toLowerCase().includes(searchText.toLowerCase()),
);

const filteredObjects = objects?.filter((object) => object.name.toLowerCase().includes(searchText.toLowerCase()));

return (
<List
isLoading={isLoadingTemplates}
isLoading={isLoadingTemplates || isLoadingObjects}
onSearchTextChange={setSearchText}
searchBarPlaceholder="Search Templates..."
pagination={templatesPagination}
pagination={objectsPagination || templatesPagination}
>
{filteredTemplates && filteredTemplates.length > 0 ? (
<List.Section
Expand Down Expand Up @@ -62,6 +74,30 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
) : (
<EmptyView title="No templates found" />
)}
{filteredObjects && filteredObjects.length > 0 ? (
<List.Section
title={searchText ? "Search Results" : "Objects"}
subtitle={`${pluralize(filteredObjects.length, "object", { withNumber: true })}`}
>
{filteredObjects.map((object) => (
<ObjectListItem
key={object.id}
spaceId={object.space_id}
objectId={object.id}
icon={{ source: object.icon, mask: getMaskForObject(object.layout, object.icon) }}
title={object.name}
subtitle={{ value: object.type, tooltip: `Type: ${object.type}` }}
accessories={[]}
mutate={[mutateObjects]}
viewType="object"
isGlobalSearch={isGlobalSearch}
isPinned={isPinned}
/>
))}
</List.Section>
) : (
<EmptyView title="No objects found" />
)}
</List>
);
}

0 comments on commit caa1667

Please sign in to comment.