diff --git a/src/components/ObjectActions.tsx b/src/components/ObjectActions.tsx index 239d8d2..1c73319 100644 --- a/src/components/ObjectActions.tsx +++ b/src/components/ObjectActions.tsx @@ -170,9 +170,9 @@ export default function ObjectActions({ async function handlePin() { if (isPinned) { - await removePinnedObject(spaceId, objectId, spaceIdForPinned); + await removePinnedObject(spaceId, objectId, spaceIdForPinned, title, getContextLabel()); } else { - await addPinnedObject(spaceId, objectId, spaceIdForPinned, getContextLabel()); + await addPinnedObject(spaceId, objectId, spaceIdForPinned, title, getContextLabel()); } if (mutate) { for (const m of mutate) { diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 579e732..4ce80bc 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -6,12 +6,18 @@ export const apiAppName = "raycast_v1_0125"; export const apiUrl = "http://localhost:31009/v1"; export const anytypeNetwork = "N83gJpVd9MuNRZAuJLZ7LiMntTThhPc6DtzWWVjb1M3PouVU"; export const downloadUrl = "https://download.anytype.io/"; +export const errorConnectionMessage = "Can't connect to API. Please ensure Anytype is running and reachable."; + +// Numbers export const apiLimit = getPreferenceValues().limit; export const apiLimitMax = 1000; export const iconWidth = 64; -export const errorConnectionMessage = "Can't connect to API. Please ensure Anytype is running and reachable."; +export const maxPinnedObjects = 5; + +// Local Storage Keys +export const pinnedObjectsKey = "pinned_objects"; -// API Endponts +// API Endpoints export const apiEndpoints = { createObject: (spaceId: string) => ({ url: `${apiUrl}/spaces/${spaceId}/objects`, diff --git a/src/helpers/localStorage.ts b/src/helpers/localStorage.ts index ad92db9..e88b530 100644 --- a/src/helpers/localStorage.ts +++ b/src/helpers/localStorage.ts @@ -1,10 +1,8 @@ import { LocalStorage, showToast, Toast } from "@raycast/api"; - -const maxPinnedObjects = 5; -const PINNED_OBJECTS_KEY = "pinned_objects"; +import { maxPinnedObjects, pinnedObjectsKey } from "./constants"; export async function getPinnedObjects(spaceIdForPinned: string): Promise<{ spaceId: string; objectId: string }[]> { - const pinnedObjects = await LocalStorage.getItem(`${spaceIdForPinned}_${PINNED_OBJECTS_KEY}`); + const pinnedObjects = await LocalStorage.getItem(`${spaceIdForPinned}_${pinnedObjectsKey}`); return pinnedObjects ? JSON.parse(pinnedObjects) : []; } @@ -12,6 +10,7 @@ export async function addPinnedObject( spaceId: string, objectId: string, spaceIdForPinned: string, + title: string, contextLabel: string, ): Promise { const pinnedObjects = await getPinnedObjects(spaceIdForPinned); @@ -34,20 +33,42 @@ export async function addPinnedObject( } pinnedObjects.push({ spaceId, objectId }); - await LocalStorage.setItem(`${spaceIdForPinned}_${PINNED_OBJECTS_KEY}`, JSON.stringify(pinnedObjects)); + await LocalStorage.setItem(`${spaceIdForPinned}_${pinnedObjectsKey}`, JSON.stringify(pinnedObjects)); await showToast({ style: Toast.Style.Success, - title: ` ${contextLabel} pinned successfully`, + title: `${contextLabel} pinned`, + message: title, }); } -export async function removePinnedObject(spaceId: string, objectId: string, spaceIdForPinned: string): Promise { +export async function removePinnedObject( + spaceId: string, + objectId: string, + spaceIdForPinned: string, + title: string, + contextLabel: string, +): Promise { const pinnedObjects = await getPinnedObjects(spaceIdForPinned); const updatedPinnedObjects = pinnedObjects.filter( (pinned) => pinned.spaceId !== spaceId || pinned.objectId !== objectId, ); - await LocalStorage.setItem(`${spaceIdForPinned}_${PINNED_OBJECTS_KEY}`, JSON.stringify(updatedPinnedObjects)); + + if (updatedPinnedObjects.length === pinnedObjects.length) { + await showToast({ + style: Toast.Style.Failure, + title: `${contextLabel} is not pinned`, + }); + return; + } + + await LocalStorage.setItem(`${spaceIdForPinned}_${pinnedObjectsKey}`, JSON.stringify(updatedPinnedObjects)); + + await showToast({ + style: Toast.Style.Success, + title: `${contextLabel} unpinned`, + message: title, + }); } async function movePinnedItem( @@ -64,7 +85,7 @@ async function movePinnedItem( } // Swap the two items using destructuring assignment [pinnedObjects[index], pinnedObjects[targetIndex]] = [pinnedObjects[targetIndex], pinnedObjects[index]]; - await LocalStorage.setItem(`${spaceIdForPinned}_${PINNED_OBJECTS_KEY}`, JSON.stringify(pinnedObjects)); + await LocalStorage.setItem(`${spaceIdForPinned}_${pinnedObjectsKey}`, JSON.stringify(pinnedObjects)); } export async function moveUpInPinned(spaceId: string, objectId: string, spaceIdForPinned: string): Promise {