TicketsView.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { useCallback, useEffect, useRef, useState } from "react";
  2. import { Check, CopyIcon, Inbox, Loader2, PlusCircle, RefreshCcw } from "lucide-react";
  3. import type { FeedbackTicketSummary } from "@zcode/shared";
  4. import type { IFeedbackService } from "@zcode/services";
  5. import { Button } from "@/components/ui/button.js";
  6. import { cn } from "@/components/lib/utils.js";
  7. import { FeedbackErrorTip, StatusIndicator } from "@/feedback/feedbackBadges.js";
  8. import { ScrollFadeViewport } from "@/components/ui/scroll-fade-viewport.js";
  9. import { useFeedbackStore } from "@/feedback/feedbackStore.js";
  10. import { formatRelativeTime } from "@/feedback/feedbackUserView.js";
  11. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  12. import { getErrorMessage } from "@/lib/errorMessage.js";
  13. import { logger } from "@/logger.js";
  14. export function TicketsView({
  15. feedbackService,
  16. onCreateNew,
  17. }: {
  18. feedbackService: IFeedbackService;
  19. onCreateNew: () => void;
  20. }) {
  21. const selectedTicketId = useFeedbackStore((state) => state.selectedTicketId);
  22. const { intl } = useZCodeIntl();
  23. const formatMessage = intl.formatMessage;
  24. const copiedResetTimerRef = useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);
  25. const [items, setItems] = useState<FeedbackTicketSummary[]>([]);
  26. const [loadingList, setLoadingList] = useState(true);
  27. const [error, setError] = useState<string | null>(null);
  28. const [copiedTicketId, setCopiedTicketId] = useState<string | null>(null);
  29. const loadList = useCallback(async () => {
  30. setLoadingList(true);
  31. setError(null);
  32. try {
  33. const result = await feedbackService.list({ mine: true, limit: 50 });
  34. setItems(result.items);
  35. } catch (loadError) {
  36. setError(getErrorMessage(loadError));
  37. } finally {
  38. setLoadingList(false);
  39. }
  40. }, [feedbackService]);
  41. useEffect(() => {
  42. void loadList();
  43. }, [loadList]);
  44. useEffect(() => {
  45. return () => {
  46. if (copiedResetTimerRef.current !== null) {
  47. globalThis.clearTimeout(copiedResetTimerRef.current);
  48. }
  49. };
  50. }, []);
  51. const handleCopyTicketId = useCallback(
  52. (ticketId: string) => {
  53. if (typeof navigator === "undefined" || !navigator.clipboard?.writeText) {
  54. logger.warn(formatMessage({ id: "feedback.detail.issueCopyFailed" }), {
  55. issueId: ticketId,
  56. reason: "clipboard-unavailable",
  57. });
  58. return;
  59. }
  60. void navigator.clipboard.writeText(ticketId).then(
  61. () => {
  62. setCopiedTicketId(ticketId);
  63. if (copiedResetTimerRef.current !== null) {
  64. globalThis.clearTimeout(copiedResetTimerRef.current);
  65. }
  66. copiedResetTimerRef.current = globalThis.setTimeout(() => {
  67. setCopiedTicketId(null);
  68. copiedResetTimerRef.current = null;
  69. }, 1600);
  70. },
  71. (copyError: unknown) => {
  72. logger.warn(formatMessage({ id: "feedback.detail.issueCopyFailed" }), {
  73. issueId: ticketId,
  74. error: copyError instanceof Error ? copyError.message : String(copyError),
  75. });
  76. },
  77. );
  78. },
  79. [formatMessage],
  80. );
  81. return (
  82. <div className="flex min-h-0 flex-1 flex-col overflow-hidden bg-popover">
  83. {loadingList && items.length === 0 ? (
  84. <TicketsInitialLoadingState />
  85. ) : (
  86. <ScrollFadeViewport className="px-6 pb-4 pt-3">
  87. {error && items.length === 0 ? (
  88. <div className="pb-2">
  89. <FeedbackErrorTip message={error} />
  90. </div>
  91. ) : null}
  92. {!loadingList && items.length === 0 && !error ? (
  93. <EmptyState onCreateNew={onCreateNew} />
  94. ) : null}
  95. <FeedbackTicketList
  96. items={items}
  97. selectedTicketId={selectedTicketId}
  98. copiedTicketId={copiedTicketId}
  99. onCopyTicketId={handleCopyTicketId}
  100. />
  101. {loadingList && items.length > 0 ? (
  102. <div className="flex items-center justify-center py-2 text-ui-xs text-foreground-subtle">
  103. <Loader2 className="mr-1.5 size-3 animate-spin" />
  104. {formatMessage({ id: "common.loading" })}
  105. </div>
  106. ) : null}
  107. </ScrollFadeViewport>
  108. )}
  109. <div className="flex shrink-0 items-center justify-between gap-2 px-6 pb-6 pt-2">
  110. <Button
  111. type="button"
  112. variant="ghost"
  113. size="icon-lg"
  114. onClick={() => void loadList()}
  115. disabled={loadingList}
  116. aria-label={formatMessage({ id: "common.refresh" })}
  117. className="rounded-xl text-foreground-subtle hover:text-foreground"
  118. >
  119. {loadingList ? (
  120. <Loader2 className="size-4 animate-spin" />
  121. ) : (
  122. <RefreshCcw className="size-4" />
  123. )}
  124. </Button>
  125. <Button size="lg" onClick={onCreateNew} className="rounded-lg">
  126. <PlusCircle />
  127. {formatMessage({ id: "feedback.tickets.newFeedback" })}
  128. </Button>
  129. </div>
  130. </div>
  131. );
  132. }
  133. function TicketsInitialLoadingState() {
  134. const { intl } = useZCodeIntl();
  135. return (
  136. <div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-2 px-6 py-8 text-center text-ui-base text-foreground-subtle">
  137. <Loader2 className="size-4 animate-spin text-primary" />
  138. <span>{intl.formatMessage({ id: "common.loading" })}</span>
  139. </div>
  140. );
  141. }
  142. function FeedbackTicketList({
  143. items,
  144. selectedTicketId,
  145. copiedTicketId,
  146. onCopyTicketId,
  147. }: {
  148. items: FeedbackTicketSummary[];
  149. selectedTicketId: string | null;
  150. copiedTicketId: string | null;
  151. onCopyTicketId: (ticketId: string) => void;
  152. }) {
  153. const { intl } = useZCodeIntl();
  154. const formatMessage = intl.formatMessage;
  155. if (items.length === 0) return null;
  156. return (
  157. <div className="overflow-hidden rounded-lg border border-border bg-card">
  158. <div className="hidden grid-cols-[7.5rem_minmax(0,1fr)_13rem] gap-3 border-b border-border px-3 py-2 text-ui-xs font-medium text-foreground-subtle md:grid">
  159. <span>{formatMessage({ id: "feedback.tickets.column.status" })}</span>
  160. <span>{formatMessage({ id: "feedback.tickets.column.description" })}</span>
  161. <span>{formatMessage({ id: "feedback.tickets.column.id" })}</span>
  162. </div>
  163. <ul className="divide-y divide-border">
  164. {items.map((item) => {
  165. const selected = selectedTicketId === item.id;
  166. const copied = copiedTicketId === item.id;
  167. const copyLabel = formatMessage({
  168. id: copied ? "feedback.detail.issueCopied" : "feedback.detail.issueCopy",
  169. });
  170. return (
  171. <li
  172. key={item.id}
  173. className={cn(
  174. "grid gap-2 px-3 py-2.5 md:grid-cols-[7.5rem_minmax(0,1fr)_13rem] md:items-center md:gap-3",
  175. selected ? "bg-card-selected" : "bg-card",
  176. )}
  177. >
  178. <div className="flex min-w-0 items-center justify-between gap-2 md:block">
  179. <StatusIndicator status={item.status} />
  180. <span className="shrink-0 text-ui-xs text-foreground-subtle md:hidden">
  181. {formatRelativeTime(item.created_at, formatMessage)}
  182. </span>
  183. </div>
  184. <div className="min-w-0">
  185. <div className="line-clamp-2 text-ui-base leading-5 text-foreground md:truncate">
  186. {item.title}
  187. </div>
  188. <div className="mt-1 hidden text-ui-xs text-foreground-subtle md:block">
  189. {formatRelativeTime(item.created_at, formatMessage)}
  190. </div>
  191. </div>
  192. <div className="flex min-w-0 items-center gap-1.5">
  193. <span className="shrink-0 text-ui-xs text-foreground-subtle md:hidden">
  194. {formatMessage({ id: "feedback.tickets.issuePrefix" })}
  195. </span>
  196. <span className="min-w-0 flex-1 truncate font-mono text-ui-xs text-foreground">
  197. {item.id}
  198. </span>
  199. <Button
  200. type="button"
  201. size="icon-sm"
  202. variant="ghost"
  203. aria-label={copyLabel}
  204. title={copyLabel}
  205. onClick={() => onCopyTicketId(item.id)}
  206. className="shrink-0 text-foreground-subtle hover:text-foreground"
  207. >
  208. {copied ? (
  209. <Check className="size-3 text-success" />
  210. ) : (
  211. <CopyIcon className="size-3" />
  212. )}
  213. </Button>
  214. </div>
  215. </li>
  216. );
  217. })}
  218. </ul>
  219. </div>
  220. );
  221. }
  222. function EmptyState({ onCreateNew }: { onCreateNew: () => void }) {
  223. const { intl } = useZCodeIntl();
  224. const formatMessage = intl.formatMessage;
  225. return (
  226. <div className="flex flex-col items-center justify-center gap-2 px-4 pt-12 text-center">
  227. <div className="flex size-9 items-center justify-center rounded-lg border border-border bg-card">
  228. <Inbox className="size-4 text-foreground-subtle" />
  229. </div>
  230. <div className="text-ui-base font-medium text-foreground">
  231. {formatMessage({ id: "feedback.tickets.empty.title" })}
  232. </div>
  233. <p className="text-ui-base leading-5 text-foreground-subtle">
  234. {formatMessage({ id: "feedback.tickets.empty.description" })}
  235. </p>
  236. <Button size="sm" variant="outline" onClick={onCreateNew} className="mt-1 rounded-lg">
  237. <PlusCircle />
  238. {formatMessage({ id: "feedback.tickets.empty.action" })}
  239. </Button>
  240. </div>
  241. );
  242. }