/
AISettings.razor
91 lines (83 loc) · 2.95 KB
/
AISettings.razor
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
@inject AppData AppData
<Row>
<Column>
<Field>
<FieldLabel>OpenAI API Key</FieldLabel>
<FieldBody>
<Addons>
<Addon AddonType="AddonType.Start">
<Button Color="Color.Secondary" Clicked="@ShowAPIKey" Disabled="@(string.IsNullOrEmpty(OpenAIApiKey))" Block>
@if (textRole == TextRole.Password)
{
<Icon Name="IconName.Eye" />
}
else
{
<Icon Name="IconName.EyeSlash" />
}
</Button>
</Addon>
<Addon AddonType="AddonType.Body">
<TextEdit Placeholder="Enter OpenAI API Key" Role="@textRole" @bind-Text="OpenAIApiKey" />
</Addon>
@if (!string.IsNullOrEmpty(OpenAIApiKey))
{
<Addon AddonType="AddonType.End">
<Button Color="Color.Secondary" Clicked="@SaveAPIKey" Loading="@Loading" Block>Save</Button>
</Addon>
}
</Addons>
</FieldBody>
</Field>
</Column>
</Row>
@code {
[Inject] INotificationService? NotificationService { get; set; }
public string OpenAIApiKey { get; set; } = string.Empty;
public bool Loading {get;set;} = false;
TextRole textRole = TextRole.Password;
private System.Threading.Timer timer;
protected override async Task OnInitializedAsync()
{
var res = await AppData.GetAndDecryptOpenAIApiKey();
if (!string.IsNullOrEmpty(res))
OpenAIApiKey = res;
}
private async Task ShowAPIKey()
{
if (textRole == TextRole.Text)
{
textRole = TextRole.Password;
timer.Dispose();
await InvokeAsync(StateHasChanged);
}
else
{
textRole = TextRole.Text;
await InvokeAsync(StateHasChanged);
timer = new System.Threading.Timer(async (object stateInfo) =>
{
textRole = TextRole.Password;
await InvokeAsync(StateHasChanged);
timer.Dispose();
}, new System.Threading.AutoResetEvent(false), 5000, 5000);
}
}
private async Task SaveAPIKey()
{
Loading = true;
await InvokeAsync(StateHasChanged);
if (await AppData.InitAssistant(OpenAIApiKey))
{
if (NotificationService != null)
await NotificationService.Success("Api Key Saved.", "Saved");
}
else
{
if (NotificationService != null)
await NotificationService.Error("Cannot save Api Key.", "Error");
}
Loading = false;
await InvokeAsync(StateHasChanged);
}
}