X Enterprises
Composables

useRestaurantFilters

Manages restaurant listing filter state with SSR-safe URL sync, derived option lists, and lazy-fetched results.

useRestaurantFilters

Filter state composable for the restaurants listing page. State is held in useState (SSR-safe) and kept in sync with URL query parameters (cuisines, locations, prices, rating, amenities, dietary). The composable pre-fetches a meta dataset (restaurants-meta) to derive the available filter options for the sidebar, and uses a separate lazy async data call (filtered-restaurants) that refreshes whenever the URL is updated.

Filters are AND-combined across groups (e.g. cuisine AND price) and OR-combined within a group (e.g. Italian OR Japanese).

Usage

const {
  filteredRestaurants,
  totalCount,
  pending,
  toggleCuisine,
  toggleLocation,
  togglePriceLevel,
  toggleAmenity,
  toggleDietaryOption,
  setRating,
  clearAllFilters,
  hasActiveFilters,
  activeFilterCount,
} = useRestaurantFilters()

Returns

Reactive state

KeyTypeDescription
selectedCuisinesRef<string[]>Currently selected cuisine ids.
selectedLocationsRef<string[]>Currently selected neighborhoods.
selectedRatingMinRef<number | null>Minimum star rating threshold.
selectedPriceLevelsRef<number[]>Currently selected price levels (1–4).
selectedAmenitiesRef<string[]>Currently selected amenity ids.
selectedDietaryOptionsRef<string[]>Currently selected dietary option ids.
selectedOpenNowRef<'any' | 'open' | 'closed'>Open-now filter. Defaults to 'any'.
selectedSortRef<'recommended' | 'distance' | 'rating' | 'price' | 'reviews'>Sort order. Defaults to 'recommended'. 'distance' triggers a geolocation permission prompt.
selectedVibesRef<string[]>Currently selected vibe ids (Good For / Atmosphere / Noise). Combined with AND logic.
filteredRestaurantsComputedRef<any[]>Restaurants matching the active filters (derived from filteredData).
totalCountComputedRef<number>Total count from the API response (raw.total when present, otherwise array length).
pendingComputedRef<boolean>true while the filtered-results fetch is in-flight.

Derived option lists

KeyTypeDescription
availableCuisinesComputedRef<{ id: string, label: string }[]>All cuisines present in the meta dataset.
availableLocationsComputedRef<string[]>All neighborhoods present in the meta dataset (sorted).
availableAmenitiesComputedRef<{ id: string, label: string }[]>All amenities present in the meta dataset.

Sync helpers

KeyTypeDescription
initializeFromUrl() => voidReads the current route's query params and seeds each selected* ref.
updateUrl() => voidWrites each selected* ref back to the URL (router.replace) and resets page to 1, then refreshes the lazy fetch.
refresh() => Promise<void>Manually re-runs the filtered-results fetch.

Toggle / setters

KeyTypeDescription
toggleCuisine(id: string) => voidToggles a cuisine id in selectedCuisines.
toggleLocation(location: string) => voidToggles a neighborhood in selectedLocations.
togglePriceLevel(level: number) => voidToggles a price level in selectedPriceLevels.
toggleAmenity(id: string) => voidToggles an amenity id in selectedAmenities.
toggleDietaryOption(option: string) => voidToggles a dietary option in selectedDietaryOptions.
toggleVibe(vibeId: string) => voidToggles a vibe id in selectedVibes. Vibe ids come from useVibes().
setRating(rating: number | null) => voidSets the minimum rating (pass null to clear).
clearAllFilters() => voidResets all filter refs and the URL query.

Computed status

KeyTypeDescription
hasActiveFiltersComputedRef<boolean>true when at least one filter is active.
activeFilterCountComputedRef<number>Total number of currently active filter values.

AI Context

composable: useRestaurantFilters
package: "@xenterprises/nuxt-x-restaurants"
use-when: >
  Building or customising the restaurants listing page filter UX. Used
  internally by XRDFilters, XRDFiltersAsideFilters, and XRDFiltersActiveFilters.
  Also use when you need to read filteredRestaurants, totalCount, or pending
  reactively, drive derived option lists (availableCuisines/Locations/Amenities),
  or clear filters programmatically from outside the filter panel.
Copyright © 2026