Files
cal.diy/companion/hooks/useActiveBookingFilter.tsx
T
Beto aa127b2c2b refactor: (companion) (iOS) create an ios screen for specific native logic (#26081)
* create an ios screen for specific native logic

* feat: update bookings layout to conditionally show header on iOS

* feat: add useActiveBookingFilter hook for managing booking filter state

- Introduced a new hook, useActiveBookingFilter, to manage the active booking filter state and provide related utilities.
- Added filter options for "upcoming", "unconfirmed", "past", and "cancelled" bookings.
- Updated index.ts to export the new hook and its types for UI state management.

* refactor: streamline booking filter management and remove unused code

- Integrated the useActiveBookingFilter hook to manage booking filter state, simplifying the filter logic.
- Removed redundant state variables and functions related to booking filters.
- Cleaned up the code by eliminating unused imports and commented-out code sections.
- Updated the booking fetching logic to utilize the new filter parameters directly.

* refactor: move meeting URL extraction and info retrieval to utility module

- Extracted meeting URL extraction and meeting info retrieval logic into a new utility module, meetings-utils.ts.
- Updated index.ios.tsx and index.tsx to utilize the new getMeetingInfo function from the utility module, simplifying the component code.
- Removed redundant helper functions from the component files to enhance maintainability.

* refactor: extract empty state content logic into utility module

- Moved the getEmptyStateContent function from the Bookings component files to a new utility module, bookings-utils.ts, to enhance code reusability and maintainability.
- Updated index.ios.tsx and index.tsx to utilize the new utility function, simplifying the component code and improving clarity.
- Removed redundant empty state logic from the component files.

* refactor: enhance booking filtering and utility functions

- Updated the Bookings component to utilize new utility functions for filtering bookings by event type and search queries, improving code clarity and maintainability.
- Moved formatting functions (formatTime, formatDate, formatMonthYear, getMonthYearKey) and grouping logic (groupBookingsByMonth) to the bookings-utils module, centralizing related functionality.
- Removed redundant code from the Bookings component files, streamlining the overall structure.

* refactor: implement booking actions hook for improved management

- Introduced a new `useBookingActions` hook to centralize booking-related actions such as rescheduling, confirming, and rejecting bookings, enhancing code organization and reusability.
- Updated the `Bookings` component to utilize the new hook, simplifying the component logic and reducing redundancy.
- Removed outdated state management and functions related to booking actions, streamlining the overall code structure.

* refactor: consolidate booking components and enhance structure

- Refactored the Bookings component to utilize a new BookingListScreen component, improving code organization and readability.
- Introduced BookingListItem for individual booking rendering, streamlining the rendering logic and enhancing maintainability.
- Removed redundant state management and functions from the Bookings component, simplifying the overall structure.
- Updated modal handling for booking actions, rescheduling, and rejection to improve user experience and code clarity.

* feat: add BookingListItem component for enhanced booking display

- Introduced a new BookingListItem component for rendering individual booking details, improving modularity and reusability.
- Implemented context menu actions for booking management, including rescheduling, editing location, and adding guests.
- Updated type definitions for BookingListItemProps to include additional action handlers for better functionality.
- Refactored existing BookingListItem component to utilize the new props structure, enhancing clarity and maintainability.

* refactor: simplify layout and styling in BookingListItem and BookingListScreen components

- Updated the BookingListItem component to streamline the layout by reducing padding and adjusting the gap between elements.
- Modified the BookingListScreen component to remove unnecessary horizontal padding in the content container style, enhancing the overall layout consistency.
2025-12-21 11:38:42 +00:00

136 lines
3.7 KiB
TypeScript

import { useState, useMemo, useCallback } from "react";
import type { NativeSyntheticEvent } from "react-native";
import type { BookingFilters } from "./useBookings";
export type BookingFilter = "upcoming" | "unconfirmed" | "past" | "cancelled";
export interface BookingFilterOption {
key: BookingFilter;
label: string;
}
const FILTER_OPTIONS: BookingFilterOption[] = [
{ key: "upcoming", label: "Upcoming" },
{ key: "unconfirmed", label: "Unconfirmed" },
{ key: "past", label: "Past" },
{ key: "cancelled", label: "Cancelled" },
];
/**
* Hook to manage active booking filter state and provide filter-related utilities.
*
* Features:
* - Manages active filter state
* - Provides filter options and labels for UI components
* - Converts filter selection to API-compatible parameters
* - Handles segmented control integration
* - Supports change callbacks for dependent filter cleanup
*
* @param initialFilter - Initial filter value (defaults to "upcoming")
* @param onFilterChange - Optional callback invoked when filter changes
* @returns Object containing filter state, handlers, and computed values
*
* @example
* ```tsx
* const {
* activeFilter,
* filterLabels,
* activeIndex,
* filterParams,
* handleFilterChange,
* handleSegmentChange,
* } = useActiveBookingFilter("upcoming", () => {
* // Clear dependent filters like search query, event type filter, etc.
* setSearchQuery("");
* setSelectedEventTypeId(null);
* });
*
* // Use with React Query
* const { data: bookings } = useBookings(filterParams);
*
* // Use with SegmentedControl
* <SegmentedControl
* values={filterLabels}
* selectedIndex={activeIndex}
* onChange={handleSegmentChange}
* />
* ```
*/
export function useActiveBookingFilter(
initialFilter: BookingFilter = "upcoming",
onFilterChange?: (filter: BookingFilter) => void
) {
const [activeFilter, setActiveFilter] = useState<BookingFilter>(initialFilter);
/**
* Converts the active filter to API-compatible query parameters
*/
const filterParams = useMemo<BookingFilters>(() => {
switch (activeFilter) {
case "upcoming":
return { status: ["upcoming"], limit: 50 };
case "unconfirmed":
return { status: ["unconfirmed"], limit: 50 };
case "past":
return { status: ["past"], limit: 100 };
case "cancelled":
return { status: ["cancelled"], limit: 100 };
default:
return { status: ["upcoming"], limit: 50 };
}
}, [activeFilter]);
/**
* Array of filter labels for UI components (e.g., SegmentedControl)
*/
const filterLabels = useMemo(() => FILTER_OPTIONS.map((option) => option.label), []);
/**
* Current active filter index for UI components
*/
const activeIndex = useMemo(
() => FILTER_OPTIONS.findIndex((option) => option.key === activeFilter),
[activeFilter]
);
/**
* Changes the active filter and invokes the optional callback
*/
const handleFilterChange = useCallback(
(filter: BookingFilter) => {
setActiveFilter(filter);
onFilterChange?.(filter);
},
[onFilterChange]
);
/**
* Handler for React Native SegmentedControl onChange event
*/
const handleSegmentChange = useCallback(
(event: NativeSyntheticEvent<{ selectedSegmentIndex: number }>) => {
const { selectedSegmentIndex } = event.nativeEvent;
const selectedFilter = FILTER_OPTIONS[selectedSegmentIndex];
if (selectedFilter) {
handleFilterChange(selectedFilter.key);
}
},
[handleFilterChange]
);
return {
// State
activeFilter,
// Computed values
filterOptions: FILTER_OPTIONS,
filterLabels,
activeIndex,
filterParams,
// Handlers
handleFilterChange,
handleSegmentChange,
};
}