Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileSurfaceChrome.tsx

160 lines5.0 KB

1import type { ReactNode } from "react";
2​
3import { Spinner } from "~/components/ui/spinner";
4import { Button } from "~/components/ui/button";
5import { Toggle } from "~/components/ui/toggle";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
7import { DIFF_SURFACE_THEME_UNSAFE_CSS } from "~/lib/diffRendering";
8import { cn } from "~/lib/utils";
9​
10/**
11 * One header row for every file surface in the side panel, whether the file
12 * comes from the workspace or was captured as an attachment: crumbs on the
13 * left, icon-only actions on the right. Attachments and workspace files must
14 * not grow separate chrome.
15 */
16export const FILE_SURFACE_SUBHEADER_CLASS =
17 "flex h-10 min-h-10 shrink-0 items-center gap-2 border-b border-border/60 bg-background px-3 in-data-[preview-panel-mode=inline]:mb-3 in-data-[preview-panel-mode=inline]:h-7 in-data-[preview-panel-mode=inline]:min-h-7 in-data-[preview-panel-mode=inline]:border-b-transparent";
18​
19export const FILE_LINK_REVEAL_ATTRIBUTE = "data-file-link-reveal";
20​
21export const FILE_LINK_REVEAL_UNSAFE_CSS = `
22 ${DIFF_SURFACE_THEME_UNSAFE_CSS}
23​
24 diffs-container {
25 --diffs-bg: var(--code-background, var(--background)) !important;
26 --diffs-light-bg: var(--code-background, var(--background)) !important;
27 --diffs-dark-bg: var(--code-background, var(--background)) !important;
28 background-color: var(--code-background, var(--background)) !important;
29 color: var(--code-foreground, var(--foreground)) !important;
30 }
31​
32 [${FILE_LINK_REVEAL_ATTRIBUTE}][data-line] {
33 background-color: light-dark(
34 color-mix(
35 in lab,
36 var(--diffs-computed-diff-line-bg) 82%,
37 var(--diffs-bg-selection-override, var(--diffs-selection-base))
38 ),
39 color-mix(
40 in lab,
41 var(--diffs-computed-diff-line-bg) 75%,
42 var(--diffs-bg-selection-override, var(--diffs-selection-base))
43 )
44 ) !important;
45 }
46​
47 [${FILE_LINK_REVEAL_ATTRIBUTE}][data-column-number] {
48 background-color: light-dark(
49 color-mix(
50 in lab,
51 var(--diffs-computed-diff-line-bg) 75%,
52 var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
53 ),
54 color-mix(
55 in lab,
56 var(--diffs-computed-diff-line-bg) 60%,
57 var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
58 )
59 ) !important;
60 color: var(--diffs-selection-number-fg) !important;
61 }
62`;
63​
64/**
65 * An icon-only header action with its label in a tooltip, the same control workspace files use.
66 * A `pressed` action is a toggle and says so; a command (Copy, Save, Close) is a plain button,
67 * because announcing it as an unpressed toggle tells a screen reader it has a state it has not.
68 */
69export function FileSurfaceAction(props: {
70 readonly label: string;
71 readonly pressed?: boolean;
72 readonly disabled?: boolean;
73 readonly onPress: () => void;
74 readonly children: ReactNode;
75}) {
76 const pressed = props.pressed;
77 return (
78 <Tooltip>
79 <TooltipTrigger
80 render={
81 pressed === undefined ? (
82 <Button
83 type="button"
84 className="shrink-0"
85 disabled={props.disabled ?? false}
86 onClick={props.onPress}
87 aria-label={props.label}
88 variant="ghost"
89 size="icon-sm"
90 >
91 {props.children}
92 </Button>
93 ) : (
94 <Toggle
95 className="shrink-0"
96 pressed={pressed}
97 disabled={props.disabled ?? false}
98 onPressedChange={props.onPress}
99 aria-label={props.label}
100 variant="ghost"
101 size="sm"
102 >
103 {props.children}
104 </Toggle>
105 )
106 }
107 />
108 <TooltipPopup>{props.label}</TooltipPopup>
109 </Tooltip>
110 );
112​
113export function FileSurfaceNotice(props: { readonly children: ReactNode }) {
114 return (
115 <div
116 role="status"
117 className="shrink-0 border-b border-warning/20 bg-warning-surface px-3 py-1.5 text-[11px] text-warning-foreground"
118 >
119 {props.children}
120 </div>
121 );
123​
124export function FileSurfaceLoading(props: { readonly className?: string }) {
125 return (
126 <div
127 role="status"
128 aria-label="Loading file"
129 className={cn(
130 "flex min-h-0 flex-1 items-center justify-center text-muted-foreground",
131 props.className,
132 )}
133 >
134 <Spinner className="size-5" />
135 </div>
136 );
138​
139export function FileSurfaceFailure(props: {
140 readonly message: string;
141 readonly onRetry?: () => void;
142}) {
143 return (
144 <div
145 role="alert"
146 className="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 px-6 text-center text-xs leading-relaxed"
147 >
148 <p className="text-destructive">{props.message}</p>
149 {props.onRetry ? (
150 <button
151 type="button"
152 onClick={props.onRetry}
153 className="rounded-md border border-input px-2.5 py-1 text-xs text-foreground hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring"
154 >
155 Try again
156 </button>
157 ) : null}
158 </div>
159 );