Skip to content

Commit

Permalink
Refactor primary object actions and fix context labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Feb 13, 2025
1 parent 7193a8a commit 3268383
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 66 deletions.
99 changes: 43 additions & 56 deletions src/components/ObjectActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function ObjectActions({
isNoPinView,
isPinned,
}: ObjectActionsProps) {
const { primaryAction } = getPreferenceValues();
const objectUrl = `anytype://object?objectId=${objectId}&spaceId=${spaceId}`;
const pinSuffixForView = isGlobalSearch
? localStorageKeys.suffixForGlobalSearch
Expand All @@ -60,22 +61,20 @@ export default function ObjectActions({
const isCollection = layout === "collection";
const isType = viewType === CurrentView.types;

const { defaultCommand } = getPreferenceValues();

function getContextLabel(isSingular = true) {
const labelMap: Record<string, string> = {
// browse
object: "Object",
type: "Type",
member: "Member",
template: "Template",
objects: "Object",
types: "Type",
members: "Member",
templates: "Template",

// search
all: "Object",
page: "Page",
task: "Task",
list: "List",
bookmark: "Bookmark",
pages: "Page",
tasks: "Task",
lists: "List",
bookmarks: "Bookmark",
};
const baseLabel = labelMap[viewType] || "Item";
return !isDetailView && !isSingular ? pluralize(2, baseLabel) : baseLabel;
Expand Down Expand Up @@ -204,74 +203,62 @@ export default function ObjectActions({
}
}

const canShowDetails = !isType && !isCollection && !isDetailView;
const showDetailsAction = canShowDetails && (
<Action.Push
icon={{ source: Icon.Sidebar }}
title="Show Details"
target={
<ObjectDetail
spaceId={spaceId}
objectId={objectId}
title={title}
viewType={viewType}
isGlobalSearch={isGlobalSearch}
isPinned={isPinned}
/>
}
/>
);

const openObjectAction = (
<Action.OpenInBrowser
icon={{ source: "../assets/anytype-icon.png" }}
title={`Open ${getContextLabel()} in Anytype`}
url={objectUrl}
/>
);

const firstPrimaryAction = primaryAction === "show_details" ? showDetailsAction : openObjectAction;
const secondPrimaryAction = primaryAction === "show_details" ? openObjectAction : showDetailsAction;

return (
<ActionPanel title={title}>
<ActionPanel.Section>
{defaultCommand === "show_details" && !isType && !isCollection && !isDetailView && (
<Action.Push
icon={{ source: Icon.Sidebar }}
title="Show Details"
target={
<ObjectDetail
spaceId={spaceId}
objectId={objectId}
title={title}
isGlobalSearch={isGlobalSearch}
isPinned={isPinned}
/>
}
/>
)}

{defaultCommand === "open_object" && (
<Action.OpenInBrowser
icon={{ source: "../assets/anytype-icon.png" }}
title={`Open ${getContextLabel()} in Anytype`}
url={objectUrl}
/>
)}

{firstPrimaryAction}
{isCollection && (
<Action.Push
icon={Icon.List}
title="View Collection"
target={<CollectionList spaceId={spaceId} listId={objectId} />}
/>
)}

{isType && (
<Action.Push
icon={Icon.BulletPoints}
title="View Templates"
target={
<TemplateList spaceId={spaceId} typeId={objectId} isGlobalSearch={isGlobalSearch} isPinned={isPinned} />
}
/>
)}

{defaultCommand !== "open_object" && (
<Action.OpenInBrowser
icon={{ source: "../assets/anytype-icon.png" }}
title={`Open ${getContextLabel()} in Anytype`}
url={objectUrl}
/>
)}

{defaultCommand !== "show_details" && !isType && !isCollection && !isDetailView && (
<Action.Push
icon={{ source: Icon.Sidebar }}
title="Show Details"
target={
<ObjectDetail
<TemplateList
spaceId={spaceId}
objectId={objectId}
title={title}
typeId={objectId}
viewType={viewType}
isGlobalSearch={isGlobalSearch}
isPinned={isPinned}
/>
}
/>
)}
{secondPrimaryAction}
</ActionPanel.Section>
<ActionPanel.Section>
{objectExport && (
Expand Down
12 changes: 10 additions & 2 deletions src/components/ObjectDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ type ObjectDetailProps = {
spaceId: string;
objectId: string;
title: string;
viewType: string;
isGlobalSearch: boolean;
isPinned: boolean;
};

export default function ObjectDetail({ spaceId, objectId, title, isGlobalSearch, isPinned }: ObjectDetailProps) {
export default function ObjectDetail({
spaceId,
objectId,
title,
viewType,
isGlobalSearch,
isPinned,
}: ObjectDetailProps) {
const { object, objectError, isLoadingObject, mutateObject } = useObject(spaceId, objectId);
const { objectExport, objectExportError, isLoadingObjectExport, mutateObjectExport } = useExport(
spaceId,
Expand Down Expand Up @@ -110,7 +118,7 @@ export default function ObjectDetail({ spaceId, objectId, title, isGlobalSearch,
mutateObject={mutateObject}
mutateExport={mutateObjectExport}
objectExport={objectExport}
viewType="object"
viewType={viewType}
isGlobalSearch={isGlobalSearch}
isNoPinView={false}
isPinned={isPinned}
Expand Down
10 changes: 3 additions & 7 deletions src/components/TemplateList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import ObjectListItem from "./ObjectListItem";
type TemplatesListProps = {
spaceId: string;
typeId: string;
viewType: string;
isGlobalSearch: boolean;
isPinned: boolean;
};

export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned }: TemplatesListProps) {
export default function TemplateList({ spaceId, typeId, viewType, isGlobalSearch, isPinned }: TemplatesListProps) {
const [searchText, setSearchText] = useState("");
const { templates, templatesError, isLoadingTemplates, mutateTemplates, templatesPagination } = useTemplates(
spaceId,
Expand Down Expand Up @@ -106,7 +107,7 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
accessories={object.accessories}
mutate={object.mutate}
layout={object.layout}
viewType="object"
viewType={viewType}
isGlobalSearch={isGlobalSearch}
isNoPinView={true}
isPinned={object.isPinned}
Expand All @@ -119,12 +120,7 @@ export default function TemplateList({ spaceId, typeId, isGlobalSearch, isPinned
contextValues={{
space: spaceId,
type: typeId,
list: "",
name: searchText,
icon: "",
description: "",
body: "",
source: "",
}}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/search-anytype.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Search() {
localStorageKeys.suffixForGlobalSearch,
);

const viewType = filterType === "all" ? "object" : filterType.replace(/s$/, "");
const viewType = filterType === "all" ? "objects" : filterType;
const excludedKeysForPages = new Set([
// not shown anywhere
"ot-audio",
Expand Down

0 comments on commit 3268383

Please sign in to comment.