Skip to content

Commit

Permalink
Add context actions in empty view for object and space creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Feb 13, 2025
1 parent 46c8ea5 commit 0cbef29
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/api/createSpace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { apiFetch } from "../helpers/api";
import { apiEndpoints } from "../helpers/constants";

export async function createSpace(objectData: { name: string }): Promise<void> {
export async function createSpace(space: { name: string }): Promise<void> {
const { url, method } = apiEndpoints.createSpace;

await apiFetch(url, {
method: method,
body: JSON.stringify({ name: objectData.name }),
body: JSON.stringify({ name: space.name }),
});
}
4 changes: 2 additions & 2 deletions src/browse-spaces.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Icon, Image, List, Toast, showToast } from "@raycast/api";
import { useEffect, useMemo, useState } from "react";
import { getMembers } from "./api/getMembers";
import EmptyView from "./components/EmptyView";
import EmptyViewSpace from "./components/EmptyViewSpace";
import EnsureAuthenticated from "./components/EnsureAuthenticated";
import SpaceListItem from "./components/SpaceListItem";
import { Space } from "./helpers/schemas";
Expand Down Expand Up @@ -133,7 +133,7 @@ function BrowseSpaces() {
})}
</List.Section>
) : (
<EmptyView title="No Spaces Found" />
<EmptyViewSpace title="No spaces found" />
)}
</List>
);
Expand Down
11 changes: 9 additions & 2 deletions src/components/CollectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
import { processObject } from "../helpers/object";
import { pluralize } from "../helpers/strings";
import { useObjectsInList } from "../hooks/useObjectsInList";
import EmptyView from "./EmptyView";
import EmptyViewObject from "./EmptyViewObject";
import ObjectListItem from "./ObjectListItem";

type CollectionListProps = {
Expand Down Expand Up @@ -61,7 +61,14 @@ export default function CollectionList({ spaceId, listId }: CollectionListProps)
))}
</List.Section>
) : (
<EmptyView title="No objects found" />
<EmptyViewObject
title="No objects found"
contextValues={{
space: spaceId,
list: listId,
name: searchText,
}}
/>
)}
</List>
);
Expand Down
43 changes: 43 additions & 0 deletions src/components/CreateSpaceForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Action, ActionPanel, Form, Icon, showToast, Toast } from "@raycast/api";
import { useState } from "react";
import { createSpace } from "../api/createSpace";

export default function CreateSpaceForm() {
const [spaceName, setSpaceName] = useState("");

const handleSubmit = async () => {
if (!spaceName) {
showToast(Toast.Style.Failure, "Space name is required");
return;
}

try {
await createSpace({ name: spaceName });
showToast(Toast.Style.Success, "Space created successfully");
} catch (error) {
if (error instanceof Error) {
showToast(Toast.Style.Failure, "Failed to create space", error.message);
} else {
showToast(Toast.Style.Failure, "Failed to create space", "Unknown error");
}
}
};

return (
<Form
actions={
<ActionPanel>
<Action.SubmitForm title="Create Space" icon={Icon.Plus} onSubmit={handleSubmit} />
</ActionPanel>
}
>
<Form.TextField
id="spaceName"
title="Space Name"
placeholder="Enter space name"
value={spaceName}
onChange={setSpaceName}
/>
</Form>
);
}
5 changes: 0 additions & 5 deletions src/components/EmptyView.tsx

This file was deleted.

32 changes: 32 additions & 0 deletions src/components/EmptyViewObject.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Action, ActionPanel, Icon, List } from "@raycast/api";
import Command, { CreateObjectFormValues } from "../create-object";

type EmptyViewObjectProps = {
title: string;
contextValues: CreateObjectFormValues;
};

export default function EmptyViewObject({ title, contextValues }: EmptyViewObjectProps) {
const draftValues: CreateObjectFormValues = {
space: contextValues.space,
type: contextValues.type,
list: contextValues.list,
name: contextValues.name,
icon: contextValues.icon,
description: contextValues.description,
body: contextValues.body,
source: contextValues.source,
};

return (
<List.EmptyView
title={title}
description="Create a new object by pressing ⏎"
actions={
<ActionPanel>
<Action.Push title="Create Object" target={<Command draftValues={draftValues} />} icon={Icon.Plus} />

Check failure on line 27 in src/components/EmptyViewObject.tsx

View workflow job for this annotation

GitHub Actions / type-check

Type '{ draftValues: CreateObjectFormValues; }' is missing the following properties from type 'LaunchProps<{ draftValues?: CreateObjectFormValues | undefined; launchContext?: LaunchContext | undefined; }>': launchType, arguments
</ActionPanel>
}
/>
);
}
16 changes: 16 additions & 0 deletions src/components/EmptyViewSpace.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Action, ActionPanel, Icon, List } from "@raycast/api";
import CreateSpaceForm from "./CreateSpaceForm";

