Skip to content

Commit

Permalink
improve project page
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Mar 29, 2024
1 parent 31b778c commit e9efa08
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 181 deletions.
180 changes: 0 additions & 180 deletions src/components/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Tonic from '@socketsupply/tonic'
import fs from 'socket:fs'
import path from 'socket:path'
import process from 'socket:process'
import { exec } from 'socket:child_process'
import { Encryption, sha256 } from 'socket:network'

import Config from '../lib/config.js'
Expand Down Expand Up @@ -52,35 +51,6 @@ class AppProperties extends Tonic {
const project = document.querySelector('app-project')
const config = new Config(app.state.currentProject?.id)

if (event === 'org' || event === 'shared-secret') {
const app = this.props.parent
const config = new Config(app.state.currentProject?.id)
if (!config) return

let bundleId = await config.get('meta', 'bundle_identifier')
if (bundleId) bundleId = bundleId.replace(/"/g, '')
const { data: dataBundle } = await app.db.projects.get(bundleId)

if (event === 'org') {
dataBundle.org = el.value
dataBundle.clusterId = await sha256(el.value, { bytes: true })
}

if (event === 'shared-secret') {
const sharedKey = await Encryption.createSharedKey(el.value)
const derivedKeys = await Encryption.createKeyPair(sharedKey)
const subclusterId = Buffer.from(derivedKeys.publicKey)

dataBundle.sharedKey = sharedKey
dataBundle.sharedSecret = el.value
dataBundle.subclusterId = subclusterId
}

await app.db.projects.put(bundleId, dataBundle)
await app.initNetwork()
this.reRender()
}

//
// when the user wants to toggle one of the preview windows they have configured
//
Expand Down Expand Up @@ -110,25 +80,6 @@ class AppProperties extends Tonic {
}
}

async click (e) {
const elCopy = Tonic.match(e.target, '[symbol-id="copy-icon"]')

if (elCopy) {
navigator.clipboard.writeText(elCopy.nextElementSibling.value)
return
}

const el = Tonic.match(e.target, '[data-event]')
if (!el) return

const { event } = el.dataset

if (event === 'publish') {
const coDialogPublish = document.querySelector('dialog-publish')
if (coDialogPublish) coDialogPublish.show()
}
}

async loadProjectNode (node) {
this.reRender()
return true
Expand Down Expand Up @@ -164,103 +115,6 @@ class AppProperties extends Tonic {
}
}

let bundleId = await config.get('meta', 'bundle_identifier')
if (bundleId) bundleId = bundleId.replace(/"/g, '')

let project = {}

const { data: hasBundle } = await app.db.projects.has(bundleId)

if (hasBundle) {
const { data } = await app.db.projects.get(bundleId)
project = data
} else if (currentProject?.isDirectory) {
//
// The clusterId is hard coded for now.
//
const clusterId = await sha256('socket-app-studio', { bytes: true })

project.sharedSecret = (await Encryption.createId()).toString('hex')
const sharedKey = await Encryption.createSharedKey(project.sharedSecret)
const derivedKeys = await Encryption.createKeyPair(sharedKey)
const subclusterId = Buffer.from(derivedKeys.publicKey)

//
// Projects are keyed off the bundleId
//
Object.assign(project, {
bundleId,
clusterId,
subclusterId,
sharedKey,
org: 'union-app-studio',
})

await app.db.projects.put(bundleId, project)

//
// We need to tell the network to start listening for this subcluster
//
await app.initNetwork()
}

let projectUpdates = []
let gitStatus = { stdout: '' }

if (cwd) {
//
// If there is a current project, check if its been git await configtialized.
//
try {
await fs.promises.stat(path.join(cwd, '.git'))
} catch (err) {
try {
gitStatus = await exec('git await configt', { cwd })
} catch (err) {
gitStatus.stderr = err.message
}

if (gitStatus?.stderr.includes('command not found')) {
projectUpdates.push(this.html`
<tonic-toaster-inline
id="git-not-installed"
dismiss="false"
display="true"
>Git is not installed and is required to use this program.
</tonic-toaster-inline>
`)
}
}

//
// Try to get the status of the project to tell the user what
// has changed and help them decide if they should publish.
//
try {
gitStatus = await exec('git status --porcelain', { cwd })
} catch (err) {
gitStatus.stderr = err.message
}

projectUpdates = this.html`
<pre id="project-status"><code>No changes.</code></pre>
`

const notInitialized = gitStatus.stderr.includes('not a git repo')

if (notInitialized || (!gitStatus.stderr && gitStatus.stdout.length)) {
projectUpdates = this.html`
<pre id="project-status"><code>${gitStatus.stdout || 'No Changes.'}</code></pre>
<tonic-button
id="publish"
data-event="publish"
width="180px"
class="pull-right"
>Publish</tonic-button>
`
}
}

return this.html`
<tonic-accordion id="options" selected="preview-windows">
<h3>App Settings</h3>
Expand Down Expand Up @@ -315,40 +169,6 @@ class AppProperties extends Tonic {
<tonic-checkbox data-section="permissions" id="allow_airplay" checked="${await config.get('permissions', 'allow_airplay')}" data-event="property" label="AirPlay"></tonic-checkbox>
<tonic-checkbox data-section="permissions" id="allow_hotkeys" checked="${await config.get('permissions', 'allow_hotkeys')}" data-event="property" label="AirPlay"></tonic-checkbox>
</tonic-accordion-section>
<tonic-accordion-section
name="share-settings"
id="share-settings"
label="Sharing"
>
<tonic-input
label="Organization"
id="org-name"
data-event="org"
spellcheck="false"
value="${project.org}"
></tonic-input>
<tonic-input
label="Shared Secret"
id="shared-secret"
data-event="shared-secret"
spellcheck="false"
value="${project.sharedSecret}"
></tonic-input>
<tonic-input
label="Project Link"
id="project-link"
symbol-id="copy-icon"
position="right"
spellcheck="false"
readonly="true"
value="union://${project.sharedSecret}?id=${encodeURIComponent(bundleId)}&org=${project.org}"
></tonic-input>
<label>Project Status</label>
${projectUpdates}
</tonic-accordion-section>
</tonic-accordion>
`
}
Expand Down
29 changes: 29 additions & 0 deletions src/css/view-project-summary.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ view-project-summary .empty-state {
justify-content: center;
align-content: center;
}

view-project-summary .sharing {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap: 20px;
grid-template-rows: auto auto 1fr;
align-items: end;
}

view-project-summary #org-name {
grid-area: 1 / span 2;
}

