256 lines
8.3 KiB
TypeScript
256 lines
8.3 KiB
TypeScript
import { Drawer } from "expo-router/drawer";
|
|
import { DrawerContentScrollView, DrawerItem } from "@react-navigation/drawer";
|
|
import { useRouter, Redirect } from "expo-router";
|
|
import {
|
|
TouchableOpacity,
|
|
DeviceEventEmitter,
|
|
View,
|
|
ActivityIndicator,
|
|
} from "react-native";
|
|
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
|
|
import { REFRESH_EVENT } from "common-ui/src/lib/const-strs";
|
|
import { useStaffAuth } from "@/components/context/staff-auth-context";
|
|
import { trpc } from "@/src/trpc-client";
|
|
import { tw, MyText, theme } from "common-ui";
|
|
|
|
const SCREEN_TITLES: Record<string, string> = {
|
|
index: "Dashboard",
|
|
products: "Products",
|
|
"prices-overview": "Prices Overview",
|
|
"product-groupings": "Product Groupings",
|
|
"dashboard-banners": "Dashboard Banners",
|
|
slots: "Slots",
|
|
complaints: "Complaints",
|
|
"manage-orders": "Manage Orders",
|
|
coupons: "Coupons",
|
|
stores: "Stores",
|
|
"user-management": "User Management",
|
|
"send-notifications": "Send Notifications",
|
|
"product-tags": "Product Tags",
|
|
"rebalance-orders": "Rebalance Orders",
|
|
"customize-app": "Customize App",
|
|
};
|
|
|
|
function CustomDrawerContent() {
|
|
const router = useRouter();
|
|
const { logout, staff, isLoggedIn, regionId } = useStaffAuth();
|
|
|
|
// Server truth for the header: the login payload carries only regionId and a
|
|
// session restored from storage may predate this screen, so ask the API.
|
|
const { data: me } = trpc.admin.staffUser.me.useQuery(undefined, {
|
|
enabled: isLoggedIn,
|
|
});
|
|
|
|
const staffName = me?.name ?? staff?.name ?? "Unknown user";
|
|
// regionId 0 is the backend's "staff has no region" value.
|
|
const staffRegionId = me?.regionId ?? regionId;
|
|
const regionLabel =
|
|
me?.regionName ??
|
|
(staffRegionId ? `Region #${staffRegionId}` : "No region");
|
|
|
|
return (
|
|
<DrawerContentScrollView>
|
|
<View style={tw`p-4 border-b border-gray-200`}>
|
|
<View style={tw`flex-row items-center`}>
|
|
<MaterialIcons name="person" size={40} color="#3B82F6" />
|
|
<View style={tw`ml-3 flex-1`}>
|
|
<MyText
|
|
style={tw`text-lg font-semibold text-gray-800`}
|
|
numberOfLines={1}
|
|
>
|
|
{staffName}
|
|
</MyText>
|
|
<View style={tw`flex-row items-center mt-1`}>
|
|
<MaterialIcons name="place" size={14} color="#6B7280" />
|
|
<MyText
|
|
style={tw`text-sm text-gray-600 ml-1`}
|
|
numberOfLines={1}
|
|
>
|
|
{regionLabel}
|
|
</MyText>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
<DrawerItem
|
|
label="Dashboard"
|
|
onPress={() => router.dismissTo("/(drawer)/dashboard" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="dashboard" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Products"
|
|
onPress={() => router.push("/(drawer)/dashboard/products" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="inventory" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Prices Overview"
|
|
onPress={() => router.push("/(drawer)/dashboard/prices-overview" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="attach-money" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Product Groupings"
|
|
onPress={() => router.push("/(drawer)/dashboard/product-groupings" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="group-work" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Dashboard Banners"
|
|
onPress={() => router.push("/(drawer)/dashboard/dashboard-banners" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="view-carousel" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Slots"
|
|
onPress={() => router.push("/(drawer)/dashboard/slots" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="schedule" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Complaints"
|
|
onPress={() => router.push("/(drawer)/dashboard/complaints" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="report-problem" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Manage Orders"
|
|
onPress={() => router.push("/(drawer)/dashboard/manage-orders" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="shopping-bag" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Coupons"
|
|
onPress={() => router.push("/(drawer)/dashboard/coupons" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="local-offer" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Stores"
|
|
onPress={() => router.push("/(drawer)/dashboard/stores" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="store" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="User Management"
|
|
onPress={() => router.push("/(drawer)/dashboard/user-management" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="people" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Send Notifications"
|
|
onPress={() => router.push("/(drawer)/dashboard/send-notifications" as any)}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="campaign" size={size} color={color} />
|
|
)}
|
|
/>
|
|
<DrawerItem
|
|
label="Logout"
|
|
onPress={() => logout()}
|
|
icon={({ color, size }) => (
|
|
<MaterialIcons name="logout" size={size} color={color} />
|
|
)}
|
|
/>
|
|
</DrawerContentScrollView>
|
|
);
|
|
}
|
|
|
|
export default function Layout() {
|
|
const router = useRouter();
|
|
const { isLoggedIn, isLoading } = useStaffAuth();
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<View style={tw`flex-1 justify-center items-center bg-white`}>
|
|
<ActivityIndicator size="large" color="#3B82F6" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (!isLoggedIn) {
|
|
return <Redirect href="/login" />;
|
|
}
|
|
|
|
return (
|
|
<Drawer
|
|
drawerContent={CustomDrawerContent}
|
|
screenOptions={({ navigation }) => {
|
|
const state = navigation.getState();
|
|
const focusedRoute = state?.routes?.[state?.index ?? 0];
|
|
const dashboardStack = (focusedRoute as any)?.state;
|
|
const nestedIndex = dashboardStack?.index ?? 0;
|
|
const canGoBack = nestedIndex > 0;
|
|
const nestedRoute = dashboardStack?.routes?.[nestedIndex];
|
|
const title = SCREEN_TITLES[nestedRoute?.name ?? ""] ?? "Dashboard";
|
|
|
|
return {
|
|
swipeEnabled: false,
|
|
headerShown: true,
|
|
headerStyle: {
|
|
backgroundColor: theme.colors.gray1,
|
|
shadowOpacity: 0,
|
|
shadowRadius: 0,
|
|
shadowOffset: { height: 0, width: 0 },
|
|
elevation: 0,
|
|
},
|
|
headerTitle: title,
|
|
headerTitleAlign: "center",
|
|
headerLeft: () => (
|
|
<TouchableOpacity
|
|
onPress={() =>
|
|
canGoBack ? router.back() : (navigation as any).openDrawer()
|
|
}
|
|
style={{
|
|
marginLeft: 15,
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
backgroundColor: "#f2f2f2",
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
}}
|
|
>
|
|
<MaterialIcons
|
|
name={canGoBack ? "arrow-back" : "menu"}
|
|
size={24}
|
|
color="black"
|
|
/>
|
|
</TouchableOpacity>
|
|
),
|
|
headerRight: () => (
|
|
<TouchableOpacity
|
|
style={{
|
|
marginRight: 15,
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
backgroundColor: "#f2f2f2",
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
}}
|
|
onPress={() => {
|
|
DeviceEventEmitter.emit(REFRESH_EVENT);
|
|
}}
|
|
>
|
|
<MaterialIcons name="refresh" size={24} color="black" />
|
|
</TouchableOpacity>
|
|
),
|
|
};
|
|
}}
|
|
>
|
|
<Drawer.Screen name="dashboard" options={{ title: "Dashboard" }} />
|
|
</Drawer>
|
|
);
|
|
}
|