export default function EmptyViewSpace({ title }: { title: string }) {
return (
<List.EmptyView
title={title}
description="Create a new space by pressing ⏎"
actions={
<ActionPanel>
<Action.Push title="Create Space" target={<CreateSpaceForm />} icon={Icon.Plus} />
</ActionPanel>
}
/>
);
}
12 changes: 8 additions & 4 deletions src/components/ObjectList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useMembers } from "../hooks/useMembers";
import { usePinnedMembers, usePinnedObjects, usePinnedTypes } from "../hooks/usePinnedObjects";
import { useSearch } from "../hooks/useSearch";
import { useTypes } from "../hooks/useTypes";
import EmptyView from "./EmptyView";
import EmptyViewObject from "./EmptyViewObject";
import ObjectListItem from "./ObjectListItem";

type ObjectListProps = {
Expand Down Expand Up @@ -175,8 +175,6 @@ export default function ObjectList({ spaceId }: ObjectListProps) {
};

const { processedPinned, processedRegular } = getCurrentItems();
console.log("processedPinned", processedPinned);
console.log("processedRegular", processedRegular);

return (
<List
Expand Down Expand Up @@ -251,7 +249,13 @@ export default function ObjectList({ spaceId }: ObjectListProps) {
))}
</List.Section>
) : (
<EmptyView title={`No ${currentView.charAt(0).toUpperCase() + currentView.slice(1)} Found`} />
<EmptyViewObject
title={`No ${currentView.charAt(0).toUpperCase() + currentView.slice(1)} Found`}
contextValues={{
space: spaceId,
name: searchText,
}}
/>
)}
</List>
);
Expand Down
25 changes: 22 additions & 3 deletions src/components/TemplateList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 EmptyViewObject from "./EmptyViewObject";
import ObjectActions from "./ObjectActions";
import ObjectListItem from "./ObjectListItem";

Expand Down Expand Up @@ -81,7 +81,14 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
))}
</List.Section>
) : (
<EmptyView title="No templates found" />
<EmptyViewObject
title="No templates found"
contextValues={{
space: spaceId,
type: typeId,
name: searchText,
}}
/>
)}
{filteredObjects && filteredObjects.length > 0 ? (
<List.Section
Expand All @@ -107,7 +114,19 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
))}
</List.Section>
) : (
<EmptyView title="No objects found" />
<EmptyViewObject
title="No objects found"
contextValues={{
space: spaceId,
type: typeId,
list: "",
name: searchText,
icon: "",
description: "",
body: "",
source: "",
}}
/>
)}
</List>
);
Expand Down
16 changes: 8 additions & 8 deletions src/create-object.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { useSearch } from "./hooks/useSearch";
import { useSpaces } from "./hooks/useSpaces";

export interface CreateObjectFormValues {
space: string;
type: string;
list: string;
name: string;
icon: string;
description: string;
body: string;
source: string;
space?: string;
type?: string;
list?: string;
name?: string;
icon?: string;
description?: string;
body?: string;
source?: string;
}

interface LaunchContext {
Expand Down
9 changes: 7 additions & 2 deletions src/search-anytype.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Icon, Image, List, showToast, Toast } from "@raycast/api";
import { MutatePromise } from "@raycast/utils";
import { useEffect, useState } from "react";
import EmptyView from "./components/EmptyView";
import EmptyViewObject from "./components/EmptyViewObject";
import EnsureAuthenticated from "./components/EnsureAuthenticated";
import ObjectListItem from "./components/ObjectListItem";
import { localStorageKeys } from "./helpers/constants";
Expand Down Expand Up @@ -229,7 +229,12 @@ function Search() {
))}
</List.Section>
) : (
<EmptyView title="No Objects Found" />
<EmptyViewObject
title="No objects found"
contextValues={{
name: searchText,
}}
/>
)}
</List>
);
Expand Down

0 comments on commit 0cbef29

Please sign in to comment.