Skip to content

Commit 3078d74

Browse files
committed
fix tests
1 parent 8241cc2 commit 3078d74

File tree

13 files changed

+299
-264
lines changed

13 files changed

+299
-264
lines changed

packages/jsrepl/src/app/repl/[[...slug]]/components/code-editor.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export default function CodeEditor() {
6969
tabSize: 2,
7070
renderLineHighlight: userState.editor.renderLineHighlight,
7171
lineNumbers: userState.editor.lineNumbers,
72-
scrollBeyondLastLine: false,
7372
})
7473

7574
useEffect(() => {

packages/jsrepl/src/app/repl/[[...slug]]/components/errors-notification.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function ErrorsNotification() {
4040
</Button>
4141

4242
{expanded && (
43-
<span className="flex max-w-prose flex-1 select-text items-center gap-2 whitespace-normal p-2 font-normal">
43+
<span className="flex max-w-prose flex-1 select-text items-center gap-2 whitespace-normal p-2 pr-3 font-normal">
4444
{primaryError.location?.file && (
4545
<span className="text-muted-foreground">
4646
{primaryError.location.file}:{primaryError.location.line}:

packages/jsrepl/src/components/providers/repl-save-provider.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ import { useSupabaseClient } from '@/hooks/useSupabaseClient'
1313
import { useUser } from '@/hooks/useUser'
1414
import { useUserStoredState } from '@/hooks/useUserStoredState'
1515
import { useWritableModels } from '@/hooks/useWritableModels'
16-
import {
17-
checkDirty,
18-
checkEffectivelyDirty,
19-
fork,
20-
getPageUrl,
21-
save,
22-
} from '@/lib/repl-stored-state/adapter-supabase'
16+
import { fork, getPageUrl, save } from '@/lib/repl-stored-state/adapter-supabase'
17+
import { checkDirty, checkEffectivelyDirty } from '@/lib/repl-stored-state/adapter-supabase/utils'
2318
import { ResponseError, isAbortError } from '@/lib/response-error'
2419
import type { ReplStoredState } from '@/types/repl.types'
2520

packages/jsrepl/src/hooks/useMonacopilot.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useReplStoredState } from './useReplStoredState'
1616
type RequestHandler = Exclude<RegisterCompletionOptions['requestHandler'], undefined>
1717

1818
export default function useMonacopilot() {
19-
const { editorRef } = useMonacoEditor()
19+
const { editor } = useMonacoEditor()
2020
const { models } = useReplModels()
2121
const [replState] = useReplStoredState()
2222
const recentModelsRef = useRef<CodeEditorModel[]>([])
@@ -107,15 +107,11 @@ export default function useMonacopilot() {
107107
}, [models, replState.activeModel, enableRelatedFiles])
108108

109109
useEffect(() => {
110-
if (!editorRef.current) {
110+
if (!editor || !isEnabled) {
111111
return
112112
}
113113

114-
if (!isEnabled) {
115-
return
116-
}
117-
118-
const completion = registerCompletion(monaco, editorRef.current, {
114+
const completion = registerCompletion(monaco, editor, {
119115
endpoint: '',
120116
language: [
121117
{ language: 'typescript', exclusive: true },
@@ -144,7 +140,7 @@ export default function useMonacopilot() {
144140
return () => {
145141
completion.deregister()
146142
}
147-
}, [editorRef, isEnabled, requestHandler, enableCaching, maxContextLines, enableRelatedFiles])
143+
}, [editor, isEnabled, requestHandler, enableCaching, maxContextLines, enableRelatedFiles])
148144
}
149145

150146
function customPrompt(metadata: CompletionMetadata) {

packages/jsrepl/src/hooks/useReplDecorations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function useReplDecorations() {
1414
const [replState] = useReplStoredState()
1515
const [rewindMode] = useReplRewindMode()
1616
const { payloads } = useReplPayloads()
17-
const { editorRef } = useMonacoEditor()
17+
const { editor, editorRef } = useMonacoEditor()
1818
const { setDecorationsOutdated } = useReplOutdatedDecorations()
1919

2020
const decorationsDisposable = useRef<() => void>(undefined)
@@ -136,11 +136,11 @@ export default function useReplDecorations() {
136136
}, [])
137137

138138
useEffect(() => {
139-
if (!editorRef.current) {
139+
if (!editor) {
140140
return
141141
}
142142

143-
const disposable = editorRef.current.addAction({
143+
const disposable = editor.addAction({
144144
id: 'jsrepl.copyContentsWithDecors',
145145
label: 'Copy With REPL Decorations',
146146
async run(editor) {
@@ -154,5 +154,5 @@ export default function useReplDecorations() {
154154
return () => {
155155
disposable.dispose()
156156
}
157-
}, [editorRef])
157+
}, [editor])
158158
}

packages/jsrepl/src/hooks/useReplOutdatedDecorations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useMonacoEditor } from './useMonacoEditor'
44
import { useReplModels } from './useReplModels'
55

66
export default function useReplDecorationsOutdated() {
7-
const { editorRef } = useMonacoEditor()
7+
const { editor, editorRef } = useMonacoEditor()
88
const { models } = useReplModels()
99

1010
const decorationsOutdatedRef = useRef(false)
@@ -35,7 +35,6 @@ export default function useReplDecorationsOutdated() {
3535
)
3636

3737
useEffect(() => {
38-
const editor = editorRef.current
3938
if (!editor) {
4039
return
4140
}
@@ -54,7 +53,7 @@ export default function useReplDecorationsOutdated() {
5453
return () => {
5554
disposable.dispose()
5655
}
57-
}, [models, editorRef, setDecorationsOutdated])
56+
}, [models, editor, setDecorationsOutdated])
5857

5958
return { setDecorationsOutdated }
6059
}

packages/jsrepl/src/lib/repl-stored-state/adapter-supabase/api.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SupabaseClient } from '@supabase/supabase-js'
22
import { Database, ReplStoredState } from '@/types'
3+
import { toCreatePayload, toUpdatePayload } from './utils'
34

45
/**
56
* Get a repl by id
@@ -80,14 +81,7 @@ export async function createRepl(
8081
repl: ReplStoredState,
8182
{ supabase, signal }: { supabase: SupabaseClient<Database>; signal?: AbortSignal }
8283
) {
83-
const payload = {
84-
title: repl.title,
85-
description: repl.description,
86-
fs: repl.fs,
87-
opened_models: repl.openedModels,
88-
active_model: repl.activeModel,
89-
show_preview: repl.showPreview,
90-
}
84+
const payload = toCreatePayload(repl)
9185

9286
return await supabase
9387
.from('repls')
@@ -118,15 +112,7 @@ export async function updateRepl(
118112
throw new Error('Repl ID is required')
119113
}
120114

121-
const payload = {
122-
id: repl.id,
123-
title: repl.title,
124-
description: repl.description,
125-
fs: repl.fs,
126-
opened_models: repl.openedModels,
127-
active_model: repl.activeModel,
128-
show_preview: repl.showPreview,
129-
}
115+
const payload = toUpdatePayload(repl)
130116

131117
return await supabase
132118
.from('repls')

packages/jsrepl/src/lib/repl-stored-state/adapter-supabase/index.ts

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { SupabaseClient } from '@supabase/supabase-js'
2-
import { deepEqual } from '@/lib/equal'
32
import * as ReplFS from '@/lib/repl-fs'
43
import { ResponseError } from '@/lib/response-error'
5-
import type { Database, ReplRecordPayload, ReplStoredState, ReplUpdatePayload } from '@/types'
4+
import type { Database, ReplStoredState } from '@/types'
65
import {
76
createRepl,
87
deleteRepl,
@@ -13,6 +12,7 @@ import {
1312
getUserRepls,
1413
updateRepl,
1514
} from './api'
15+
import { fromPayload } from './utils'
1616

1717
type ExtraUrlProps = {
1818
openedModels?: string[]
@@ -236,60 +236,6 @@ export function getPageUrl(state: ReplStoredState, extraUrlProps?: ExtraUrlProps
236236
return url
237237
}
238238

239-
export function checkDirty(state: ReplStoredState, savedState: ReplStoredState) {
240-
const a = toPayload(state)
241-
const b = toPayload(savedState)
242-
return !deepEqual(a, b)
243-
}
244-
245-
export function checkEffectivelyDirty(state: ReplStoredState, savedState: ReplStoredState) {
246-
const a = toPayload(state)
247-
const b = toPayload(savedState)
248-
249-
delete a.opened_models
250-
delete a.active_model
251-
delete a.show_preview
252-
253-
delete b.opened_models
254-
delete b.active_model
255-
delete b.show_preview
256-
257-
return !deepEqual(a, b)
258-
}
259-
260-
function fromPayload(payload: ReplRecordPayload): ReplStoredState {
261-
return {
262-
id: payload.id,
263-
user_id: payload.user_id,
264-
user: payload.user
265-
? {
266-
avatar_url: payload.user.avatar_url,
267-
user_name: payload.user.user_name,
268-
}
269-
: null,
270-
created_at: payload.created_at,
271-
updated_at: payload.updated_at,
272-
title: payload.title,
273-
description: payload.description,
274-
fs: payload.fs,
275-
openedModels: payload.opened_models,
276-
activeModel: payload.active_model,
277-
showPreview: payload.show_preview,
278-
}
279-
}
280-
281-
function toPayload(state: ReplStoredState): ReplUpdatePayload {
282-
return {
283-
id: state.id,
284-
title: state.title,
285-
description: state.description,
286-
fs: state.fs,
287-
opened_models: state.openedModels,
288-
active_model: state.activeModel,
289-
show_preview: state.showPreview,
290-
}
291-
}
292-
293239
function searchParamsToExtraUrlProps(searchParams: URLSearchParams): ExtraUrlProps {
294240
let openedModels: string[] | undefined
295241
let activeModel: string | undefined
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { deepEqual } from '@/lib/equal'
2+
import {
3+
ReplCreatePayload,
4+
ReplRecordPayloadWithUser,
5+
ReplStoredState,
6+
ReplUpdatePayload,
7+
} from '@/types'
8+
9+
export function fromPayload(payload: ReplRecordPayloadWithUser): ReplStoredState {
10+
return {
11+
id: payload.id,
12+
user_id: payload.user_id,
13+
user: payload.user
14+
? {
15+
avatar_url: payload.user.avatar_url,
16+
user_name: payload.user.user_name,
17+
}
18+
: null,
19+
created_at: payload.created_at,
20+
updated_at: payload.updated_at,
21+
title: payload.title,
22+
description: payload.description,
23+
fs: payload.fs,
24+
openedModels: payload.opened_models,
25+
activeModel: payload.active_model,
26+
showPreview: payload.show_preview,
27+
}
28+
}
29+
30+
export function toUpdatePayload(state: ReplStoredState): ReplUpdatePayload {
31+
return {
32+
id: state.id,
33+
title: state.title,
34+
description: state.description,
35+
fs: state.fs,
36+
opened_models: state.openedModels,
37+
active_model: state.activeModel,
38+
show_preview: state.showPreview,
39+
}
40+
}
41+
42+
export function toCreatePayload(state: ReplStoredState): ReplCreatePayload {
43+
return {
44+
title: state.title,
45+
description: state.description,
46+
fs: state.fs,
47+
opened_models: state.openedModels,
48+
active_model: state.activeModel,
49+
show_preview: state.showPreview,
50+
}
51+
}
52+
53+
export function checkDirty(state: ReplStoredState, savedState: ReplStoredState) {
54+
const a = toUpdatePayload(state)
55+
const b = toUpdatePayload(savedState)
56+
return !deepEqual(a, b)
57+
}
58+
59+
export function checkEffectivelyDirty(state: ReplStoredState, savedState: ReplStoredState) {
60+
const a = toUpdatePayload(state)
61+
const b = toUpdatePayload(savedState)
62+
63+
delete a.opened_models
64+
delete a.active_model
65+
delete a.show_preview
66+
67+
delete b.opened_models
68+
delete b.active_model
69+
delete b.show_preview
70+
71+
return !deepEqual(a, b)
72+
}

packages/jsrepl/src/types/repl.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ export type ReplStoredState = {
2828
showPreview: boolean
2929
}
3030

31-
export type ReplRecordPayload = Database['public']['Tables']['repls']['Row'] & {
31+
export type ReplRecordPayload = Database['public']['Tables']['repls']['Row']
32+
33+
export type ReplRecordPayloadWithUser = Database['public']['Tables']['repls']['Row'] & {
3234
user: Database['public']['Tables']['public_profiles']['Row'] | null
3335
}
3436

3537
export type ReplUpdatePayload = Database['public']['Tables']['repls']['Update']
3638

39+
export type ReplCreatePayload = Omit<Database['public']['Tables']['repls']['Insert'], 'id'>
40+
3741
export type UserStoredState = {
3842
/**
3943
* Last used App version. It is used to show New Version toast.

0 commit comments

Comments
 (0)