11import * as changeCase from "change-case" ;
2- import { LocalStorage , getPreferenceValues } from "@raycast/api" ;
2+ import { LocalStorage , getPreferenceValues , showToast , Toast } from "@raycast/api" ;
33import fetch from "node-fetch" ;
44
5- import { CASES } from "./constants" ;
5+ import { CASES , CODE_VAR_HISTORY } from "./constants" ;
66import type { Result , ChatCompletion } from "./types" ;
77
8- // todo:Return results directly from query history
9- export async function getSearchHistory ( ) : Promise < Result [ ] > {
10- const historyString = ( await LocalStorage . getItem ( "history" ) ) as string ;
8+ export async function getHistory ( queryText : string ) : Promise < Result [ ] > {
9+ const historyString = ( await LocalStorage . getItem ( `${ CODE_VAR_HISTORY } _${ queryText } ` ) ) as string ;
1110 if ( historyString === undefined ) return [ ] ;
1211 const items : Result [ ] = JSON . parse ( historyString ) ;
1312 return items ;
1413}
1514
16- export async function queryVariableNames ( content : string , signal ?: AbortSignal ) : Promise < Result [ ] > {
15+ export async function deleteAllHistory ( ) {
16+ await LocalStorage . clear ( ) ;
17+ showToast ( {
18+ style : Toast . Style . Success ,
19+ title : "Success" ,
20+ message : "Cleared search history" ,
21+ } ) ;
22+ }
23+
24+ export async function deleteHistoryItem ( result : Result ) {
25+ await LocalStorage . removeItem ( `${ CODE_VAR_HISTORY } _${ result . query } ` ) ;
26+ showToast ( {
27+ style : Toast . Style . Success ,
28+ title : "Success" ,
29+ message : "Removed from history" ,
30+ } ) ;
31+ }
32+
33+ export async function queryVariableNames ( queryText : string , signal ?: AbortSignal ) : Promise < Result [ ] > {
1734 const { entrypoint, apiKey } = getPreferenceValues < { entrypoint : string ; apiKey : string } > ( ) ;
1835
1936 const response = await fetch ( entrypoint , {
@@ -28,7 +45,7 @@ export async function queryVariableNames(content: string, signal?: AbortSignal):
2845 messages : [
2946 {
3047 role : "user" ,
31- content : `Translate to en: \n\n ${ content } ` ,
48+ content : `Translate to en: \n\n ${ queryText } ` ,
3249 } ,
3350 ] ,
3451 } ) ,
@@ -41,15 +58,29 @@ export async function queryVariableNames(content: string, signal?: AbortSignal):
4158 let result : Result [ ] = [ ] ;
4259 try {
4360 const text = ( content ?. choices ?. [ 0 ] ?. message ?. content || "" ) . replace ( / \n / g, "" ) . replace ( / \. / g, "" ) . trim ( ) ;
44- result = CASES . map ( ( type ) => {
45- if ( type === "capitalCase" ) {
46- return { value : changeCase [ type ] ( text . replace ( / / g, "" ) ) , type } ;
61+ result = CASES . map ( ( caseType ) => {
62+ if ( caseType === "capitalCase" ) {
63+ return {
64+ value : changeCase [ caseType ] ( text . replace ( / / g, "" ) ) ,
65+ type : caseType ,
66+ } ;
4767 }
48- return { value : changeCase [ type ] ( text ) , type } ;
68+ return {
69+ value : changeCase [ caseType ] ( text ) ,
70+ type : caseType ,
71+ } ;
4972 } ) ;
5073 } catch ( error ) {
5174 result = [ ] ;
5275 }
76+
77+ if ( queryText && result . length > 0 ) {
78+ await LocalStorage . setItem (
79+ `${ CODE_VAR_HISTORY } _${ queryText } ` ,
80+ JSON . stringify ( result . map ( ( item ) => ( { ...item , query : queryText } ) ) )
81+ ) ;
82+ }
83+
5384 return result ;
5485 }
5586}
0 commit comments