-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lightspeed): rememeber last opened conversation and display it on…
… load (#159)
- Loading branch information
1 parent
7b21401
commit 9556c86
Showing
4 changed files
with
247 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@red-hat-developer-hub/backstage-plugin-lightspeed': patch | ||
--- | ||
|
||
Persist last conversation state and reload on user revisit |
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
101 changes: 101 additions & 0 deletions
101
...paces/lightspeed/plugins/lightspeed/src/hooks/__tests__/uselastOpenedConversation.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,101 @@ | ||
/* | ||
* Copyright 2025 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import { useLastOpenedConversation } from '../useLastOpenedConversation'; | ||
|
||
describe('useLastOpenedConversation', () => { | ||
const localStorageKey = 'lastOpenedConversation'; | ||
const mockUser = 'user123'; | ||
|
||
beforeEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
it('should not be ready when user is undefined', () => { | ||
const { result } = renderHook(() => useLastOpenedConversation(undefined)); | ||
|
||
expect(result.current.isReady).toBe(false); | ||
expect(result.current.lastOpenedId).toBeNull(); | ||
}); | ||
|
||
it('should load the lastOpenedId from localStorage when user is provided', () => { | ||
const storedData = JSON.stringify({ [mockUser]: 'conv456' }); | ||
localStorage.setItem(localStorageKey, storedData); | ||
|
||
const { result } = renderHook(() => useLastOpenedConversation(mockUser)); | ||
|
||
expect(result.current.isReady).toBe(true); | ||
expect(result.current.lastOpenedId).toBe('conv456'); | ||
}); | ||
|
||
it('should handle missing user data in localStorage', () => { | ||
localStorage.setItem(localStorageKey, JSON.stringify({})); | ||
|
||
const { result } = renderHook(() => useLastOpenedConversation(mockUser)); | ||
|
||
expect(result.current.isReady).toBe(true); | ||
expect(result.current.lastOpenedId).toBeNull(); | ||
}); | ||
|
||
it('should update localStorage when setLastOpenedId is called', () => { | ||
const { result } = renderHook(() => useLastOpenedConversation(mockUser)); | ||
|
||
act(() => { | ||
result.current.setLastOpenedId('conv789'); | ||
}); | ||
|
||
const storedData = JSON.parse( | ||
localStorage.getItem(localStorageKey) || '{}', | ||
); | ||
expect(storedData[mockUser]).toBe('conv789'); | ||
expect(result.current.lastOpenedId).toBe('conv789'); | ||
}); | ||
|
||
it('should clear lastOpenedId when clearLastOpenedId is called', () => { | ||
localStorage.setItem( | ||
localStorageKey, | ||
JSON.stringify({ [mockUser]: 'conv456' }), | ||
); | ||
|
||
const { result } = renderHook(() => useLastOpenedConversation(mockUser)); | ||
|
||
act(() => { | ||
result.current.clearLastOpenedId(); | ||
}); | ||
|
||
const storedData = JSON.parse( | ||
localStorage.getItem(localStorageKey) || '{}', | ||
); | ||
expect(storedData[mockUser]).toBeUndefined(); | ||
expect(result.current.lastOpenedId).toBeNull(); | ||
}); | ||
|
||
it('should not update localStorage if user is undefined', () => { | ||
const { result } = renderHook(() => useLastOpenedConversation(undefined)); | ||
|
||
act(() => { | ||
result.current.setLastOpenedId('conv123'); | ||
}); | ||
|
||
expect(localStorage.getItem(localStorageKey)).toBeNull(); | ||
}); | ||
|
||
it('should not clear localStorage if user is undefined', () => { | ||
const storedData = JSON.stringify({ [mockUser]: 'conv456' }); | ||
localStorage.setItem(localStorageKey, storedData); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
workspaces/lightspeed/plugins/lightspeed/src/hooks/useLastOpenedConversation.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,90 @@ | ||
/* | ||
* Copyright 2025 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
Dispatch, | ||
SetStateAction, | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
type UseLastOpenedConversationReturn = { | ||
lastOpenedId: string | null; | ||
setLastOpenedId: Dispatch<SetStateAction<string | null>>; | ||
clearLastOpenedId: () => void; | ||
isReady: boolean; | ||
}; | ||
|
||
export const useLastOpenedConversation = ( | ||
user: string | undefined, | ||
key = 'lastOpenedConversation', | ||
): UseLastOpenedConversationReturn => { | ||
const [lastOpenedId, setLastOpenedId] = useState<string | null>(null); | ||
const [isReady, setIsReady] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!user) { | ||
setLastOpenedId(null); | ||
setIsReady(false); | ||
return; | ||
} | ||
|
||
try { | ||
const storedData = localStorage.getItem(key); | ||
const parsedData = storedData ? JSON.parse(storedData) : {}; | ||
setLastOpenedId(parsedData[user] || null); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error accessing localStorage:', error); | ||
} finally { | ||
setIsReady(true); | ||
} | ||
}, [user, key]); | ||
|
||
useEffect(() => { | ||
if (!user || lastOpenedId === null) return; | ||
// Update localStorage whenever the last opened ID changes | ||
try { | ||
const storedData = localStorage.getItem(key); | ||
const parsedData = storedData ? JSON.parse(storedData) : {}; | ||
|
||
if (parsedData[user] !== lastOpenedId) { | ||
parsedData[user] = lastOpenedId; | ||
localStorage.setItem(key, JSON.stringify(parsedData)); | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error updating localStorage:', error); | ||
} | ||
}, [lastOpenedId, user, key]); | ||
|
||
const clearLastOpenedId = useCallback(() => { | ||
if (!user) return; | ||
|
||
try { | ||
const storedData = localStorage.getItem(key); | ||
const parsedData = storedData ? JSON.parse(storedData) : {}; | ||
delete parsedData[user]; | ||
localStorage.setItem(key, JSON.stringify(parsedData)); | ||
setLastOpenedId(null); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error clearing localStorage:', error); | ||
} | ||
}, [user, key]); | ||
|
||
return { lastOpenedId, setLastOpenedId, clearLastOpenedId, isReady }; | ||
}; |