Elffuss BraidHub

kikocis/continue public

eaabf65a9f8d · 3.7 KiBRawPermalink
1import { DocumentTextIcon, GlobeAltIcon } from "@heroicons/react/24/outline";
2import { RuleSource, RuleWithSource } from "core";
3import { ComponentType, useMemo, useState } from "react";
4import ToggleDiv from "../../ToggleDiv";
5
6interface RulesPeekProps {
7 appliedRules?: RuleWithSource[];
8 icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
9}
10
11interface RulesPeekItemProps {
12 rule: RuleWithSource;
13}
14
15// Convert technical source to user-friendly text
16const getSourceLabel = (source: RuleSource): string => {
17 switch (source) {
18 case "default-chat":
19 return "Default Chat";
20 case "default-agent":
21 return "Default Agent";
22 case "model-options-chat":
23 return "Model Chat Options";
24 case "model-options-plan":
25 return "Model Plan Options";
26 case "model-options-agent":
27 return "Model Agent Options";
28 case "rules-block":
29 return "Rules Block";
30 case "json-systemMessage":
31 return "System Message";
32 case ".continuerules":
33 return "Project Rules";
34 default:
35 return source;
36 }
37};
38
39export function RulesPeekItem({ rule }: RulesPeekItemProps) {
40 const isGlobal = rule.alwaysApply ?? !rule.globs;
41 const [expanded, setExpanded] = useState(false);
42
43 // Define maximum length for rule text display
44 const maxRuleLength = 100;
45 const isRuleLong = rule.rule.length > maxRuleLength;
46
47 // Get the displayed rule text based on expanded state
48 const displayedRule =
49 isRuleLong && !expanded
50 ? `${rule.rule.slice(0, maxRuleLength)}...`
51 : rule.rule;
52
53 const toggleExpand = () => {
54 if (isRuleLong) {
55 setExpanded(!expanded);
56 }
57 };
58
59 return (
60 <div
61 className={`group mr-2 flex flex-col overflow-hidden rounded px-1.5 py-1 text-xs hover:bg-white/10 ${isRuleLong ? "cursor-pointer hover:text-gray-200" : ""}`}
62 data-testid="rules-peek-item"
63 onClick={toggleExpand}
64 >
65 <div className="flex w-full items-center">
66 {isGlobal ? (
67 <GlobeAltIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" />
68 ) : (
69 <DocumentTextIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" />
70 )}
71
72 <div className="flex min-w-0 flex-1 gap-2 text-xs">
73 <div className="max-w-[50%] flex-shrink-0 truncate font-medium">
74 {rule.name || "Assistant rule"}
75 </div>
76
77 <div className="min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-500">
78 {isGlobal
79 ? "Always applied"
80 : `Pattern: ${typeof rule.globs === "string" ? rule.globs : Array.isArray(rule.globs) ? rule.globs.join(", ") : ""}`}
81 </div>
82 </div>
83 </div>
84 <div
85 className={`mt-1 whitespace-pre-line pl-6 pr-2 text-xs italic text-gray-300`}
86 title={
87 isRuleLong ? (expanded ? "Click to collapse" : "Click to expand") : ""
88 }
89 >
90 {displayedRule}
91 {isRuleLong && (
92 <span className="text-description-muted ml-1 opacity-0 transition-opacity group-hover:opacity-100">
93 {expanded ? "(collapse)" : "(expand)"}
94 </span>
95 )}
96 </div>
97 <div className="mt-1 pl-6 pr-2 text-xs text-gray-500">
98 Source: {getSourceLabel(rule.source)}
99 </div>
100 </div>
101 );
102}
103
104export function RulesPeek({ appliedRules, icon }: RulesPeekProps) {
105 const rules = useMemo(() => {
106 return appliedRules ?? [];
107 }, [appliedRules]);
108
109 if (!rules || rules.length === 0) {
110 return null;
111 }
112
113 return (
114 <ToggleDiv
115 icon={icon}
116 title={`${rules.length} rule${rules.length > 1 ? "s" : ""}`}
117 testId="rules-peek"
118 >
119 {rules.map((rule, idx) => (
120 <RulesPeekItem key={`rule-${idx}`} rule={rule} />
121 ))}
122 </ToggleDiv>
123 );
124}