|
| 1 | +"use client"; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright Contributors to the OpenCue Project |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import * as React from "react"; |
| 20 | +import { useSession } from "next-auth/react"; |
| 21 | + |
| 22 | +import { |
| 23 | + Dialog, |
| 24 | + DialogContent, |
| 25 | + DialogDescription, |
| 26 | + DialogFooter, |
| 27 | + DialogHeader, |
| 28 | + DialogTitle, |
| 29 | +} from "@/components/ui/dialog"; |
| 30 | +import { Button } from "@/components/ui/button"; |
| 31 | +import type { Job } from "@/app/jobs/columns"; |
| 32 | +import { addJobSubscriber } from "@/app/utils/action_utils"; |
| 33 | +import { toastWarning } from "@/app/utils/notify_utils"; |
| 34 | + |
| 35 | +/** |
| 36 | + * "Subscribe to Job" dialog. Mounted once at the page level and opened |
| 37 | + * in response to a `cueweb:open-subscribe-to-job` CustomEvent dispatched |
| 38 | + * from the row context menu. Decoupled this way so the menu's free- |
| 39 | + * function handlers can stay free of component refs. |
| 40 | + * |
| 41 | + * UI mirrors CueGUI's SubscribeToJobDialog: |
| 42 | + * |
| 43 | + * Subscribe to jobs <jobName> |
| 44 | + * |
| 45 | + * Job name |
| 46 | + * <jobName> |
| 47 | + * |
| 48 | + * From: <noreply address> |
| 49 | + * To: <user>@<domain> |
| 50 | + * |
| 51 | + * [ Save ] [ Cancel ] |
| 52 | + * |
| 53 | + * Save calls the Cuebot AddSubscriber RPC via /api/job/action/addsubscriber; |
| 54 | + * Cuebot emails the subscriber when the job finishes. The From address is |
| 55 | + * informational - the actual sender is whatever Cuebot is configured with. |
| 56 | + * |
| 57 | + * Defaults are configurable at build time: |
| 58 | + * |
| 59 | + * NEXT_PUBLIC_EMAIL_DOMAIN - domain part of the default "To" |
| 60 | + * address. Defaults to |
| 61 | + * "your.domain.com". |
| 62 | + * NEXT_PUBLIC_SUBSCRIBE_FROM_EMAIL - the informational From label. |
| 63 | + * Defaults to |
| 64 | + * "opencue-noreply@<EMAIL_DOMAIN>". |
| 65 | + */ |
| 66 | + |
| 67 | +export const OPEN_SUBSCRIBE_TO_JOB_EVENT = "cueweb:open-subscribe-to-job"; |
| 68 | + |
| 69 | +export type OpenSubscribeToJobDetail = { |
| 70 | + job: Job; |
| 71 | +}; |
| 72 | + |
| 73 | +const EMAIL_DOMAIN = ( |
| 74 | + process.env.NEXT_PUBLIC_EMAIL_DOMAIN ?? "your.domain.com" |
| 75 | +).trim(); |
| 76 | +const FROM_EMAIL = ( |
| 77 | + process.env.NEXT_PUBLIC_SUBSCRIBE_FROM_EMAIL ?? |
| 78 | + `opencue-noreply@${EMAIL_DOMAIN}` |
| 79 | +).trim(); |
| 80 | + |
| 81 | +function defaultToFor(user: string): string { |
| 82 | + const safeUser = user?.trim() || "you"; |
| 83 | + return `${safeUser}@${EMAIL_DOMAIN}`; |
| 84 | +} |
| 85 | + |
| 86 | +// RFC 5322-ish: just enough to reject obvious typos before hitting the |
| 87 | +// backend. Cuebot does its own validation server-side. |
| 88 | +const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 89 | + |
| 90 | +export function SubscribeToJobDialog() { |
| 91 | + const { data: session } = useSession(); |
| 92 | + const [open, setOpen] = React.useState(false); |
| 93 | + const [job, setJob] = React.useState<Job | null>(null); |
| 94 | + const [to, setTo] = React.useState(""); |
| 95 | + const [busy, setBusy] = React.useState(false); |
| 96 | + |
| 97 | + React.useEffect(() => { |
| 98 | + function handler(e: Event) { |
| 99 | + const detail = (e as CustomEvent<OpenSubscribeToJobDetail>).detail; |
| 100 | + if (!detail?.job) return; |
| 101 | + const j = detail.job; |
| 102 | + // Prefer the signed-in user's email when available; otherwise fall |
| 103 | + // back to <sessionName-or-jobUser>@<EMAIL_DOMAIN>, matching the |
| 104 | + // CueGUI default of getpass.getuser()+EMAIL_DOMAIN. |
| 105 | + const sessionEmail = session?.user?.email?.trim(); |
| 106 | + const userFallback = session?.user?.name?.trim() || j.user; |
| 107 | + setJob(j); |
| 108 | + setTo(sessionEmail || defaultToFor(userFallback)); |
| 109 | + setOpen(true); |
| 110 | + } |
| 111 | + window.addEventListener(OPEN_SUBSCRIBE_TO_JOB_EVENT, handler); |
| 112 | + return () => |
| 113 | + window.removeEventListener(OPEN_SUBSCRIBE_TO_JOB_EVENT, handler); |
| 114 | + }, [session?.user?.email, session?.user?.name]); |
| 115 | + |
| 116 | + async function handleSave() { |
| 117 | + if (!job) return; |
| 118 | + const subscriber = to.trim(); |
| 119 | + if (!EMAIL_RE.test(subscriber)) { |
| 120 | + toastWarning("Enter a valid email address before saving."); |
| 121 | + return; |
| 122 | + } |
| 123 | + setBusy(true); |
| 124 | + try { |
| 125 | + await addJobSubscriber(job, subscriber); |
| 126 | + setOpen(false); |
| 127 | + } finally { |
| 128 | + setBusy(false); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return ( |
| 133 | + <Dialog open={open} onOpenChange={(o) => !busy && setOpen(o)}> |
| 134 | + <DialogContent className="sm:max-w-xl"> |
| 135 | + <DialogHeader> |
| 136 | + <DialogTitle> |
| 137 | + <span className="font-mono break-all"> |
| 138 | + Subscribe to jobs {job?.name ?? "Job"} |
| 139 | + </span> |
| 140 | + </DialogTitle> |
| 141 | + <DialogDescription> |
| 142 | + On Save the address is registered as a subscriber on Cuebot; |
| 143 | + you'll receive an email from Cuebot when the job finishes. The |
| 144 | + <span className="font-mono"> From:</span> address shown is |
| 145 | + informational - the real sender is whatever Cuebot is |
| 146 | + configured with. |
| 147 | + </DialogDescription> |
| 148 | + </DialogHeader> |
| 149 | + |
| 150 | + <div className="grid gap-3 py-2 text-sm"> |
| 151 | + <div> |
| 152 | + <div className="text-foreground/70">Job name</div> |
| 153 | + <div className="mt-1 rounded-md border border-input bg-foreground/[0.04] px-3 py-2 font-mono text-xs break-all"> |
| 154 | + {job?.name ?? ""} |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + |
| 158 | + <div className="flex items-center gap-3"> |
| 159 | + <label |
| 160 | + htmlFor="subscribe-to-job-from" |
| 161 | + className="w-16 shrink-0 text-right text-foreground/70" |
| 162 | + > |
| 163 | + From: |
| 164 | + </label> |
| 165 | + <span |
| 166 | + id="subscribe-to-job-from" |
| 167 | + className="flex-1 rounded-md border border-input bg-foreground/[0.04] px-3 py-1.5 font-mono text-xs" |
| 168 | + > |
| 169 | + {FROM_EMAIL} |
| 170 | + </span> |
| 171 | + </div> |
| 172 | + |
| 173 | + <div className="flex items-center gap-3"> |
| 174 | + <label |
| 175 | + htmlFor="subscribe-to-job-to" |
| 176 | + className="w-16 shrink-0 text-right text-foreground/70" |
| 177 | + > |
| 178 | + To: |
| 179 | + </label> |
| 180 | + <input |
| 181 | + id="subscribe-to-job-to" |
| 182 | + type="email" |
| 183 | + value={to} |
| 184 | + onChange={(e) => setTo(e.target.value)} |
| 185 | + autoFocus |
| 186 | + className="flex-1 rounded-md border border-input bg-background px-3 py-1.5 text-sm" |
| 187 | + /> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + |
| 191 | + <DialogFooter> |
| 192 | + <Button |
| 193 | + type="button" |
| 194 | + variant="outline" |
| 195 | + onClick={() => setOpen(false)} |
| 196 | + disabled={busy} |
| 197 | + > |
| 198 | + Cancel |
| 199 | + </Button> |
| 200 | + <Button |
| 201 | + type="button" |
| 202 | + onClick={handleSave} |
| 203 | + disabled={busy || !to.trim()} |
| 204 | + > |
| 205 | + {busy ? "Saving..." : "Save"} |
| 206 | + </Button> |
| 207 | + </DialogFooter> |
| 208 | + </DialogContent> |
| 209 | + </Dialog> |
| 210 | + ); |
| 211 | +} |
0 commit comments