All files / src/layouts MainLayout.tsx

100% Statements 22/22
100% Branches 20/20
100% Functions 5/5
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                      1x         16x 16x 16x 16x                     16x           16x 16x   16x   3x 2x 2x 1x             16x   2x 1x           16x   2x 1x               16x           16x                                                                                                                                          
// src/layouts/MainLayout.tsx
import React, { useCallback } from 'react';
import { Outlet } from 'react-router-dom';
import { useAuth } from '../hooks/useAuth';
import { useFlyers } from '../hooks/useFlyers';
import { useShoppingLists } from '../hooks/useShoppingLists';
import { useMasterItems } from '../hooks/useMasterItems';
import { useWatchedItems } from '../hooks/useWatchedItems';
import { useActiveDeals } from '../hooks/useActiveDeals';
 
import { FlyerList } from '../features/flyer/FlyerList';
import { FlyerUploader } from '../features/flyer/FlyerUploader';
import { ShoppingListComponent } from '../features/shopping/ShoppingList';
import { WatchedItemsList } from '../features/shopping/WatchedItemsList';
import { PriceChart } from '../features/charts/PriceChart';
import { PriceHistoryChart } from '../features/charts/PriceHistoryChart';
import Leaderboard from '../components/Leaderboard';
import { ActivityLog, ActivityLogClickHandler } from '../pages/admin/ActivityLog';
import { AnonymousUserBanner } from '../components/AnonymousUserBanner';
import { ErrorDisplay } from '../components/ErrorDisplay';
 
export interface MainLayoutProps {
  onFlyerSelect: (flyer: import('../types').Flyer) => void;
  selectedFlyerId: number | null;
  onOpenProfile: () => void;
}
 
export const MainLayout: React.FC<MainLayoutProps> = ({
  onFlyerSelect,
  selectedFlyerId,
  onOpenProfile,
}) => {
  const { userProfile, authStatus } = useAuth();
  const user = userProfile?.user ?? null;
  const { flyers, refetchFlyers, flyersError } = useFlyers();
  const { masterItems, error: masterItemsError } = useMasterItems();
  const {
    shoppingLists,
    activeListId,
    setActiveListId,
    createList,
    deleteList,
    addItemToList,
    updateItemInList,
    removeItemFromList,
    error: shoppingListError,
  } = useShoppingLists();
  const {
    watchedItems,
    addWatchedItem,
    removeWatchedItem,
    error: watchedItemsError,
  } = useWatchedItems();
  const { totalActiveItems, error: activeDealsError } = useActiveDeals();
 
  const handleActivityLogClick: ActivityLogClickHandler = useCallback(
    (log) => {
      if (log.action === 'list_shared') {
        const listId = log.details.shopping_list_id;
        if (shoppingLists.some((list) => list.shopping_list_id === listId)) {
          setActiveListId(listId);
        }
      }
    },
    [shoppingLists, setActiveListId],
  );
 
  const handleAddItemToShoppingList = useCallback(
    async (item: { masterItemId?: number; customItemName?: string }) => {
      if (activeListId) {
        await addItemToList(activeListId, item);
      }
    },
    [activeListId, addItemToList],
  );
 
  const handleAddItemFromWatchedList = useCallback(
    (masterItemId: number) => {
      if (activeListId) {
        addItemToList(activeListId, { masterItemId });
      }
    },
    [activeListId, addItemToList],
  );
 
  // Consolidate error states into a single variable for cleaner display logic.
  const combinedError =
    flyersError?.message ||
    masterItemsError ||
    shoppingListError ||
    watchedItemsError ||
    activeDealsError;
 
  return (
    <main className="max-w-screen-2xl mx-auto py-4 px-2.5 sm:py-6 lg:py-8">
      {authStatus === 'SIGNED_OUT' && flyers.length > 0 && (
        <div className="max-w-5xl mx-auto mb-6 px-4 lg:px-0">
          {' '}
          {/* This div was missing a closing tag in the original code, but it's outside the diff scope. */}
          <AnonymousUserBanner onOpenProfile={onOpenProfile} />
        </div>
      )}
      <div className="grid grid-cols-1 lg:grid-cols-4 gap-8 items-start">
        <div className="lg:col-span-1 flex flex-col space-y-6">
          <FlyerList
            flyers={flyers}
            onFlyerSelect={onFlyerSelect}
            selectedFlyerId={selectedFlyerId}
            profile={userProfile}
          />
          <FlyerUploader onProcessingComplete={refetchFlyers} />
        </div>
 
        <div className="lg:col-span-2 flex flex-col space-y-6">
          {combinedError && <ErrorDisplay message={combinedError} />}
          {/* The Outlet will render the specific page content (e.g., FlyerDisplay or Welcome message) */}
          <Outlet
            context={{
              totalActiveItems,
              masterItems,
              addWatchedItem,
              shoppingLists,
              activeListId,
              addItemToList,
            }}
          />
        </div>
 
        <div className="lg:col-span-1 flex-col space-y-6">
          <>
            <ShoppingListComponent
              user={user}
              lists={shoppingLists}
              activeListId={activeListId}
              onSelectList={setActiveListId}
              onCreateList={createList}
              onDeleteList={deleteList}
              onAddItem={handleAddItemToShoppingList}
              onUpdateItem={updateItemInList}
              onRemoveItem={removeItemFromList}
            />
            <WatchedItemsList
              items={watchedItems}
              onAddItem={addWatchedItem}
              onRemoveItem={removeWatchedItem}
              user={user}
              activeListId={activeListId}
              onAddItemToList={handleAddItemFromWatchedList}
            />
            <PriceChart
              unitSystem={'imperial'} // This can be passed down or sourced from a context
              user={user}
            />
            <PriceHistoryChart />
            <Leaderboard />
            <ActivityLog userProfile={userProfile} onLogClick={handleActivityLogClick} />
          </>
        </div>
      </div>
    </main>
  );
};