shop
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { parseLocationContent } from "@/components/chat/LocationMessageBubble";
|
||||
import { parseSharedPost } from "@/components/chat/SharedPostBubble";
|
||||
import { parseSharedProduct } from "@/components/chat/SharedProductBubble";
|
||||
import {
|
||||
extractStoryReactionText,
|
||||
parseSharedStory,
|
||||
@@ -54,6 +55,11 @@ export function getCopyableMessageText(
|
||||
return null;
|
||||
}
|
||||
|
||||
const sharedProduct = parseSharedProduct(content);
|
||||
if (sharedProduct) {
|
||||
return sharedProduct.title?.trim() || null;
|
||||
}
|
||||
|
||||
const sharedStory = parseSharedStory(content);
|
||||
if (sharedStory) {
|
||||
const reactionText = extractStoryReactionText(content);
|
||||
|
||||
63
src/lib/shops/localCart.ts
Normal file
63
src/lib/shops/localCart.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
"use client";
|
||||
|
||||
export type CartItem = {
|
||||
listingId: string;
|
||||
variantId: string;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
const CART_KEY = "shop_local_cart";
|
||||
const FAVORITES_KEY = "shop_local_favorites";
|
||||
|
||||
function readJson<T>(key: string, fallback: T): T {
|
||||
if (typeof window === "undefined") return fallback;
|
||||
try {
|
||||
const raw = localStorage.getItem(key);
|
||||
return raw ? (JSON.parse(raw) as T) : fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function writeJson<T>(key: string, value: T): void {
|
||||
if (typeof window === "undefined") return;
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
}
|
||||
|
||||
export function getCart(): CartItem[] {
|
||||
return readJson<CartItem[]>(CART_KEY, []);
|
||||
}
|
||||
|
||||
export function addToCart(listingId: string, variantId: string, quantity = 1): void {
|
||||
const cart = getCart();
|
||||
const existing = cart.find(
|
||||
(item) => item.listingId === listingId && item.variantId === variantId
|
||||
);
|
||||
if (existing) {
|
||||
existing.quantity += quantity;
|
||||
} else {
|
||||
cart.push({ listingId, variantId, quantity });
|
||||
}
|
||||
writeJson(CART_KEY, cart);
|
||||
}
|
||||
|
||||
export function getFavorites(): string[] {
|
||||
return readJson<string[]>(FAVORITES_KEY, []);
|
||||
}
|
||||
|
||||
export function isFavorite(listingId: string): boolean {
|
||||
return getFavorites().includes(listingId);
|
||||
}
|
||||
|
||||
export function toggleFavorite(listingId: string): boolean {
|
||||
const favorites = getFavorites();
|
||||
const idx = favorites.indexOf(listingId);
|
||||
if (idx >= 0) {
|
||||
favorites.splice(idx, 1);
|
||||
writeJson(FAVORITES_KEY, favorites);
|
||||
return false;
|
||||
}
|
||||
favorites.push(listingId);
|
||||
writeJson(FAVORITES_KEY, favorites);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user