Open History to see the last commit
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
10export const FILE_SURFACE_SUBHEADER_CLASS =
11 "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";
12
13export const FILE_LINK_REVEAL_ATTRIBUTE = "data-file-link-reveal";
14
15export const FILE_LINK_REVEAL_UNSAFE_CSS = `
16 ${DIFF_SURFACE_THEME_UNSAFE_CSS}
17
18 diffs-container {
19 --diffs-bg: var(--code-background, var(--background)) !important;
20 --diffs-light-bg: var(--code-background, var(--background)) !important;
21 --diffs-dark-bg: var(--code-background, var(--background)) !important;
22 background-color: var(--code-background, var(--background)) !important;
23 color: var(--code-foreground, var(--foreground)) !important;
24 }
25
26 /* Tint through --diffs-line-bg, not background-color. The editor paints row
27 tints on a layer below its text selection; a background on the row itself
28 covers the selection and makes selected text on this line invisible. */
29 [${FILE_LINK_REVEAL_ATTRIBUTE}][data-line] {
30 --diffs-line-bg: light-dark(
31 color-mix(
32 in lab,
33 var(--diffs-computed-diff-line-bg) 82%,
34 var(--diffs-bg-selection-override, var(--diffs-selection-base))
35 ),
36 color-mix(
37 in lab,
38 var(--diffs-computed-diff-line-bg) 75%,
39 var(--diffs-bg-selection-override, var(--diffs-selection-base))
40 )
41 ) !important;
42 }
43
44 [${FILE_LINK_REVEAL_ATTRIBUTE}][data-column-number] {
45 background-color: light-dark(
46 color-mix(
47 in lab,
48 var(--diffs-computed-diff-line-bg) 75%,
49 var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
50 ),
51 color-mix(
52 in lab,
53 var(--diffs-computed-diff-line-bg) 60%,
54 var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
55 )
56 ) !important;
57 color: var(--diffs-selection-number-fg) !important;
58 }
59`;
60
61export function FileSurfaceAction(props: {
62 readonly label: string;
63 readonly pressed?: boolean;
64 readonly disabled?: boolean;
65 readonly onPress: () => void;
66 readonly children: ReactNode;
67}) {
68 const pressed = props.pressed;
69 return (
70 <Tooltip>
71 <TooltipTrigger
72 render={
73 pressed === undefined ? (
74 <Button
75 type="button"
76 className="shrink-0"
77 disabled={props.disabled ?? false}
78 onClick={props.onPress}
79 aria-label={props.label}
80 variant="ghost"
81 size="icon-sm"
82 >
83 {props.children}
84 </Button>
85 ) : (
86 <Toggle
87 className="shrink-0"
88 pressed={pressed}
89 disabled={props.disabled ?? false}
90 onPressedChange={props.onPress}
91 aria-label={props.label}
92 variant="ghost"
93 size="sm"
94 >
95 {props.children}
96 </Toggle>
97 )
98 }
99 />
100 <TooltipPopup>{props.label}</TooltipPopup>
101 </Tooltip>
102 );
103}
104
105export function FileSurfaceNotice(props: { readonly children: ReactNode }) {
106 return (
107 <div
108 role="status"
109 className="shrink-0 border-b border-warning/20 bg-warning-surface px-3 py-1.5 text-2xs text-warning-foreground"
110 >
111 {props.children}
112 </div>
113 );
114}
115
116export function FileSurfaceLoading(props: { readonly className?: string }) {
117 return (
118 <div
119 role="status"
120 aria-label="Loading file"
121 className={cn(
122 "flex min-h-0 flex-1 items-center justify-center text-muted-foreground",
123 props.className,
124 )}
125 >
126 <Spinner size="lg" />
127 </div>
128 );
129}
130
131export function FileSurfaceFailure(props: {
132 readonly message: string;
133 readonly onRetry?: () => void;
134}) {
135 return (
136 <div
137 role="alert"
138 className="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 px-6 text-center text-xs leading-relaxed"
139 >
140 <p className="text-destructive">{props.message}</p>
141 {props.onRetry ? (
142 <button
143 type="button"
144 onClick={props.onRetry}
145 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-inset focus-visible:ring-ring"
146 >
147 Try again
148 </button>
149 ) : null}
150 </div>
151 );
152}