From 00f146cad073db2fd85e1c024a319fc70c05cdf9 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Wed, 20 Nov 2024 22:56:10 +0100 Subject: [PATCH] docs: make the m2m example cli load the access tokens manually --- .../with-m2m/assistant-cli/src/ui/app.tsx | 126 +++++++++++------- 1 file changed, 76 insertions(+), 50 deletions(-) diff --git a/examples/express/with-m2m/assistant-cli/src/ui/app.tsx b/examples/express/with-m2m/assistant-cli/src/ui/app.tsx index b52e3bb2c..e8d786cdc 100644 --- a/examples/express/with-m2m/assistant-cli/src/ui/app.tsx +++ b/examples/express/with-m2m/assistant-cli/src/ui/app.tsx @@ -1,8 +1,8 @@ -import React, {useCallback, useEffect, useState} from 'react'; +import React, {useCallback, useState} from 'react'; import {ListView} from './listScreen.js'; import {AssistantEvent} from '../eventFunctions.js'; import {AssistantNote} from '../noteFunctions.js'; -import {Box, Text} from 'ink'; +import {Box, Text, useInput} from 'ink'; export default function App({ getAccessToken, @@ -39,22 +39,37 @@ export default function App({ const [accessTokenForNoteService, setAccessTokenForNoteService] = useState< string >(); + const [isLoading, setIsLoading] = useState(false); - useEffect(() => { - async function fetchAccessTokens() { - const calendarAccessToken = await getAccessToken( - 'calendar-service', - 'calendar.read calendar.write', - ); - const noteAccessToken = await getAccessToken( - 'note-service', - 'note.read note.write', - ); - setAccessTokenForCalendarService(calendarAccessToken); - setAccessTokenForNoteService(noteAccessToken); - } - fetchAccessTokens().catch(console.error); + const loadAccessTokens = useCallback(async () => { + setIsLoading(true); + const calendarAccessToken = await getAccessToken( + 'calendar-service', + 'calendar.read calendar.write', + ); + const noteAccessToken = await getAccessToken( + 'note-service', + 'note.read note.write', + ); + setAccessTokenForCalendarService(calendarAccessToken); + setAccessTokenForNoteService(noteAccessToken); + setIsLoading(false); }, []); + + useInput( + input => { + if (input === 'r') { + loadAccessTokens().catch(console.error); + } + }, + { + isActive: + !isLoading && + !accessTokenForCalendarService && + !accessTokenForNoteService, + }, + ); + const getEvents = useCallback(() => { if (!accessTokenForCalendarService) return Promise.reject(new Error('No access token')); @@ -125,9 +140,6 @@ export default function App({ [accessTokenForNoteService], ); - if (!accessTokenForNoteService || !accessTokenForCalendarService) - return Loading access token... ; - return ( <> @@ -136,40 +148,54 @@ export default function App({ - - You can navigate using the arrow keys and press enter to select an - item. - - - If you select the Add Item or Edit Item options, you will add a new - item and immediately enter edit mode. - - - You can also delete items by selecting them and pressing the delete - key. - - - While editing, you can navigate between fields using the arrow keys - and press enter to save your changes. - - You can leave editing mode by pressing escape. - - You can exit the application by pressing escape outside of editing - mode. - + {accessTokenForNoteService && accessTokenForCalendarService && ( + <> + + You can navigate using the arrow keys and press enter to select an + item. + + + If you select the Add Item or Edit Item options, you will add a + new item and immediately enter edit mode. + + + You can also delete items by selecting them and pressing the + delete key. + + + While editing, you can navigate between fields using the arrow + keys and press enter to save your changes. + + + You can leave editing mode by pressing escape. + + + You can exit the application by pressing escape outside of editing + mode. + + + )} {/* Your current access token is for the calendar service: {accessTokenForCalendarService} */} {/* Your current access token is for the note service: {accessTokenForNoteService} */} - + {accessTokenForNoteService && accessTokenForCalendarService ? ( + + ) : ( + + {isLoading + ? 'Loading...' + : "Please press 'r' to get your access tokens from the auth service!"} + + )} ); }