(null)
const comboboxId = useId()
const listboxId = `${comboboxId}-listbox`
@@ -101,39 +103,26 @@ export function MarketSelector({ symbol, onSelect }: Props) {
setSearch("")
}
- const { markets } = useMarkets()
+ const { markets, isLoading, isError } = useMarkets()
const activeMarket = markets.find((m) => m.indexTokenAddress === symbol)
- const filtered = markets.filter((m) =>
- search.trim() === ""
- ? true
- : m.name.toLowerCase().includes(search.toLowerCase()) ||
- m.indexTokenAddress.toLowerCase().includes(search.toLowerCase())
- )
- const recent = useMemo(() => recentMarkets.map((id) => markets.find((m) => m.indexTokenAddress === id)).filter((m): m is Market => Boolean(m)), [markets, recentMarkets])
const orderedMarkets = useMemo(() => {
- if (search.trim() !== "") return filtered
- const recentIds = new Set(recent.map((market) => market.indexTokenAddress))
- return [...recent, ...filtered.filter((market) => !recentIds.has(market.indexTokenAddress))]
- }, [filtered, recent, search])
-
- useEffect(() => {
- if (!open) return
-
- const currentIndex = orderedMarkets.findIndex(
- (m) => m.indexTokenAddress === symbol
- )
- setActiveIndex(Math.max(0, currentIndex))
- }, [open, orderedMarkets, symbol])
+ const ids = section === "Watchlist" ? lists.watchlist : lists.recent
+ const source = section === "All markets" ? markets :
+ ids.flatMap((id) => markets.filter((market) => market.indexTokenAddress === id))
+ const term = search.trim().toLowerCase()
+ return source.filter((market) => !market.isDisabled &&
+ (market.name.toLowerCase().includes(term) || market.indexTokenAddress.toLowerCase().includes(term)))
+ }, [lists, markets, search, section])
+ const highlighted = orderedMarkets.at(activeIndex)
+ const watched = highlighted && lists.watchlist.includes(highlighted.indexTokenAddress)
const selectMarket = (market: Market) => {
- const nextRecent = [market.indexTokenAddress, ...recentMarkets.filter((id) => id !== market.indexTokenAddress)].slice(0, 5)
- setRecentMarkets(nextRecent)
- try { localStorage.setItem(RECENT_MARKETS_KEY, JSON.stringify(nextRecent)) } catch { /* optional preference */ }
+ updateLists({ ...lists, recent: [market.indexTokenAddress, ...lists.recent.filter((id) => id !== market.indexTokenAddress)].slice(0, 5) })
const prefersReduced =
typeof window !== "undefined" &&
- window.matchMedia("(prefers-reduced-motion: reduce)").matches
+ typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches
if (
typeof document !== "undefined" &&
"startViewTransition" in document &&
@@ -191,6 +180,7 @@ export function MarketSelector({ symbol, onSelect }: Props) {
const handleOpenChange = (next: boolean) => {
setOpen(next)
+ if (next) setActiveIndex(Math.max(0, orderedMarkets.findIndex((market) => market.indexTokenAddress === symbol)))
if (!next) setSearch("")
}
@@ -260,22 +250,32 @@ export function MarketSelector({ symbol, onSelect }: Props) {
}
aria-autocomplete="list"
value={search}
- onChange={(e) => setSearch(e.target.value)}
+ onChange={(e) => { setSearch(e.target.value); setActiveIndex(0) }}
onKeyDown={handleInputKeyDown}
placeholder="Search markets..."
className="w-full rounded-sm bg-background px-2.5 py-1.5 text-xs text-foreground ring-1 ring-border outline-none placeholder:text-muted-foreground focus:ring-primary"
/>
+
+ {(["All markets", "Watchlist", "Recent markets"] as const).map((name) =>
+ {
+ setSection(name)
+ setActiveIndex(0)
+ inputRef.current?.focus()
+ }}>{name} )}
+
+ {!connected && Wallet disconnected. Lists are saved on this device.
}
+ {isLoading && Loading markets…
}
+ {isError && Market updates unavailable.
}
- {recent.length > 0 && search.trim() === "" && Recent markets }
{orderedMarkets.length === 0 ? (
- No markets found
+ {search || section === "All markets" ? "No markets found" : section === "Watchlist" ? "Your watchlist is empty" : "No recent markets"}
) : (
orderedMarkets.map((market, index) => (
@@ -289,6 +289,11 @@ export function MarketSelector({ symbol, onSelect }: Props) {
))
)}
+ {highlighted && {
+ const id = highlighted.indexTokenAddress
+ updateLists({ ...lists, watchlist: watched ? lists.watchlist.filter((value) => value !== id) : [...lists.watchlist, id] })
+ setActiveIndex(0)
+ }}>{watched ? "Remove " + highlighted.name + " from watchlist" : "Add " + highlighted.name + " to watchlist"} }
{symbol && (
diff --git a/apps/web/src/features/trade/components/chart/market-lists.test.ts b/apps/web/src/features/trade/components/chart/market-lists.test.ts
new file mode 100644
index 00000000..43be1954
--- /dev/null
+++ b/apps/web/src/features/trade/components/chart/market-lists.test.ts
@@ -0,0 +1,11 @@
+import { expect, it } from "vitest"
+import { readMarketLists, saveMarketLists } from "./market-lists"
+
+it("migrates legacy recent markets and network favorites without duplicates", () => {
+ localStorage.setItem("so4-recent-markets-v1", '["btc","btc",2,"eth"]')
+ localStorage.setItem("so4-market-favorites-v1:test", '{"markets":["eth"]}')
+ expect(readMarketLists("test")).toEqual({ recent: ["btc", "eth"], watchlist: ["eth"] })
+ saveMarketLists("test", { recent: [], watchlist: [] })
+ expect(readMarketLists("test")).toEqual({ recent: [], watchlist: [] })
+ localStorage.clear()
+})
diff --git a/apps/web/src/features/trade/components/chart/market-lists.ts b/apps/web/src/features/trade/components/chart/market-lists.ts
new file mode 100644
index 00000000..bc277a1e
--- /dev/null
+++ b/apps/web/src/features/trade/components/chart/market-lists.ts
@@ -0,0 +1,22 @@
+type MarketLists = { recent: Array; watchlist: Array }
+
+function read(key: string) {
+ try { return JSON.parse(localStorage.getItem(key) ?? "null") } catch { return null }
+}
+
+function ids(values: unknown): Array {
+ return Array.isArray(values) ? [...new Set(values.filter((id): id is string => typeof id === "string" && id.length > 0))] : []
+}
+
+export function readMarketLists(network: string): MarketLists {
+ const saved = read("so4-market-lists-v2:" + network)
+ if (saved?.version === 2) return { recent: ids(saved.recent).slice(0, 5), watchlist: ids(saved.watchlist) }
+ const favorites = read("so4-market-favorites-v1:" + network)
+ return { recent: ids(read("so4-recent-markets-v1")).slice(0, 5), watchlist: ids(favorites?.markets ?? favorites) }
+}
+
+export function saveMarketLists(network: string, lists: MarketLists) {
+ try {
+ localStorage.setItem("so4-market-lists-v2:" + network, JSON.stringify({ version: 2, ...lists }))
+ } catch { /* Browsing markets also works with session-only preferences. */ }
+}
diff --git a/apps/web/src/features/trade/data/markets.ts b/apps/web/src/features/trade/data/markets.ts
index 4e69bb2d..20a35898 100644
--- a/apps/web/src/features/trade/data/markets.ts
+++ b/apps/web/src/features/trade/data/markets.ts
@@ -1,6 +1,7 @@
import { POOL_MARKETS } from "@/features/pools/data/markets"
export type Market = {
+ isDisabled?: boolean
address: string
name: string
indexTokenAddress: string // the asset being priced (e.g. BTC)
diff --git a/apps/web/src/features/trade/hooks/useMarkets.ts b/apps/web/src/features/trade/hooks/useMarkets.ts
index 9d5c0bde..1858b24c 100644
--- a/apps/web/src/features/trade/hooks/useMarkets.ts
+++ b/apps/web/src/features/trade/hooks/useMarkets.ts
@@ -24,7 +24,7 @@ async function fetchMarkets(): Promise> {
export function useMarkets() {
const { getToken } = useTokenList()
- const { data: markets = MARKETS } = useQuery>({
+ const { data: markets = MARKETS, isLoading, isError } = useQuery>({
queryKey: queryKeys.trade.markets(),
queryFn: fetchMarkets,
...queryPolicy("metadata"),
@@ -44,7 +44,7 @@ export function useMarkets() {
[markets, getToken],
)
- return { markets, getMarket, getMarketsForIndexToken }
+ return { markets, isLoading, isError, getMarket, getMarketsForIndexToken }
}
export type { Market }