Elffuss BraidHub

kikocis/continue public

main · 3.1 KiBRawPermalink
1import { DocumentTextIcon, GlobeAltIcon } from "@heroicons/react/24/outline";
2import { RuleWithSource } from "core";
3import { getRuleSourceDisplayName } from "core/llm/rules/rules-utils";
4import { ComponentType, useMemo, useState } from "react";
5import ToggleDiv from "../../ToggleDiv";
6
7interface RulesPeekProps {
8 appliedRules?: RuleWithSource[];
9 icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
10}
11
12interface RulesPeekItemProps {
13 rule: RuleWithSource;
14}
15
16export function RulesPeekItem({ rule }: RulesPeekItemProps) {
17 const isGlobal = rule.alwaysApply ?? !rule.globs;
18 const [expanded, setExpanded] = useState(false);
19
20 // Define maximum length for rule text display
21 const maxRuleLength = 100;
22 const isRuleLong = rule.rule.length > maxRuleLength;
23
24 // Get the displayed rule text based on expanded state
25 const displayedRule =
26 isRuleLong && !expanded
27 ? `${rule.rule.slice(0, maxRuleLength)}...`
28 : rule.rule;
29
30 const toggleExpand = () => {
31 if (isRuleLong) {
32 setExpanded(!expanded);
33 }
34 };
35
36 return (
37 <div
38 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" : ""}`}
39 data-testid="rules-peek-item"
40 onClick={toggleExpand}
41 >
42 <div className="flex w-full items-center">
43 {isGlobal ? (
44 <GlobeAltIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" />
45 ) : (
46 <DocumentTextIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" />
47 )}
48
49 <div className="flex min-w-0 flex-1 gap-2 text-xs">
50 <div className="max-w-[50%] flex-shrink-0 truncate font-medium">
51 {rule.name || "Agent rule"}
52 </div>
53
54 <div className="min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-500">
55 {isGlobal
56 ? "Always applied"
57 : `Pattern: ${typeof rule.globs === "string" ? rule.globs : Array.isArray(rule.globs) ? rule.globs.join(", ") : ""}`}
58 </div>
59 </div>
60 </div>
61 <div
62 className={`mt-1 whitespace-pre-line pl-6 pr-2 text-xs italic text-gray-300`}
63 title={
64 isRuleLong ? (expanded ? "Click to collapse" : "Click to expand") : ""
65 }
66 >
67 {displayedRule}
68 {isRuleLong && (
69 <span className="text-description-muted ml-1 opacity-0 transition-opacity group-hover:opacity-100">
70 {expanded ? "(collapse)" : "(expand)"}
71 </span>
72 )}
73 </div>
74 <div className="mt-1 pl-6 pr-2 text-xs text-gray-500">
75 Source: {getRuleSourceDisplayName(rule)}
76 </div>
77 </div>
78 );
79}
80
81export function RulesPeek({ appliedRules, icon }: RulesPeekProps) {
82 const rules = useMemo(() => {
83 return appliedRules ?? [];
84 }, [appliedRules]);
85
86 if (!rules || rules.length === 0) {
87 return null;
88 }
89
90 return (
91 <ToggleDiv
92 icon={icon}
93 title={`${rules.length} rule${rules.length > 1 ? "s" : ""}`}
94 testId="rules-peek"
95 >
96 {rules.map((rule, idx) => (
97 <RulesPeekItem key={`rule-${idx}`} rule={rule} />
98 ))}
99 </ToggleDiv>
100 );
101}