|
1 | 1 | import type { WorkspaceApp } from "api/typesGenerated";
|
| 2 | +import { Button } from "components/Button/Button"; |
| 3 | +import { |
| 4 | + DropdownMenu, |
| 5 | + DropdownMenuContent, |
| 6 | + DropdownMenuItem, |
| 7 | + DropdownMenuTrigger, |
| 8 | +} from "components/DropdownMenu/DropdownMenu"; |
| 9 | +import { |
| 10 | + EllipsisVertical, |
| 11 | + ExternalLinkIcon, |
| 12 | + HouseIcon, |
| 13 | + RotateCwIcon, |
| 14 | +} from "lucide-react"; |
| 15 | +import { openAppInNewWindow } from "modules/apps/apps"; |
2 | 16 | import { useAppLink } from "modules/apps/useAppLink";
|
3 | 17 | import type { Task } from "modules/tasks/tasks";
|
4 |
| -import type { FC } from "react"; |
| 18 | +import { type FC, useRef } from "react"; |
5 | 19 | import { cn } from "utils/cn";
|
6 | 20 |
|
7 | 21 | type TaskAppIFrameProps = {
|
@@ -31,24 +45,85 @@ export const TaskAppIFrame: FC<TaskAppIFrameProps> = ({
|
31 | 45 | workspace: task.workspace,
|
32 | 46 | });
|
33 | 47 |
|
34 |
| - let href = link.href; |
35 |
| - try { |
36 |
| - const url = new URL(link.href); |
37 |
| - if (pathname) { |
38 |
| - url.pathname = pathname; |
| 48 | + const appHref = (): string => { |
| 49 | + try { |
| 50 | + const url = new URL(link.href, location.href); |
| 51 | + if (pathname) { |
| 52 | + url.pathname = pathname; |
| 53 | + } |
| 54 | + return url.toString(); |
| 55 | + } catch (err) { |
| 56 | + console.warn(`Failed to parse URL ${link.href} for app ${app.id}`, err); |
| 57 | + return link.href; |
39 | 58 | }
|
40 |
| - href = url.toString(); |
41 |
| - } catch (err) { |
42 |
| - console.warn(`Failed to parse URL ${link.href} for app ${app.id}`, err); |
43 |
| - } |
| 59 | + }; |
| 60 | + |
| 61 | + const frameRef = useRef<HTMLIFrameElement>(null); |
| 62 | + const frameSrc = appHref(); |
44 | 63 |
|
45 | 64 | return (
|
46 |
| - <iframe |
47 |
| - src={href} |
48 |
| - title={link.label} |
49 |
| - loading="eager" |
50 |
| - className={cn([active ? "block" : "hidden", "w-full h-full border-0"])} |
51 |
| - allow="clipboard-read; clipboard-write" |
52 |
| - /> |
| 65 | + <div className={cn([active ? "block" : "hidden", "w-full h-ful"])}> |
| 66 | + <div className="bg-surface-tertiary flex items-center p-2 py-1 gap-1"> |
| 67 | + <Button |
| 68 | + size="icon" |
| 69 | + variant="subtle" |
| 70 | + onClick={(e) => { |
| 71 | + e.preventDefault(); |
| 72 | + if (frameRef.current?.contentWindow) { |
| 73 | + frameRef.current.contentWindow.location.reload(); |
| 74 | + } |
| 75 | + }} |
| 76 | + > |
| 77 | + <RotateCwIcon /> |
| 78 | + <span className="sr-only">Refresh</span> |
| 79 | + </Button> |
| 80 | + |
| 81 | + <Button |
| 82 | + size="icon" |
| 83 | + variant="subtle" |
| 84 | + onClick={(e) => { |
| 85 | + e.preventDefault(); |
| 86 | + if (frameRef.current?.contentWindow) { |
| 87 | + frameRef.current.contentWindow.location.href = appHref(); |
| 88 | + } |
| 89 | + }} |
| 90 | + > |
| 91 | + <HouseIcon /> |
| 92 | + <span className="sr-only">Home</span> |
| 93 | + </Button> |
| 94 | + |
| 95 | + {/* Possibly we will put a URL bar here, but for now we cannot due to |
| 96 | + * cross-origin restrictions in iframes. */} |
| 97 | + <div className="w-full"></div> |
| 98 | + |
| 99 | + <DropdownMenu> |
| 100 | + <DropdownMenuTrigger asChild> |
| 101 | + <Button size="icon" variant="subtle" aria-label="More options"> |
| 102 | + <EllipsisVertical aria-hidden="true" /> |
| 103 | + <span className="sr-only">More options</span> |
| 104 | + </Button> |
| 105 | + </DropdownMenuTrigger> |
| 106 | + <DropdownMenuContent align="end"> |
| 107 | + <DropdownMenuItem |
| 108 | + onClick={() => { |
| 109 | + openAppInNewWindow(frameSrc); |
| 110 | + }} |
| 111 | + > |
| 112 | + <ExternalLinkIcon /> |
| 113 | + Open app in new tab |
| 114 | + </DropdownMenuItem> |
| 115 | + </DropdownMenuContent> |
| 116 | + </DropdownMenu> |
| 117 | + </div> |
| 118 | + |
| 119 | + <iframe |
| 120 | + ref={frameRef} |
| 121 | + src={frameSrc} |
| 122 | + title={link.label} |
| 123 | + loading="eager" |
| 124 | + className={"w-full h-full border-0"} |
| 125 | + allow="clipboard-read; clipboard-write" |
| 126 | + /> |
| 127 | + </div> |
53 | 128 | );
|
54 | 129 | };
|
0 commit comments