Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to switch out your project and env from the electron app #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"jquery": "^3.6.0",
"lodash-es": "^4.17.21",
"monaco-editor": "^0.33.0",
"netrc": "^0.1.4",
"path-browserify": "^1.0.1",
"pg-format": "^1.0.4",
"pluralize": "^8.0.0",
Expand Down
58 changes: 56 additions & 2 deletions src/components/dashboard/DashboardPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '../../svgs/icons'
import useCurrentProject from '../../hooks/useCurrentProject'
import logger from '../../utils/logger'
import SelectInput from '../shared/inputs/SelectInput'

const className = 'dashboard'
const pcn = getPCN(className)
Expand Down Expand Up @@ -54,6 +55,30 @@ function DashboardPage(props) {
const [seedCursors, setSeedCursors] = useState([])
const [currentSchemaName, _] = useState(DEFAULT_SCHEMA_NAME)
const [tables, setTables] = useState(null)
const [projects, setProjects] = useState([])
const [databaseEnvironments, setDatabaseEnvironments] = useState([])
const [currentProjectEnv, setCurrentProjectEnv] = useState({
project: null,
env: null,
})

const getProjects = async () => {
const currEnv = await window.electronAPI.showEnv()
const currProject = await window.electronAPI.showProject()
const projects = await window.electronAPI.getUserProjects()
const newDatabaseEnvironments = await window.electronAPI.getCurrentProjectEnvs(currProject)

setDatabaseEnvironments(newDatabaseEnvironments)
setProjects(projects)
setCurrentProjectEnv({
project: currProject,
env: currEnv,
})
}

useEffect(() => {
getProjects()
}, [])

const tableNames = useMemo(() => tables?.map(t => t.name) || null, [tables])
const tablesBodyRef = useRef()
Expand Down Expand Up @@ -269,7 +294,36 @@ function DashboardPage(props) {
<div className={pcn('__content-liner')}>
<div className={pcn('__content-header')}>
<div className={pcn('__content-header-left')}>
{ renderHeaderProjectPath() }
<SelectInput
options={projects.map(p => ({ value: p, label: p }))}
classNamePrefix='spec'
isRequired={true}
updateFromAbove={true}
className={pcn('__col-input-field')}
placeholder={`${project?.org || 'org'}/${project?.name || 'project'}`}
onChange={async (val) => {
await window.electronAPI.useProject(val)
getProjects()
}}
value={currentProjectEnv.project ? currentProjectEnv.project : "Select a project..."}
/>
<SelectInput
options={databaseEnvironments.map(p => ({ value: p, label: p }))}
classNamePrefix='spec'
isRequired={true}
updateFromAbove={true}
className={pcn('__col-input-field')}
placeholder={`${currentProjectEnv.env || 'local'}`}
onChange={async (val) => {
window.electronAPI.useEnv(val)
setCurrentProjectEnv({
...currentProjectEnv,
env: val,
})}}
value={currentProjectEnv.env ? currentProjectEnv.env : "Select a database environment..."}
/>
</div>
<div>
</div>
<div className={pcn('__content-header-right')}>
<div className={pcn('__header-buttons')}>
Expand All @@ -289,7 +343,7 @@ function DashboardPage(props) {
</div>
</div>
</div>
), [currentTable, renderHeaderProjectPath, renderContentBodyComp])
), [currentTable, renderHeaderProjectPath, renderContentBodyComp, currentProjectEnv, projects, databaseEnvironments])

