Skip to content

Commit

Permalink
Improve toasts for item pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Feb 9, 2025
1 parent 4ec6c19 commit 5c5ff40
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/ObjectActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
39 changes: 30 additions & 9 deletions src/helpers/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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<string>(`${spaceIdForPinned}_${PINNED_OBJECTS_KEY}`);
const pinnedObjects = await LocalStorage.getItem<string>(`${spaceIdForPinned}_${pinnedObjectsKey}`);
return pinnedObjects ? JSON.parse(pinnedObjects) : [];
}

export async function addPinnedObject(
spaceId: string,
objectId: string,
spaceIdForPinned: string,
title: string,
contextLabel: string,
): Promise<void> {
const pinnedObjects = await getPinnedObjects(spaceIdForPinned);
Expand All @@ -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<void> {
export async function removePinnedObject(
spaceId: string,
objectId: string,
spaceIdForPinned: string,
title: string,
contextLabel: string,
): Promise<void> {
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(
Expand All @@ -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<void> {
Expand Down

0 comments on commit 5c5ff40

Please sign in to comment.