view-project-summary #shared-secret {
grid-area: 1 / 2 span;
}

view-project-summary #project-link {
grid-area: 2 / span 3;
}

view-project-summary #publish {
grid-area: 2 / 4;
}

view-project-summary #project-status {
grid-area: 3 / span 4;
align-self: start;
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,18 @@ class AppView extends Tonic {
async createProject (opts = {}) {
const name = opts.name || 'project.' + Math.random().toString(16).slice(2, 6)
const bundleId = 'com.' + name
const org = opts.clusterId || 'union-app-studio'
const sharedSecret = opts.sharedSecret || (await Encryption.createId()).toString('base64')
const sharedKey = await Encryption.createSharedKey(sharedSecret)
const derivedKeys = await Encryption.createKeyPair(sharedKey)
const clusterId = await Encryption.createClusterId(opts.clusterId || 'union-app-studio')
const clusterId = await Encryption.createClusterId(org)
const subclusterId = Buffer.from(derivedKeys.publicKey)

const project = {
label: name,
waiting: opts.waiting || false,
path: opts.path || path.join(path.DATA, name),
org,
bundleId,
clusterId,
subclusterId,
Expand All @@ -334,6 +336,7 @@ class AppView extends Tonic {

try {
await exec('ssc init', { cwd: project.path })
await exec('git init', { cwd: project.path })
} catch (err) {
console.error(err)
}
Expand Down
Loading

0 comments on commit e9efa08

Please sign in to comment.