Skip to content

Commit c503495

Browse files
committedApr 2, 2024
Merge branch 'main' into web-console/auth
2 parents 9d2850c + e19e4f5 commit c503495

File tree

1 file changed

+13
-16
lines changed
  • packages/web-console/src/providers/EditorProvider

1 file changed

+13
-16
lines changed
 

‎packages/web-console/src/providers/EditorProvider/index.tsx

+13-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export type EditorContext = {
2828
monacoRef: MutableRefObject<Monaco | null>
2929
insertTextAtCursor: (text: string) => void
3030
appendQuery: (query: string, options?: AppendQueryOptions) => void
31-
buffers: Buffer[]
3231
activeBuffer: Buffer
3332
setActiveBuffer: (buffer: Buffer) => Promise<void>
3433
addBuffer: (
@@ -46,7 +45,6 @@ const defaultValues = {
4645
monacoRef: { current: null },
4746
insertTextAtCursor: () => undefined,
4847
appendQuery: (query: string, options?: AppendQueryOptions) => undefined,
49-
buffers: [],
5048
activeBuffer: fallbackBuffer,
5149
setActiveBuffer: () => Promise.resolve(),
5250
addBuffer: () => Promise.resolve(fallbackBuffer),
@@ -61,27 +59,27 @@ const EditorContext = createContext<EditorContext>(defaultValues)
6159
export const EditorProvider = ({ children }: PropsWithChildren<{}>) => {
6260
const editorRef = useRef<IStandaloneCodeEditor>(null)
6361
const monacoRef = useRef<Monaco>(null)
64-
const buffers = useLiveQuery(bufferStore.getAll, [])
65-
const activeBufferId = useLiveQuery(
66-
() => bufferStore.getActiveId(),
67-
[],
68-
)?.value
6962

7063
const [activeBuffer, setActiveBufferState] = useState<Buffer>(fallbackBuffer)
7164
const [inFocus, setInFocus] = useState(false)
65+
const [buffersInitialised, setBuffersInitialised] = useState(false)
7266

73-
const ranOnce = useRef(false)
7467
// this effect should run only once, after mount and after `buffers` and `activeBufferId` are ready from the db
7568
useEffect(() => {
76-
if (!ranOnce.current && buffers && activeBufferId) {
77-
const buffer =
78-
buffers?.find((buffer) => buffer.id === activeBufferId) ?? buffers[0]
79-
setActiveBufferState(buffer)
80-
ranOnce.current = true
69+
async function initBuffers() {
70+
const buffers = await bufferStore.getAll()
71+
const activeBufferId = (await bufferStore.getActiveId())?.value
72+
if (buffers && activeBufferId) {
73+
const buffer =
74+
buffers?.find((buffer) => buffer.id === activeBufferId) ?? buffers[0]
75+
setActiveBufferState(buffer)
76+
setBuffersInitialised(true)
77+
}
8178
}
82-
}, [buffers, activeBufferId])
79+
initBuffers()
80+
}, [])
8381

84-
if (!buffers || !activeBufferId || activeBuffer === fallbackBuffer) {
82+
if (!buffersInitialised) {
8583
return null
8684
}
8785

@@ -201,7 +199,6 @@ export const EditorProvider = ({ children }: PropsWithChildren<{}>) => {
201199
appendQuery(editorRef.current, text, options)
202200
}
203201
},
204-
buffers,
205202
activeBuffer,
206203
setActiveBuffer,
207204
addBuffer,

0 commit comments

Comments
 (0)
Failed to load comments.