-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(opentrons-ai-client): refactoring api call and inputprompt c…
…omponent refactoring api call and inputprompt component close AUTH-402
- Loading branch information
Showing
7 changed files
with
155 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const END_POINT = | ||
'https://fk0py9eu3e.execute-api.us-east-2.amazonaws.com/sandbox/chat/completion' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react' | ||
import axios from 'axios' | ||
|
||
import type { AxiosRequestConfig } from 'axios' | ||
|
||
interface UseApiCallResult<T> { | ||
data: T | null | ||
error: string | null | ||
isLoading: boolean | ||
fetchData: (config: AxiosRequestConfig) => Promise<void> | ||
} | ||
|
||
export const useApiCall = <T>(): UseApiCallResult<T> => { | ||
const [data, setData] = useState<T | null>(null) | ||
const [error, setError] = useState<string | null>(null) | ||
const [isLoading, setIsLoading] = useState<boolean>(false) | ||
|
||
const fetchData = async (config: AxiosRequestConfig): Promise<void> => { | ||
console.log('useApiCall - fetchData') | ||
setIsLoading(true) | ||
setError(null) | ||
|
||
try { | ||
const response = await axios.request<T>({ | ||
...config, | ||
withCredentials: true, | ||
}) | ||
console.log(response) | ||
setData(response.data) | ||
} catch (err: any) { | ||
// ToDo (kk:05/15/2024) remove any | ||
console.log(err) | ||
setError(err.message as string) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
return { data, error, isLoading, fetchData } | ||
} |
17 changes: 17 additions & 0 deletions
17
opentrons-ai-client/src/resources/hooks/useGetAuth0Token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useAuth0 } from '@auth0/auth0-react' | ||
|
||
const audience = 'sandbox-ai-api' | ||
export const useGetAuth0Token = async (): Promise<string | null> => { | ||
const { getAccessTokenSilently } = useAuth0() | ||
try { | ||
const accessToken = await getAccessTokenSilently({ | ||
authorizationParams: { | ||
audience, | ||
}, | ||
}) | ||
return accessToken | ||
} catch (err) { | ||
console.error(`cannot get token: ${err}`) | ||
} | ||
return null | ||
} |
11 changes: 11 additions & 0 deletions
11
opentrons-ai-client/src/resources/utils/__tests__/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { calcTextAreaHeight } from '../utils' | ||
|
||
describe('calcTextAreaHeight', () => { | ||
it('should return the correct number of lines', () => { | ||
const input = 'Hello\nWorld\nThis is testing data.' | ||
const expectedOutput = 3 | ||
const result = calcTextAreaHeight(input) | ||
expect(result).toEqual(expectedOutput) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Calculates the number of lines in a given string. | ||
* @param input - The string to calculate the number of lines for. | ||
* @returns The number of lines in the input string. | ||
*/ | ||
|
||
export const calcTextAreaHeight = (input: string): number => { | ||
const rowsNum = input.split('\n').length | ||
return rowsNum | ||
} |