1
- import { WritableAtom , atom } from 'jotai' ;
1
+ import { atom } from 'jotai' ;
2
2
import { atomFamily , atomWithStorage } from 'jotai/utils' ;
3
3
import { Octokit } from 'octokit' ;
4
4
import { fetchFileContent } from '../components/Editor/editorUtils' ;
@@ -8,7 +8,7 @@ import { formatProblems } from '../utils/prettierFormatter';
8
8
export type EditorFile = {
9
9
path : string ;
10
10
markdown : string ;
11
- problems : string ;
11
+ problems ? : string ;
12
12
} ;
13
13
14
14
export const filesFamily = atomFamily ( ( path : string ) => {
@@ -47,16 +47,13 @@ const baseActiveFileAtom = atomWithStorage(
47
47
'guide:editor:activeFile' ,
48
48
null as string | null
49
49
) ;
50
- // Writable type needed if strictNullChecks is false
51
- // TODO: remove this when strictNullChecks is true
52
- export type Writable < T > = WritableAtom < T , any [ ] , unknown > ;
53
50
export const branchAtom = atomWithStorage ( 'guide:editor:branch' , null ) ;
54
- export const tokenAtom = atom ( null ) as Writable < string | null > ;
51
+ export const tokenAtom = atom ( null ) ;
55
52
export const octokitAtom = atom ( get =>
56
53
get ( tokenAtom ) === null ? null : new Octokit ( { auth : get ( tokenAtom ) } )
57
54
) ;
58
- export const forkAtom = atom ( undefined ) as Writable < string | undefined | null > ;
59
- export const baseTabAtom = atom ( 'content' ) as Writable < 'content' | 'problems' > ;
55
+ export const forkAtom = atom ( undefined ) ;
56
+ export const baseTabAtom = atom ( 'content' ) ;
60
57
export const editingSolutionAtom = atom ( get => {
61
58
const activeFile = get ( activeFileAtom ) ;
62
59
return activeFile && activeFile . path . startsWith ( 'solutions' ) ;
@@ -84,10 +81,10 @@ export const githubInfoAtom = atom(
84
81
async get => ( await get ( octokitAtom ) ?. request ( 'GET /user' ) ) ?. data
85
82
) ;
86
83
export const activeFileAtom = atom (
87
- get =>
88
- get ( baseActiveFileAtom ) === null
89
- ? null
90
- : get ( filesFamily ( get ( baseActiveFileAtom ) ) ) ,
84
+ get => {
85
+ const activeFile = get ( baseActiveFileAtom ) ;
86
+ return activeFile ? get ( filesFamily ( activeFile ) ) : null ;
87
+ } ,
91
88
( get , set , nextActiveFilePath ) => {
92
89
set ( baseActiveFileAtom , nextActiveFilePath ) ;
93
90
}
@@ -100,7 +97,7 @@ export const filesListAtom = atomWithStorage<string[]>(
100
97
101
98
export const openOrCreateExistingFileAtom = atom (
102
99
null ,
103
- async ( get , set , filePath : string | null ) => {
100
+ async ( get , set , filePath : string ) => {
104
101
if ( get ( filesListAtom ) . find ( f => f === filePath ) ) {
105
102
set ( activeFileAtom , filePath ) ;
106
103
} else {
@@ -201,7 +198,8 @@ $\\texttt{func(var)}$
201
198
problems : '' ,
202
199
} ;
203
200
204
- const updateProblemJSON = ( json : string ) => {
201
+ const updateProblemJSON = ( json : string | undefined ) => {
202
+ if ( ! json ) return undefined ;
205
203
const updated = JSON . parse ( json ) ;
206
204
Object . keys ( updated ) . forEach ( key => {
207
205
if ( key === 'MODULE_ID' ) return ;
@@ -252,17 +250,17 @@ export const closeFileAtom = atom(null, (get, set, filePath: string) => {
252
250
filesListAtom ,
253
251
get ( filesListAtom ) . filter ( file => file !== filePath )
254
252
) ;
255
- if ( get ( activeFileAtom ) . path === filePath ) {
253
+ if ( get ( activeFileAtom ) ? .path === filePath ) {
256
254
const remainingFiles = get ( filesListAtom ) ;
257
255
set ( activeFileAtom , remainingFiles . length > 0 ? remainingFiles [ 0 ] : null ) ;
258
256
}
259
- set ( filesFamily ( filePath ) , null ) ;
257
+ filesFamily . remove ( filePath ) ;
260
258
} ) ;
261
259
262
260
const baseMonacoEditorInstanceAtom = atom ( { monaco : null } ) ;
263
261
export const monacoEditorInstanceAtom = atom (
264
262
get => get ( baseMonacoEditorInstanceAtom ) ,
265
- ( get , _set , val ) => {
263
+ ( get , _set , val : any ) => {
266
264
get ( baseMonacoEditorInstanceAtom ) . monaco = val ;
267
265
}
268
266
) ;
0 commit comments