return (
<div className={className}>
Expand Down
9 changes: 9 additions & 0 deletions src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const {
performQuery,
} = require('@spec.dev/app-db')
const url = require('url')
const { useProject, getUserProjects, getCurrentProjectEnvs, useEnv, showEnv, showProject } = require('./services/crudEnv/projectAndEnv')


const { stringify, parse } = JSON

Expand Down Expand Up @@ -262,6 +264,13 @@ app.whenReady().then(() => {
ipcMain.handle('query', query)
ipcMain.handle('createSpecClient', createSpecClient)
ipcMain.handle('killSpecClient', killSpecClient)
ipcMain.handle('useProject', useProject)
ipcMain.handle('getProjects', getProjects)
ipcMain.handle('getUserProjects', getUserProjects)
ipcMain.handle('getCurrentProjectEnvs', getCurrentProjectEnvs)
ipcMain.handle('useEnv', useEnv)
ipcMain.handle('showProject', showProject)
ipcMain.handle('showEnv', showEnv)
createWindow()
})

Expand Down
7 changes: 7 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ contextBridge.exposeInMainWorld('electronAPI', {
subscribeToPath: (...args) => ipcRenderer.invoke('subscribeToPath', ...args),
createSpecClient: (...args) => ipcRenderer.invoke('createSpecClient', ...args),
killSpecClient: () => ipcRenderer.invoke('killSpecClient'),
useProject: (...args) => ipcRenderer.invoke('useProject', ...args),
getProjects: (...args) => ipcRenderer.invoke('getProjects', ...args),
getUserProjects: (...args) => ipcRenderer.invoke('getUserProjects', ...args),
getCurrentProjectEnvs: (...args) => ipcRenderer.invoke('getCurrentProjectEnvs', ...args),
useEnv: (...args) => ipcRenderer.invoke('useEnv', ...args),
showProject: (...args) => ipcRenderer.invoke('showProject', ...args),
showEnv: (...args) => ipcRenderer.invoke('showEnv', ...args),
on: (...args) => ipcRenderer.on(...args),
off: (...args) => ipcRenderer.removeAllListeners(...args),
send: (...args) => ipcRenderer.send(...args),
Expand Down
74 changes: 74 additions & 0 deletions src/services/crudEnv/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const path = require('path')
const os = require('os')

const ev = (name, fallback) =>
process.env.hasOwnProperty(name) ? process.env[name] : fallback

const constants = {
// Spec project config.
SPEC_CONFIG_DIR_NAME: '.spec',
CONNECTION_CONFIG_FILE_NAME: 'connect.toml',
PROJECT_CONFIG_FILE_NAME: 'project.toml',
MIGRATIONS_DIR_NAME: 'migrations',
HANDLERS_DIR_NAME: 'handlers',
HOOKS_DIR_NAME: 'hooks',
GRAPHQL_DIR_NAME: 'graphql',

// Global CLI config.
SPEC_GLOBAL_DIR: path.join(os.homedir(), '.spec'),
SPEC_GLOBAL_STATE_FILE_NAME: 'state.toml',
SPEC_GLOBAL_CREDS_FILE_NAME: 'creds.toml',
SPEC_GLOBAL_PROJECTS_FILE_NAME: 'projects.toml',

// Spec base/ecosystem.
SPEC_ORIGIN: ev('SPEC_ORIGIN', 'https://spec.dev'),

// Spec API config.
SPEC_API_ORIGIN: ev('SPEC_API_ORIGIN', 'https://api.spec.dev'),
USER_AUTH_HEADER_NAME: 'Spec-User-Auth-Token',

// DB defaults.
SPEC_DB_USER: 'spec',
DB_PORT: 5432,

// Live Table testing.
LIVE_OBJECT_TESTING_DB_NAME: 'live-object-testing',
LIVE_OBJECT_TESTING_API_PORT: 8000,

// Desktop app name.
SPEC_APP_NAME: 'Spec',

// Default log size.
DEFAULT_LOG_TAIL_SIZE: 20,
}

constants.SPEC_CONFIG_DIR = path.join(process.cwd(), constants.SPEC_CONFIG_DIR_NAME)
constants.CONNECTION_CONFIG_PATH = path.join(
constants.SPEC_CONFIG_DIR,
constants.CONNECTION_CONFIG_FILE_NAME
)
constants.PROJECT_CONFIG_PATH = path.join(
constants.SPEC_CONFIG_DIR,
constants.PROJECT_CONFIG_FILE_NAME
)
constants.SPEC_GLOBAL_STATE_PATH = path.join(
constants.SPEC_GLOBAL_DIR,
constants.SPEC_GLOBAL_STATE_FILE_NAME
)
constants.SPEC_GLOBAL_CREDS_PATH = path.join(
constants.SPEC_GLOBAL_DIR,
constants.SPEC_GLOBAL_CREDS_FILE_NAME
)
constants.SPEC_GLOBAL_PROJECTS_PATH = path.join(
constants.SPEC_GLOBAL_DIR,
constants.SPEC_GLOBAL_PROJECTS_FILE_NAME
)
constants.SPEC_GLOBAL_COMPOSE_DIR = path.join(constants.SPEC_GLOBAL_DIR, 'compose')
constants.SPEC_HANDLERS_DIR = path.join(constants.SPEC_CONFIG_DIR, constants.HANDLERS_DIR_NAME)
constants.SPEC_HOOKS_DIR = path.join(constants.SPEC_CONFIG_DIR, constants.HOOKS_DIR_NAME)


module.exports = {
constants,
ev
}
Loading