Skip to content

Conversation

@KayBeSee
Copy link

@KayBeSee KayBeSee commented Jul 22, 2025

closes #77

export function useFavoriteState(initialFavorites: string[]) {
const router = useRouter();
const defaultValue = initialFavorites ? JSON.stringify(initialFavorites) : '[]';
const [cookieValue, setCookieValue] = useCookie('relisten_favorites:artists', defaultValue);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a really hard time DRYing the relisten_favorites:artists variable to reuse in src/lib/serverFavoriteCookies.ts. It kept saying the export was a function?

Also open to changing naming convention. Figure it was worth scoping to artist so a different cookie name can be used for tracks.

const cookieStore = await cookies();

try {
const value = cookieStore.get('relisten_favorites:artists')?.value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you export the cookie key so its declared once and imported?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I added to src/lib/constants.ts b/c was getting some weird behavior importing/exporting between client and server components.

@KayBeSee KayBeSee requested a review from switz July 22, 2025 17:13
@switz
Copy link
Member

switz commented Jul 24, 2025

Is there any way we can make this work without calling router.refresh?

@switz
Copy link
Member

switz commented Jul 24, 2025

Also might be better to use a Set here:

  const toggleFavorite = useCallback(
    (artistId: string) => {
      const updatedFavorites = new Set([...parsedCookies]);

      if (parsedCookies.includes(artistId)) {
        updatedFavorites.delete(artistId);
      } else {
        updatedFavorites.add(artistId);
      }

      setFavorites([...updatedFavorites]);
    },
    [parsedCookies, setFavorites]
  );

@KayBeSee
Copy link
Author

KayBeSee commented Aug 5, 2025

Also might be better to use a Set here:

  const toggleFavorite = useCallback(
    (artistId: string) => {
      const updatedFavorites = new Set([...parsedCookies]);

      if (parsedCookies.includes(artistId)) {
        updatedFavorites.delete(artistId);
      } else {
        updatedFavorites.add(artistId);
      }

      setFavorites([...updatedFavorites]);
    },
    [parsedCookies, setFavorites]
  );

Good call, just updated.

Is there any way we can make this work without calling router.refresh?

I can mess around with it for sure. I was doing router.refresh based on this comment in the original issue.

@switz
Copy link
Member

switz commented Aug 7, 2025

yeah that's my bad, didn't think to realize it would also refetch the random shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mark artist as favorite

2 participants