Skip to content

Commit

Permalink
wip ui
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Apr 1, 2024
1 parent 27545a3 commit c10f09c
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 62 deletions.
2 changes: 1 addition & 1 deletion socket.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
; copy_map = src/mapping.ini

; An list of environment variables, separated by commas.
env = HOME, USER, TMPDIR, PWD, DEBUG
env = HOME, USER, TMPDIR, PWD, DEBUG, UNION_RESET

; Advanced Compiler Settings (ie C++ compiler -02, -03, etc).
flags = -O3
Expand Down
64 changes: 62 additions & 2 deletions src/components/patch-requests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Tonic from '@socketsupply/tonic'
import path from 'socket:path'
import fs from 'socket:fs'
import { exec } from 'socket:child_process'

class PatchRequests extends Tonic {
async click (e) {
Expand Down Expand Up @@ -29,11 +32,25 @@ class PatchRequests extends Tonic {
}

if (event === 'apply') {
console.log('APPLY', patch)
const { err: errApply } = await this.applyPatch(patch)

if (!errApply) {
await app.db.keys.del(patch.patchId)
this.reRender()
} else {
const notifications = document.querySelector('#notifications')

return notifications.create({
type: 'error',
title: 'Unable to apply patch',
message: errApply.message
})
}
}

if (event === 'trash') {
console.log('TRASH', patch)
await app.db.keys.del(patch.patchId)
this.reRender()
}

if (event === 'trust') {
Expand All @@ -50,6 +67,49 @@ class PatchRequests extends Tonic {
}
}

async applyPatch (patch) {
const coEditor = document.querySelector('app-editor')
const coProject = document.querySelector('app-project')
const app = this.props.app

const currentProject = app.state.currentProject
let output = {}

const name = (patch.patchId || patch.headers.parent.slice(6)) + '.patch'
const tmpFile = path.join(currentProject.id, name)
let error

try {
await fs.promises.writeFile(tmpFile, patch.src)
} catch (err) {
console.error(err)
return { err }
}

try {
output = await exec(`git am "${tmpFile}"`, { cwd: currentProject.id })
} catch (err) {
await fs.promises.unlink(tmpFile)
return { err }
}

await fs.promises.unlink(tmpFile)

if (output?.stderr) {
try {
await exec(`git am --abort"`, { cwd: currentProject.id })
} catch {}

return {
err: new Error('The patch can not be applied because the data in the patch does not match the data in the index.')
}
}

return {
data: output.stdout
}
}

async render () {
const app = this.props.app

Expand Down
38 changes: 5 additions & 33 deletions src/components/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import application from 'socket:application'
import fs from 'socket:fs'
import path from 'socket:path'
import { lookup } from 'socket:mime'
import { cp, rm } from '../lib/fs.js'

import Tonic from '@socketsupply/tonic'

Expand All @@ -10,38 +11,6 @@ const CLOSED_STATE = 0
const NOT_SELECTED = 0
const IS_SELECTED = 1

async function rm (directory) {
const files = await fs.promises.readdir(directory, { withFileTypes: true })

for (const file of files) {
const filePath = path.join(directory, file.name)

if (file.isDirectory()) {
await rm(filePath)
} else {
await fs.promises.unlink(filePath)
}
}

await fs.promises.rmdir(directory)
}

async function cp (srcDir, destDir) {
await fs.promises.mkdir(destDir, { recursive: true })
const files = await fs.promises.readdir(srcDir, { withFileTypes: true })

for (const file of files) {
const srcPath = path.join(srcDir, file.name)
const destPath = path.join(destDir, file.name)

if (file.isDirectory()) {
await cp(srcPath, destPath)
} else {
await fs.promises.copyFile(srcPath, destPath, fs.constants.COPYFILE_FICLONE)
}
}
}

class AppProject extends Tonic {
createCount = 0
contextCut = null
Expand Down Expand Up @@ -195,7 +164,6 @@ class AppProject extends Tonic {
await fs.promises.copyFile(srcNode.id, destDir, fs.constants.COPYFILE_FICLONE)
}
} catch (err) {
console.log(err)
return notifications.create({
type: 'error',
title: 'Unable to copy files',
Expand Down Expand Up @@ -626,6 +594,10 @@ class AppProject extends Tonic {
this.load()
}

async reload () {
this.load()
}

async load () {
const app = this.props.parent
const oldState = this.state.tree
Expand Down
7 changes: 3 additions & 4 deletions src/components/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@ export class DialogSubscribe extends TonicDialog {
const coInput = this.querySelector('#subscribe-shared-secret')
const url = new URL(coInput.value.trim())

const cId = url.searchParams.get('org')
const bundleId = url.searchParams.get('id')

// union://foo?id=com.demo.project&org=test

if (!bundleId || !cId) {
if (!bundleId) {
notifications.create({
type: 'error',
title: 'Error',
message: 'Invalid Project Link'
message: 'Invalid Project Link: expected property "id".'
})

super.hide()
return
}

const sharedSecret = url.hostname
const clusterId = await sha256(cId, { bytes: true })
const clusterId = await sha256('union-app-studio', { bytes: true })
const sharedKey = await Encryption.createSharedKey(sharedSecret)
const derivedKeys = await Encryption.createKeyPair(sharedKey)
const subclusterId = Buffer.from(derivedKeys.publicKey)
Expand Down
2 changes: 1 addition & 1 deletion src/css/view-project-summary.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ view-project-summary .sharing {
align-items: end;
}

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

Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import components from '@socketsupply/components'
import Indexed from '@socketsupply/indexed'

import { Patch } from './git-data.js'
import { cp, rm } from './lib/fs.js'

import { RelativeDate } from './components/relative-date.js'
import { GitStatus } from './components/git-status.js'
Expand Down Expand Up @@ -308,7 +309,7 @@ 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 org = 'union-app-studio'
const sharedSecret = opts.sharedSecret || (await Encryption.createId()).toString('base64')
const sharedKey = await Encryption.createSharedKey(sharedSecret)
const derivedKeys = await Encryption.createKeyPair(sharedKey)
Expand Down Expand Up @@ -341,7 +342,8 @@ class AppView extends Tonic {
}

async initData () {
if (process.env.DEBUG === '1') {
if (process.env.UNION_RESET === '1') {
await rm(path.DATA)
const databases = await window.indexedDB.databases()
for (const { name } of databases) await Indexed.drop(name)
}
Expand Down
34 changes: 34 additions & 0 deletions src/lib/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fs from 'socket:fs'
import path from 'socket:path'

export async function rm (directory) {
const files = await fs.promises.readdir(directory, { withFileTypes: true })

for (const file of files) {
const filePath = path.join(directory, file.name)

if (file.isDirectory()) {
await rm(filePath)
} else {
await fs.promises.unlink(filePath)
}
}

await fs.promises.rmdir(directory)
}

export async function cp (srcDir, destDir) {
await fs.promises.mkdir(destDir, { recursive: true })
const files = await fs.promises.readdir(srcDir, { withFileTypes: true })

for (const file of files) {
const srcPath = path.join(srcDir, file.name)
const destPath = path.join(destDir, file.name)

if (file.isDirectory()) {
await cp(srcPath, destPath)
} else {
await fs.promises.copyFile(srcPath, destPath, fs.constants.COPYFILE_FICLONE)
}
}
}
35 changes: 16 additions & 19 deletions src/views/project-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,14 @@ class ViewProjectSummary extends Tonic {

const { data: dataProject } = await app.db.projects.get(currentProject.projectId)

if (event === 'org' || event === 'shared-secret') {
if (event === 'org') {
dataProject.org = el.value
dataProject.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)

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

dataProject.sharedKey = sharedKey
dataProject.sharedSecret = el.value
dataProject.subclusterId = subclusterId

await app.db.projects.put(currentProject.projectId, dataProject)
await app.initNetwork()
Expand Down Expand Up @@ -88,11 +81,15 @@ class ViewProjectSummary extends Tonic {
items = this.html`
<div class="sharing">
<tonic-input
label="Organization"
id="org-name"
data-event="org"
label="Location"
id="project-path"
data-event="project-path"
spellcheck="false"
readonly="true"
symbol-id="settings-icon"
position="right"
spellcheck="false"
value="${dataProject.org}"
value="${dataProject.path}"
></tonic-input>
<tonic-input
Expand Down

0 comments on commit c10f09c

Please sign in to comment.