Skip to content

Commit

Permalink
Implement move up and down functionality for pinned objects and refac…
Browse files Browse the repository at this point in the history
…tor local storage helper
  • Loading branch information
jmetrikat committed Feb 8, 2025
1 parent 47c011d commit 2d19580
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
60 changes: 53 additions & 7 deletions src/components/ObjectActions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action, ActionPanel, Clipboard, Color, confirmAlert, Icon, Keyboard, showToast, Toast } from "@raycast/api";
import { MutatePromise } from "@raycast/utils";
import { deleteObject } from "../api/deleteObject";
import { addPinnedObject, removePinnedObject } from "../helpers/localStorageHelper";
import { addPinnedObject, moveDownInPinned, moveUpInPinned, removePinnedObject } from "../helpers/localStorage";
import { Export, Member, SpaceObject, Template, Type } from "../helpers/schemas";
import { pluralize } from "../helpers/strings";
import ObjectDetail from "./ObjectDetail";
Expand Down Expand Up @@ -138,6 +138,33 @@ export default function ObjectActions({
}
}

async function handleMoveUpInFavorites() {
await moveUpInPinned(spaceId, objectId);
if (mutate) {
for (const m of mutate) {
await m();
}
}
await showToast({
style: Toast.Style.Success,
title: "Move Up in Pinned",
});
}

async function handleMoveDownInFavorites() {
await moveDownInPinned(spaceId, objectId);
if (mutate) {
for (const m of mutate) {
await m();
}
}

await showToast({
style: Toast.Style.Success,
title: "Move Down in Pinned",
});
}

async function handlePin() {
if (isPinned) {
await removePinnedObject(spaceId, objectId);
Expand Down Expand Up @@ -188,12 +215,6 @@ export default function ObjectActions({
shortcut={Keyboard.Shortcut.Common.CopyDeeplink}
onAction={handleCopyLink}
/>
<Action
icon={isPinned ? Icon.PinDisabled : Icon.Pin}
title={isPinned ? "Unpin Object" : "Pin Object"}
shortcut={{ modifiers: ["cmd", "shift"], key: "p" }}
onAction={handlePin}
/>
<Action
icon={Icon.Trash}
title={`Delete ${getContextLabel()}`}
Expand All @@ -202,6 +223,31 @@ export default function ObjectActions({
onAction={handleDeleteObject}
/>

<ActionPanel.Section>
{isPinned && (
<Action
icon={Icon.ArrowUp}
title="Move Up in Pinned"
shortcut={{ modifiers: ["opt", "cmd"], key: "arrowUp" }}
onAction={handleMoveUpInFavorites}
/>
)}
{isPinned && (
<Action
icon={Icon.ArrowDown}
title="Move Down in Pinned"
shortcut={{ modifiers: ["opt", "cmd"], key: "arrowDown" }}
onAction={handleMoveDownInFavorites}
/>
)}
<Action
icon={isPinned ? Icon.StarDisabled : Icon.Star}
title={isPinned ? "Unpin Object" : "Pin Object"}
shortcut={{ modifiers: ["cmd", "shift"], key: "f" }}
onAction={handlePin}
/>
</ActionPanel.Section>

<ActionPanel.Section>
<Action
icon={Icon.RotateClockwise}
Expand Down
6 changes: 3 additions & 3 deletions src/components/ObjectList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function ObjectList({ spaceId }: ObjectListProps) {
text: hasValidDate ? undefined : "—",
},
]}
mutate={mutateObjects}
mutate={[mutateObjects]}
viewType="object"
/>
);
Expand All @@ -111,7 +111,7 @@ export default function ObjectList({ spaceId }: ObjectListProps) {
objectId={type.id}
icon={type.icon}
title={type.name}
mutate={mutateTypes}
mutate={[mutateTypes]}
viewType="type"
/>
));
Expand All @@ -135,7 +135,7 @@ export default function ObjectList({ spaceId }: ObjectListProps) {
tooltip: `Role: ${formatRole(member.role)}`,
},
]}
mutate={mutateMembers}
mutate={[mutateMembers]}
viewType="member"
/>
));
Expand Down
8 changes: 0 additions & 8 deletions src/components/ObjectListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ export default function ObjectListItem({
subtitle={subtitle ? { value: subtitle.value, tooltip: subtitle.tooltip } : undefined}
icon={typeof icon === "string" ? { source: icon } : icon}
accessories={[
...(isPinned
? [
{
icon: Icon.Pin,
tooltip: "Pinned",
},
]
: []),
...(accessories?.map((accessory) => {
const { icon, date, text, tooltip } = accessory;
const accessoryProps: {
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/localStorageHelper.ts → src/helpers/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ export async function removePinnedObject(spaceId: string, objectId: string): Pro
);
await LocalStorage.setItem("pinned_objects", JSON.stringify(updatedPinnedObjects));
}

async function movePinnedItem(spaceId: string, objectId: string, direction: -1 | 1): Promise<void> {
const pinnedObjects = await getPinnedObjects();
const index = pinnedObjects.findIndex((pinned) => pinned.spaceId === spaceId && pinned.objectId === objectId);
const targetIndex = index + direction;
if (index === -1 || targetIndex < 0 || targetIndex >= pinnedObjects.length) {
return;
}
// Swap the two items using destructuring assignment
[pinnedObjects[index], pinnedObjects[targetIndex]] = [pinnedObjects[targetIndex], pinnedObjects[index]];
await LocalStorage.setItem("pinned_objects", JSON.stringify(pinnedObjects));
}

export async function moveUpInPinned(spaceId: string, objectId: string): Promise<void> {
await movePinnedItem(spaceId, objectId, -1);
}

export async function moveDownInPinned(spaceId: string, objectId: string): Promise<void> {
await movePinnedItem(spaceId, objectId, 1);
}
2 changes: 1 addition & 1 deletion src/hooks/usePinnedObjects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCachedPromise } from "@raycast/utils";
import { getObject } from "../api/getObject";
import { getPinnedObjects, removePinnedObject } from "../helpers/localStorageHelper";
import { getPinnedObjects, removePinnedObject } from "../helpers/localStorage";

export function usePinnedObjects() {
const { data, error, isLoading, mutate } = useCachedPromise(
Expand Down
1 change: 1 addition & 0 deletions src/search-anytype.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function Search() {
: `Never ${getShortDateLabel()}`,
text: hasValidDate ? undefined : "—",
},
...(isPinned ? [{ icon: Icon.Star, tooltip: "Pinned" }] : []),
{
icon: {
source: spaceIcon,
Expand Down

0 comments on commit 2d19580

Please sign in to comment.