Skip to content

Commit

Permalink
fix bug: settings update not properly synced on mobile (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowingjade authored Oct 15, 2024
1 parent 679a307 commit 67f91bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 3 additions & 9 deletions src/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export class ChatView extends ItemView {
super(leaf)
this.settings = plugin.settings
this.initialChatProps = plugin.initialChatProps

this.plugin.addSettingsChangeListener((newSettings) => {
this.onSettingsChange(newSettings)
})
}

getViewType() {
Expand All @@ -55,11 +51,6 @@ export class ChatView extends ItemView {
this.root?.unmount()
}

onSettingsChange(newSettings: SmartCopilotSettings) {
this.settings = newSettings
this.render()
}

render() {
if (!this.root) {
this.root = createRoot(this.containerEl.children[1])
Expand All @@ -72,6 +63,9 @@ export class ChatView extends ItemView {
<SettingsProvider
settings={this.settings}
setSettings={(newSettings) => this.plugin.setSettings(newSettings)}
addSettingsChangeListener={(listener) =>
this.plugin.addSettingsChangeListener(listener)
}
>
<DarkModeProvider>
<LLMProvider>
Expand Down
24 changes: 20 additions & 4 deletions src/contexts/settings-context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react'
import React, { useEffect, useMemo, useState } from 'react'

import { SmartCopilotSettings } from '../types/settings'

Expand All @@ -14,16 +14,32 @@ const SettingsContext = React.createContext<SettingsContextType | undefined>(

export const SettingsProvider = ({
children,
settings,
settings: initialSettings,
setSettings,
addSettingsChangeListener,
}: {
children: React.ReactNode
settings: SmartCopilotSettings
setSettings: (newSettings: SmartCopilotSettings) => void
addSettingsChangeListener: (
listener: (newSettings: SmartCopilotSettings) => void,
) => () => void
}) => {
const [settingsCached, setSettingsCached] = useState(initialSettings)

useEffect(() => {
const removeListener = addSettingsChangeListener((newSettings) => {
setSettingsCached(newSettings)
})

return () => {
removeListener()
}
}, [addSettingsChangeListener, setSettings])

const value = useMemo(
() => ({ settings, setSettings }),
[settings, setSettings],
() => ({ settings: settingsCached, setSettings }),
[settingsCached, setSettings],
)

return (
Expand Down

0 comments on commit 67f91bc

Please sign in to comment.