|
| 1 | +import { CheckCircleIcon } from "@heroicons/react/20/solid"; |
| 2 | +import { DialogClose } from "@radix-ui/react-dialog"; |
| 3 | +import { Form, useFetcher, useLocation } from "@remix-run/react"; |
| 4 | +import { type ReactNode, useState } from "react"; |
| 5 | +import { SlackColorIcon } from "~/assets/icons/SlackColorIcon"; |
| 6 | +import { slackConnectHiddenItemId, slackConnectState } from "~/components/slackConnectState"; |
| 7 | +import { Feedback } from "~/components/Feedback"; |
| 8 | +import { SideMenuItemButton } from "~/components/navigation/SideMenuItem"; |
| 9 | +import { Button, LinkButton } from "~/components/primitives/Buttons"; |
| 10 | +import { Dialog, DialogContent, DialogHeader, DialogTrigger } from "~/components/primitives/Dialog"; |
| 11 | +import { FormButtons } from "~/components/primitives/FormButtons"; |
| 12 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 13 | +import { PermissionButton } from "~/components/primitives/PermissionButton"; |
| 14 | +import { SettingsHeader, SettingsRow } from "~/components/primitives/SettingsLayout"; |
| 15 | +import { TextLink } from "~/components/primitives/TextLink"; |
| 16 | +import { SimpleTooltip } from "~/components/primitives/Tooltip"; |
| 17 | +import { useFeatureFlags } from "~/hooks/useFeatureFlags"; |
| 18 | +import { useCanManageBillingLimits, useOrganization } from "~/hooks/useOrganizations"; |
| 19 | +import { useShowSelfServe } from "~/hooks/useShowSelfServe"; |
| 20 | +import { useUser } from "~/hooks/useUser"; |
| 21 | +import { useCurrentPlan } from "~/routes/_app.orgs.$organizationSlug/route"; |
| 22 | +import { |
| 23 | + accountPath, |
| 24 | + sideMenuPreferencesPath, |
| 25 | + slackSupportInvitePath, |
| 26 | + v3BillingPath, |
| 27 | +} from "~/utils/pathBuilder"; |
| 28 | + |
| 29 | +const TITLE = "Talk to us on Slack"; |
| 30 | +const DESCRIPTION = "Get support from the Trigger.dev team in a private Slack Connect channel."; |
| 31 | +const CHANNEL = "a private Slack Connect channel with the Trigger.dev team"; |
| 32 | + |
| 33 | +function useSlackConnect() { |
| 34 | + const featureFlags = useFeatureFlags(); |
| 35 | + return slackConnectState({ |
| 36 | + support: useCurrentPlan()?.v3Subscription?.plan?.limits.support, |
| 37 | + hasAccess: featureFlags.hasSlackSupportAccess === true, |
| 38 | + connected: featureFlags.hasSlackSupportChannel === true, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +/** Bottom of the side menu, until the org has its channel or the user dismisses it. */ |
| 43 | +export function SlackConnectSideMenuItem({ isCollapsed = false }: { isCollapsed?: boolean }) { |
| 44 | + const { offer, connected } = useSlackConnect(); |
| 45 | + const itemId = slackConnectHiddenItemId(useOrganization().id); |
| 46 | + const hidden = useUser().dashboardPreferences.sideMenu?.hiddenItems?.[itemId] === true; |
| 47 | + const fetcher = useFetcher(); |
| 48 | + |
| 49 | + if (!offer || connected || hidden || fetcher.state !== "idle") return null; |
| 50 | + |
| 51 | + const dismiss = () => |
| 52 | + fetcher.submit( |
| 53 | + { |
| 54 | + customization: JSON.stringify({ |
| 55 | + hiddenItems: { [itemId]: true }, |
| 56 | + knownItemIds: [itemId], |
| 57 | + }), |
| 58 | + }, |
| 59 | + { method: "POST", action: sideMenuPreferencesPath() } |
| 60 | + ); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="border-t border-grid-bright p-1"> |
| 64 | + <SlackConnectDialog |
| 65 | + trigger={ |
| 66 | + <SideMenuItemButton |
| 67 | + icon={SlackColorIcon} |
| 68 | + // Brand logos have no internal padding, so they render one step smaller (as in the org menu). |
| 69 | + iconClassName="size-4 ml-0.5" |
| 70 | + name={TITLE} |
| 71 | + /> |
| 72 | + } |
| 73 | + onDismiss={dismiss} |
| 74 | + tooltip={isCollapsed} |
| 75 | + /> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** Org settings section. Always there with billing, whatever the side menu shows. */ |
| 81 | +export function SlackConnectSettingsSection() { |
| 82 | + const { offer, connected } = useSlackConnect(); |
| 83 | + const organization = useOrganization(); |
| 84 | + const showSelfServe = useShowSelfServe(); |
| 85 | + if (!offer) return null; |
| 86 | + // Free plans that can upgrade go straight to Billing; the modal would only repeat the row. |
| 87 | + const upgrade = offer === "upgrade" && showSelfServe; |
| 88 | + |
| 89 | + const action = connected ? ( |
| 90 | + <div className="flex items-center gap-3"> |
| 91 | + <span className="flex items-center gap-1 text-xs text-success"> |
| 92 | + <CheckCircleIcon className="size-3.5" /> |
| 93 | + Connected |
| 94 | + </span> |
| 95 | + {/* Teammates who joined later still need their own invite. A downgraded org can't send one. */} |
| 96 | + {offer === "invite" ? ( |
| 97 | + <SlackConnectDialog trigger={<Button variant="secondary/small">Invite me</Button>} /> |
| 98 | + ) : null} |
| 99 | + </div> |
| 100 | + ) : upgrade ? ( |
| 101 | + <LinkButton variant="secondary/small" to={v3BillingPath(organization)}> |
| 102 | + Upgrade |
| 103 | + </LinkButton> |
| 104 | + ) : ( |
| 105 | + <SlackConnectDialog trigger={<Button variant="secondary/small">Set up</Button>} /> |
| 106 | + ); |
| 107 | + |
| 108 | + return ( |
| 109 | + <div> |
| 110 | + <SettingsHeader title="Support" /> |
| 111 | + <SettingsRow |
| 112 | + action={action} |
| 113 | + icon={<SlackColorIcon className="size-6 shrink-0" />} |
| 114 | + title={TITLE} |
| 115 | + description={ |
| 116 | + connected |
| 117 | + ? `Your team has ${CHANNEL}.` |
| 118 | + : offer === "upgrade" |
| 119 | + ? `${DESCRIPTION} Included on Pro.` |
| 120 | + : DESCRIPTION |
| 121 | + } |
| 122 | + /> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +function SlackConnectDialog({ |
| 128 | + trigger, |
| 129 | + onDismiss, |
| 130 | + tooltip = false, |
| 131 | +}: { |
| 132 | + trigger: ReactNode; |
| 133 | + onDismiss?: () => void; |
| 134 | + /** Collapsed side menu: the label is hidden, so show TITLE on hover like the other items. */ |
| 135 | + tooltip?: boolean; |
| 136 | +}) { |
| 137 | + const [open, setOpen] = useState(false); |
| 138 | + const organization = useOrganization(); |
| 139 | + const user = useUser(); |
| 140 | + const location = useLocation(); |
| 141 | + const showSelfServe = useShowSelfServe(); |
| 142 | + const canManageBillingLimits = useCanManageBillingLimits(); |
| 143 | + const { offer, connected } = useSlackConnect(); |
| 144 | + |
| 145 | + const email = <span className="text-text-bright">{user.email}</span>; |
| 146 | + const body = |
| 147 | + offer === "invite" ? ( |
| 148 | + connected ? ( |
| 149 | + <> |
| 150 | + Your team already has {CHANNEL}. We'll send an invite to join it to {email}. |
| 151 | + </> |
| 152 | + ) : ( |
| 153 | + <> |
| 154 | + Your plan includes {CHANNEL} for support. We'll send an invite to {email}. |
| 155 | + </> |
| 156 | + ) |
| 157 | + ) : showSelfServe ? ( |
| 158 | + `Upgrade to Pro for ${CHANNEL}.` |
| 159 | + ) : ( |
| 160 | + `Pro plans include ${CHANNEL}. Ask us and we'll add it to your plan.` |
| 161 | + ); |
| 162 | + |
| 163 | + const confirmButton = |
| 164 | + offer === "invite" ? ( |
| 165 | + <Form |
| 166 | + method="post" |
| 167 | + action={slackSupportInvitePath(organization)} |
| 168 | + onSubmit={() => setOpen(false)} |
| 169 | + > |
| 170 | + <input type="hidden" name="redirectPath" value={`${location.pathname}${location.search}`} /> |
| 171 | + <PermissionButton |
| 172 | + type="submit" |
| 173 | + variant="primary/medium" |
| 174 | + hasPermission={canManageBillingLimits} |
| 175 | + noPermissionTooltip="You don't have permission to request a Slack invite" |
| 176 | + > |
| 177 | + Send invite |
| 178 | + </PermissionButton> |
| 179 | + </Form> |
| 180 | + ) : showSelfServe ? ( |
| 181 | + <LinkButton variant="primary/medium" to={v3BillingPath(organization)}> |
| 182 | + Upgrade |
| 183 | + </LinkButton> |
| 184 | + ) : ( |
| 185 | + <Feedback |
| 186 | + button={<Button variant="primary/medium">Request more…</Button>} |
| 187 | + defaultValue="help" |
| 188 | + /> |
| 189 | + ); |
| 190 | + |
| 191 | + return ( |
| 192 | + <Dialog open={open} onOpenChange={setOpen}> |
| 193 | + {tooltip ? ( |
| 194 | + <SimpleTooltip |
| 195 | + button={<DialogTrigger asChild>{trigger}</DialogTrigger>} |
| 196 | + content={TITLE} |
| 197 | + side="right" |
| 198 | + sideOffset={8} |
| 199 | + asChild |
| 200 | + tabbable |
| 201 | + disableHoverableContent |
| 202 | + /> |
| 203 | + ) : ( |
| 204 | + <DialogTrigger asChild>{trigger}</DialogTrigger> |
| 205 | + )} |
| 206 | + <DialogContent> |
| 207 | + <DialogHeader> |
| 208 | + <span className="flex items-center gap-2"> |
| 209 | + <SlackColorIcon className="size-5" /> |
| 210 | + Slack support |
| 211 | + </span> |
| 212 | + </DialogHeader> |
| 213 | + <div className="flex flex-col gap-3 pt-3"> |
| 214 | + <Paragraph>{body}</Paragraph> |
| 215 | + {offer === "invite" && ( |
| 216 | + // GitHub sign-ups often have a personal or noreply email; the invite can't be redirected once sent. |
| 217 | + <Paragraph variant="small/dimmed"> |
| 218 | + Not the right email? <TextLink to={accountPath()}>Change it in your account</TextLink>{" "} |
| 219 | + first. |
| 220 | + </Paragraph> |
| 221 | + )} |
| 222 | + <FormButtons |
| 223 | + confirmButton={confirmButton} |
| 224 | + cancelButton={ |
| 225 | + onDismiss ? ( |
| 226 | + <Button variant="secondary/medium" onClick={onDismiss}> |
| 227 | + Don't show again |
| 228 | + </Button> |
| 229 | + ) : ( |
| 230 | + <DialogClose asChild> |
| 231 | + <Button variant="secondary/medium">Cancel</Button> |
| 232 | + </DialogClose> |
| 233 | + ) |
| 234 | + } |
| 235 | + /> |
| 236 | + </div> |
| 237 | + </DialogContent> |
| 238 | + </Dialog> |
| 239 | + ); |
| 240 | +} |
0 commit comments