WorkspaceHelpMenuButton.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { useState } from "react";
  2. import {
  3. DesktopCommandIds,
  4. TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER,
  5. TID_WORKSPACE_HELP_MENU_TRIGGER,
  6. } from "@zcode/shared";
  7. import {
  8. ActivityIcon,
  9. BookOpenIcon,
  10. CircleHelpIcon,
  11. DownloadIcon,
  12. LightbulbIcon,
  13. InfoIcon,
  14. MessageSquareIcon,
  15. UsersIcon,
  16. RefreshCwIcon,
  17. ShieldAlertIcon,
  18. } from "lucide-react";
  19. import { Badge } from "@/components/ui/badge.js";
  20. import { Button } from "@/components/ui/button.js";
  21. import {
  22. DropdownMenu,
  23. DropdownMenuContent,
  24. DropdownMenuItem,
  25. DropdownMenuSeparator,
  26. DropdownMenuTrigger,
  27. } from "@/components/ui/dropdown-menu.js";
  28. import { cn } from "@/components/lib/utils.js";
  29. import { ControlHintTooltip } from "@/ControlHintTooltip.js";
  30. import { useFeedbackStore } from "@/feedback/feedbackStore.js";
  31. import { useDesktopUpdateMenu } from "@/hooks/useDesktopUpdateMenu.js";
  32. import { usePlatform } from "@/hooks/usePlatform.js";
  33. import { useVulnerabilityReportUrl } from "@/hooks/useVulnerabilityReportUrl.js";
  34. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  35. import { createHelpMenuActionHandlers } from "@/lib/helpMenuActions.js";
  36. export function WorkspaceHelpMenuButton({
  37. className,
  38. isDesktop = false,
  39. }: {
  40. className?: string;
  41. /**
  42. * 是否桌面端。由挂载处注入而不是在组件内嗅探:Web 的 IPlatformService 桩同样实现了
  43. * executeDesktopCommand(no-op),拿它判定会让 Web 端出现一个点了没反应的「资源管理器」。
  44. */
  45. isDesktop?: boolean;
  46. }) {
  47. const { intl } = useZCodeIntl();
  48. const platform = usePlatform();
  49. const [open, setOpen] = useState(false);
  50. const { url: vulnerabilityReportUrl, loading: vulnerabilityReportLoading } =
  51. useVulnerabilityReportUrl(open);
  52. const updateMenu = useDesktopUpdateMenu(isDesktop);
  53. const openFeedbackSubmit = useFeedbackStore((state) => state.openSubmit);
  54. const openFeatureRequest = useFeedbackStore((state) => state.openFeatureRequest);
  55. const helpMenuLabel = intl.formatMessage({ id: "workspaceHeader.help.menu" });
  56. const helpMenuActions = createHelpMenuActionHandlers({
  57. platform,
  58. intl,
  59. openSubmit: openFeedbackSubmit,
  60. });
  61. const handleOpenCommunity = () => {
  62. void platform.openCommunity();
  63. };
  64. const handleOpenResourceManager = () => {
  65. void platform.executeDesktopCommand(DesktopCommandIds.OpenResourceManager);
  66. };
  67. const handleShowAbout = () => {
  68. void platform.executeDesktopCommand(DesktopCommandIds.ShowAbout);
  69. };
  70. return (
  71. <DropdownMenu open={open} onOpenChange={setOpen}>
  72. <ControlHintTooltip title={helpMenuLabel} side="bottom">
  73. <DropdownMenuTrigger asChild>
  74. <Button
  75. type="button"
  76. variant="ghost"
  77. size="icon-md"
  78. // Settings 页会把帮助按钮绝对定位在 Electron 顶部拖拽区上方。
  79. // 只依赖外层容器 no-drag 时,真实 trigger 仍可能被标题栏 drag 区吞掉点击。
  80. className={cn(
  81. "text-foreground hover:bg-hover hover:text-foreground [app-region:no-drag]",
  82. className,
  83. )}
  84. aria-label={helpMenuLabel}
  85. data-testid={TID_WORKSPACE_HELP_MENU_TRIGGER}
  86. >
  87. <CircleHelpIcon className="size-4" />
  88. </Button>
  89. </DropdownMenuTrigger>
  90. </ControlHintTooltip>
  91. <DropdownMenuContent
  92. align="end"
  93. className="min-w-0 w-max [&_[data-slot=dropdown-menu-item]]:pr-6"
  94. >
  95. <DropdownMenuItem onSelect={helpMenuActions.openProductDocs}>
  96. <BookOpenIcon className="size-4" />
  97. {intl.formatMessage({ id: "workspaceHeader.help.docs" })}
  98. </DropdownMenuItem>
  99. <DropdownMenuItem onSelect={handleOpenCommunity}>
  100. <UsersIcon className="size-4" />
  101. {intl.formatMessage({ id: "workspaceHeader.help.community" })}
  102. </DropdownMenuItem>
  103. <DropdownMenuItem onSelect={helpMenuActions.openIssueReport}>
  104. <MessageSquareIcon className="size-4" />
  105. {intl.formatMessage({ id: "workspaceHeader.help.issueReport" })}
  106. </DropdownMenuItem>
  107. {vulnerabilityReportLoading || vulnerabilityReportUrl ? (
  108. <DropdownMenuItem
  109. disabled={vulnerabilityReportLoading}
  110. aria-busy={vulnerabilityReportLoading}
  111. onSelect={
  112. vulnerabilityReportUrl
  113. ? () => platform.openExternal(vulnerabilityReportUrl)
  114. : undefined
  115. }
  116. >
  117. <ShieldAlertIcon className="size-4" />
  118. {intl.formatMessage({ id: "workspaceHeader.help.reportVulnerability" })}
  119. </DropdownMenuItem>
  120. ) : null}
  121. <DropdownMenuItem onSelect={openFeatureRequest}>
  122. <LightbulbIcon className="size-4" />
  123. {intl.formatMessage({ id: "workspaceHeader.help.productRequest" })}
  124. </DropdownMenuItem>
  125. {/* Windows/Linux 没有原生菜单栏,自绘标题栏箭头菜单也已下线,
  126. 资源管理器只能从这里进;Web 端没有该窗口,不渲染。 */}
  127. {isDesktop ? (
  128. <>
  129. <DropdownMenuSeparator />
  130. {/* 独立导出入口曾被移除;复用原菜单动作,保持打包、系统定位与提示一致。 */}
  131. <DropdownMenuItem onSelect={helpMenuActions.exportLogs}>
  132. <DownloadIcon className="size-4" />
  133. {intl.formatMessage({ id: "titleBar.menu.help.exportLogs" })}
  134. </DropdownMenuItem>
  135. <DropdownMenuItem
  136. data-testid={TID_WORKSPACE_HELP_MENU_RESOURCE_MANAGER}
  137. onSelect={handleOpenResourceManager}
  138. >
  139. <ActivityIcon className="size-4" />
  140. {intl.formatMessage({ id: "titleBar.menu.help.resourceManager" })}
  141. </DropdownMenuItem>
  142. {updateMenu.visible ? (
  143. <DropdownMenuItem
  144. disabled={updateMenu.disabled}
  145. onSelect={updateMenu.checkForUpdates}
  146. >
  147. <RefreshCwIcon className="size-4" />
  148. {updateMenu.labelId === "desktopMenu.help.restartToUpdate" ? (
  149. <>
  150. <span className="whitespace-nowrap">
  151. {intl.formatMessage({ id: "desktopMenu.help.restartUpdateAction" })}
  152. </span>
  153. <Badge
  154. variant="secondary"
  155. className="h-4 px-1.5 py-0 bg-success/10 text-success"
  156. >
  157. {updateMenu.labelValues?.version}
  158. </Badge>
  159. </>
  160. ) : (
  161. intl.formatMessage({ id: updateMenu.labelId }, updateMenu.labelValues)
  162. )}
  163. </DropdownMenuItem>
  164. ) : null}
  165. <DropdownMenuItem onSelect={handleShowAbout}>
  166. <InfoIcon className="size-4" />
  167. {intl.formatMessage({ id: "titleBar.menu.help.about" })}
  168. </DropdownMenuItem>
  169. </>
  170. ) : null}
  171. </DropdownMenuContent>
  172. </DropdownMenu>
  173. );
  174. }