-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathListingChatDrawer.tsx
More file actions
167 lines (146 loc) · 4.2 KB
/
ListingChatDrawer.tsx
File metadata and controls
167 lines (146 loc) · 4.2 KB
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
164
165
166
167
"use client";
import type { ReactNode } from "react";
import type { User } from "@supabase/supabase-js";
import { useDeviceContext } from "@/hooks/useDeviceContext";
import { Drawer } from "vaul";
import Button from "@/components/Button";
import ChatWindow from "@/components/ChatWindow";
import ListingCta from "@/components/ListingCta";
import { styled } from "@pigment-css/react";
import type { Listing } from "@/types/listing";
const sidebarWidth = "clamp(20rem, 30vw, 30rem)";
const StyledDrawerOverlay = styled(Drawer.Overlay)({
background: "rgba(0, 0, 0, 0.3)",
position: "fixed",
inset: "0",
});
const ListingCtaContainer = styled("div")({
padding: "0 1rem",
"& > *": {
width: "100%",
},
});
const StyledDrawerContent = styled(Drawer.Content)(({ theme }) => ({
background: theme.colors.background.top,
borderRadius: `${theme.corners.base} ${theme.corners.base} 0 0`,
overflowX: "hidden",
"&::after": {
display: "none",
},
marginTop: "24px",
height: "95%",
position: "fixed",
bottom: "0",
left: "0",
right: "0",
display: "flex",
flexDirection: "column",
"@media (min-width: 768px)": {
borderRadius: theme.corners.base,
height: "unset",
marginTop: "unset",
top: "24px",
right: "24px",
bottom: "24px",
left: "unset",
outline: "none",
width: sidebarWidth,
},
}));
type ListingChatDrawerProps = {
isNested?: boolean;
user: User | null;
listing: Listing;
isChatDrawerOpen: boolean;
setIsChatDrawerOpen: (open: boolean) => void;
existingThread: unknown;
};
type SharedDrawerProps = {
isNested?: boolean;
open: boolean;
onOpenChange: (open: boolean) => void;
direction?: "right";
children?: ReactNode;
};
// We need two drawer variants because `modal` changes which hooks vaul renders.
function ModalDrawer({ isNested, ...rest }: SharedDrawerProps) {
const DrawerComponent = isNested ? Drawer.NestedRoot : Drawer.Root;
return <DrawerComponent modal={true} {...rest} />;
}
function NonModalDrawer({ isNested, ...rest }: SharedDrawerProps) {
const DrawerComponent = isNested ? Drawer.NestedRoot : Drawer.Root;
return <DrawerComponent modal={false} {...rest} />;
}
export default function ListingChatDrawer({
isNested,
user,
listing,
isChatDrawerOpen,
setIsChatDrawerOpen,
existingThread,
}: ListingChatDrawerProps) {
const { isDesktop, hasTouch } = useDeviceContext();
// Mobile: always modal. Desktop: modal only if NOT a nested drawer.
// (`!isNested` treats an omitted prop the same as `false`.)
const shouldUseModal = !isDesktop || !isNested;
const visibility = listing.visibility ?? undefined;
const isStub = listing.is_stub ?? undefined;
const drawerContent = (
<>
<ListingCtaContainer>
{user ? (
listing.owner_id === user.id ? (
<ListingCta
viewer="owner"
slug={listing.slug}
visibility={visibility}
isStub={isStub}
/>
) : listing.is_stub ? (
<ListingCta
viewer="guest"
slug={listing.slug}
visibility={visibility}
isStub={true}
/>
) : (
<Drawer.Trigger asChild>
<Button variant="primary">
Contact {listing.owner_first_name || "Host"}
</Button>
</Drawer.Trigger>
)
) : (
<ListingCta
viewer="guest"
slug={listing.slug}
visibility={visibility}
isStub={isStub}
/>
)}
</ListingCtaContainer>
<Drawer.Portal>
<StyledDrawerOverlay />
<StyledDrawerContent data-vaul-no-drag={!hasTouch ? true : undefined}>
<ChatWindow
isDrawer={true}
user={user}
listing={listing}
existingThread={existingThread}
/>
</StyledDrawerContent>
</Drawer.Portal>
</>
);
const DrawerComponent = shouldUseModal ? ModalDrawer : NonModalDrawer;
return (
<DrawerComponent
isNested={isNested}
open={isChatDrawerOpen}
onOpenChange={setIsChatDrawerOpen}
direction={isDesktop ? "right" : undefined}
>
{drawerContent}
</DrawerComponent>
);
}