-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,332 additions
and
2,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,80 @@ | ||
import * as React from 'react'; | ||
import { useContext, useState } from 'react'; | ||
import { AppData } from '../types'; | ||
import { app } from "electron"; | ||
import path from "path"; | ||
import { AppData, WorkSpace } from '../types'; | ||
import { remote } from 'electron'; | ||
import path from 'path'; | ||
import { useAsyncEffect } from '../utils'; | ||
import * as fsLib from 'fs'; | ||
import { LocalFileSystemDataSource } from '../datasource/LocalFileSystemDataSource'; | ||
|
||
const fs = fsLib.promises; | ||
|
||
const userDataFolder = app.getPath('userData'); | ||
const userDataFolder = path.join(remote.app.getPath('appData'), 'yana'); | ||
const appDataFile = path.join(userDataFolder, 'workspaces.json'); | ||
|
||
console.log('AppDataFile located at', appDataFile); | ||
|
||
interface AppDataContextValue extends AppData { | ||
createWorkSpace: (name: string, path: string) => Promise<void>; | ||
createWorkSpace: (name: string, path: string) => Promise<void>; | ||
setWorkSpace: (workspace: WorkSpace) => void; | ||
currentWorkspace: WorkSpace; | ||
} | ||
|
||
export const AppDataContext = React.createContext<AppDataContextValue>(null as any); | ||
|
||
export const useAppData = () => useContext(AppDataContext); | ||
|
||
export const AppDataProvider: React.FC = props => { | ||
const [appData, setAppData] = useState<AppData>({ workspaces: [] }); | ||
const [appData, setAppData] = useState<AppData>({ workspaces: [] }); | ||
const [currentWorkspace, setCurrentWorkspace] = useState<WorkSpace>(appData.workspaces[0]); | ||
|
||
useAsyncEffect(async () => { | ||
if (!fsLib.existsSync(userDataFolder)) { | ||
await fs.mkdir(userDataFolder); | ||
} | ||
useAsyncEffect(async () => { | ||
if (!fsLib.existsSync(userDataFolder)) { | ||
fsLib.mkdirSync(userDataFolder); | ||
} | ||
|
||
let appData: AppData = { | ||
workspaces: [] | ||
}; | ||
let appData: AppData = { | ||
workspaces: [], | ||
}; | ||
|
||
if (!fsLib.existsSync(appDataFile)) { | ||
await fs.writeFile(appDataFile, JSON.stringify(appData)); | ||
} else { | ||
appData = JSON.parse(await fs.readFile(appDataFile, { encoding: 'utf8' })); | ||
} | ||
if (!fsLib.existsSync(appDataFile)) { | ||
fsLib.writeFileSync(appDataFile, JSON.stringify(appData)); | ||
} else { | ||
appData = JSON.parse(fsLib.readFileSync(appDataFile, { encoding: 'utf8' })); | ||
} | ||
|
||
setAppData(appData); | ||
}, []) | ||
setAppData(appData); | ||
setCurrentWorkspace(appData.workspaces[0]); | ||
}, []); | ||
|
||
return ( | ||
<AppDataContext.Provider value={{ | ||
return ( | ||
<AppDataContext.Provider | ||
value={{ | ||
...appData, | ||
currentWorkspace: currentWorkspace, | ||
setWorkSpace: setCurrentWorkspace, | ||
createWorkSpace: async (name, path) => { | ||
const newAppData: AppData = { | ||
...appData, | ||
createWorkSpace: async (name, path) => { | ||
const newAppData: AppData = { | ||
...appData, | ||
workspaces: [ | ||
...appData.workspaces, | ||
{ | ||
name, | ||
dataSourceType: 'fs', | ||
dataSourceOptions: { | ||
sourcePath: path | ||
} | ||
} | ||
] | ||
} | ||
workspaces: [ | ||
...appData.workspaces, | ||
{ | ||
name, | ||
dataSourceType: 'fs', | ||
dataSourceOptions: { | ||
sourcePath: path, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
await LocalFileSystemDataSource.init({ sourcePath: path }); | ||
await fs.writeFile(appDataFile, JSON.stringify(newAppData)); | ||
setAppData(newAppData); | ||
} | ||
}}> | ||
{ props.children } | ||
</AppDataContext.Provider> | ||
); | ||
await LocalFileSystemDataSource.init({ sourcePath: path }); | ||
fsLib.writeFileSync(appDataFile, JSON.stringify(newAppData)); | ||
setAppData(newAppData); | ||
}, | ||
}} | ||
> | ||
{props.children} | ||
</AppDataContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export const Colors = { | ||
primary: '#2c3e50' | ||
} | ||
primary: '#2c3e50', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.