Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache data locally #901

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions raven-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dayjs": "^1.11.10",
"downshift": "^8.3.1",
"emoji-picker-element": "^1.21.0",
"frappe-react-sdk": "^1.6.0",
"frappe-react-sdk": "^1.7.0",
"highlight.js": "^11.9.0",
"html-react-parser": "^5.1.8",
"jotai": "^2.8.0",
Expand Down Expand Up @@ -62,4 +62,4 @@
"@types/turndown": "^5.0.4",
"typescript": "^5.3.3"
}
}
}
17 changes: 17 additions & 0 deletions raven-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ function App() {
url={import.meta.env.VITE_FRAPPE_PATH ?? ''}
socketPort={import.meta.env.VITE_SOCKET_PORT ? import.meta.env.VITE_SOCKET_PORT : undefined}
//@ts-ignore
swrConfig={{
provider: localStorageProvider
}}
siteName={getSiteName()}
>
<UserProvider>
Expand All @@ -84,4 +87,18 @@ function App() {
)
}

function localStorageProvider() {
// When initializing, we restore the data from `localStorage` into a map.
const map = new Map<string, any>(JSON.parse(localStorage.getItem('app-cache') || '[]'))

// Before unloading the app, we write back all the data into `localStorage`.
window.addEventListener('beforeunload', () => {
const appCache = JSON.stringify(Array.from(map.entries()))
localStorage.setItem('app-cache', appCache)
})

// We still use the map for write & read for performance.
return map
}

export default App
30 changes: 15 additions & 15 deletions raven-app/src/components/feature/chat/ChatStream/useChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,25 @@ const useChatStream = (scrollRef: MutableRefObject<HTMLDivElement | null>) => {
* Need to scroll down twice because the scrollHeight is not updated immediately after the first scroll
*/
useLayoutEffect(() => {
if (done) {
// if (done) {

setTimeout(() => {
scrollRef.current?.scroll({
top: scrollRef.current?.scrollHeight,
// behavior: 'smooth',
})
}, 50)
setTimeout(() => {
scrollRef.current?.scroll({
top: scrollRef.current?.scrollHeight,
// behavior: 'smooth',
})
}, 50)

setTimeout(() => {
scrollRef.current?.scroll({
top: scrollRef.current?.scrollHeight,
// behavior: 'smooth',
})
}, 200)
setTimeout(() => {
scrollRef.current?.scroll({
top: scrollRef.current?.scrollHeight,
// behavior: 'smooth',
})
}, 200)


scrollRef.current?.scrollTo(0, scrollRef.current?.scrollHeight)
}
scrollRef.current?.scrollTo(0, scrollRef.current?.scrollHeight)
// }
}, [done, channelID])


Expand Down
1 change: 0 additions & 1 deletion raven-app/src/pages/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export const Component = () => {
{...register("password",
{
required: "Password is required.",
minLength: { value: 6, message: "Password should be minimum 6 characters." }
})}
name="password"
type={isPasswordOpen ? "text" : "password"}
Expand Down
15 changes: 13 additions & 2 deletions raven-app/src/utils/auth/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ export const UserProvider: FC<PropsWithChildren> = ({ children }) => {

const handleLogout = async () => {
localStorage.removeItem('ravenLastChannel')
localStorage.removeItem('app-cache')
return logout()
.then(() => {
//Clear cache on logout
return mutate(() => true, undefined, false)
return mutate((key) => {
if (key === 'raven.api.login.get_context') {
return false
}
return true
}, undefined, false)
})
.then(() => {
//Reload the page so that the boot info is fetched again
const URL = import.meta.env.VITE_BASE_NAME ? `${import.meta.env.VITE_BASE_NAME}` : ``
window.location.replace(`/login?redirect-to=${URL}/channel`)
if (URL) {
window.location.replace(`/${URL}/login`)
} else {
window.location.replace('/login')
}

// window.location.reload()
})
}
Expand Down
10 changes: 6 additions & 4 deletions raven-app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3141,10 +3141,10 @@ frappe-js-sdk@^1.5.0:
dependencies:
axios "^1.6.7"

frappe-react-sdk@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/frappe-react-sdk/-/frappe-react-sdk-1.6.0.tgz#2216422b776beaeefc7af55e436f719f1245b9aa"
integrity sha512-i43chxCzWmS6nks/FMli+dHFfPJU6jZK7Oivf4enXH6D6U7RiYI6Aoexwda8cQVTQkR7ShFIboRzZX1oT8GOlA==
frappe-react-sdk@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/frappe-react-sdk/-/frappe-react-sdk-1.7.0.tgz#6d7430f2e606dbe733ffbe1fda7566fc815b6a45"
integrity sha512-UBch8j7nWCkYWQjGG7tOds5tFif/hFdAbg8T+FlOiJAyFmtpbVdBOsWx2ime9aBkNa2U8zqjOkQLLIvL2/qOrg==
dependencies:
frappe-js-sdk "^1.5.0"
socket.io-client "4.7.1"
Expand Down Expand Up @@ -4623,6 +4623,7 @@ sourcemap-codec@^1.4.8:
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
name string-width-cjs
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -4696,6 +4697,7 @@ stringify-object@^3.3.0:
is-regexp "^1.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
name strip-ansi-cjs
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down
Loading
Loading