forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatPermissionSelector.tsx
More file actions
130 lines (122 loc) · 4.07 KB
/
ChatPermissionSelector.tsx
File metadata and controls
130 lines (122 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use client';
import { useState } from 'react';
import { useTranslation } from '@/hooks/useTranslation';
import {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuItem,
} from '@/components/ui/dropdown-menu';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Button } from '@/components/ui/button';
import { Lock, LockOpen, CaretDown } from '@/components/ui/icon';
interface ChatPermissionSelectorProps {
sessionId?: string;
permissionProfile: 'default' | 'full_access';
onPermissionChange: (profile: 'default' | 'full_access') => void;
}
export function ChatPermissionSelector({
sessionId,
permissionProfile,
onPermissionChange,
}: ChatPermissionSelectorProps) {
const { t } = useTranslation();
const [showWarning, setShowWarning] = useState(false);
const handleSelect = (profile: 'default' | 'full_access') => {
if (profile === 'full_access' && permissionProfile !== 'full_access') {
setShowWarning(true);
return;
}
applyChange(profile);
};
const applyChange = async (profile: 'default' | 'full_access') => {
// No sessionId yet (new chat) — local-only update
if (!sessionId) {
onPermissionChange(profile);
return;
}
try {
const res = await fetch(`/api/chat/sessions/${sessionId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ permission_profile: profile }),
});
if (!res.ok) {
console.warn(`[ChatPermissionSelector] PATCH failed: ${res.status}`);
return;
}
onPermissionChange(profile);
} catch (err) {
console.warn('[ChatPermissionSelector] PATCH error:', err);
}
};
const isFullAccess = permissionProfile === 'full_access';
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="sm"
className={`gap-1 px-2 py-1 text-xs font-medium ${
isFullAccess
? 'bg-status-error-muted text-status-error-foreground hover:bg-status-error-muted'
: 'bg-muted text-muted-foreground hover:bg-muted/80'
}`}
>
{isFullAccess ? (
<LockOpen size={14} className="text-status-error-foreground" />
) : (
<Lock size={14} />
)}
<span>
{isFullAccess ? t('permission.fullAccess') : t('permission.default')}
</span>
<CaretDown size={10} className="opacity-60" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="min-w-[140px]">
<DropdownMenuItem onClick={() => handleSelect('default')}>
<Lock size={14} />
<span>{t('permission.default')}</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleSelect('full_access')}>
<LockOpen size={14} className="text-status-error-foreground" />
<span>{t('permission.fullAccess')}</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<AlertDialog open={showWarning} onOpenChange={setShowWarning}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('permission.fullAccess')}</AlertDialogTitle>
<AlertDialogDescription>
{t('permission.fullAccessWarning')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t('common.cancel')}</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
onClick={() => {
setShowWarning(false);
applyChange('full_access');
}}
>
{t('permission.fullAccess')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
}