feat: improve upload flow (#240)

* feat: separate flow for folder and file uploads

* feat: add basic index document detection

* feat(wip): separate preview step

* fix: fix kb and mb units

* feat: add post upload summary, add some styling

* feat: upload flow

* fix: change element order and add conditional rendering

* refactor: remove unused variables for now

* fix: put back stamp creation to stamp page

* refactor: rework postage stamps and grid

* feat: add website and folder icons

* feat: add asset preview to download flow, add file icon

* feat: add basic design to postage stamp selection dialog

* feat: add web icon, shorten stamp in preview

* feat: extract swarm hash in download flow

* fix: extract swarmbutton and solve icon hover and focus color

* fix: always show buy button on stamp page

* refactor: downgrade

* refactor: speed up icon transition

* style: improve download buttons

* style: change [back to upload] icon

* style: add spacing before swarm gateway text

* style: post upload summary spacing

* refactor: drop verticalspacing and use box

* refactor: merge icons to one component

* refactor: use conditions instead of weird assignment

* docs: explain filter(x => x)

* refactor: generalize capacity

* refactor: avoid passing arrow functions

* refactor: get rid of PaperGridContainer and Container

* fix: fix hover color for postage stamps

* feat: add disabled and loading state to buttons

* fix: make drag and drop work for websites

* feat: handle folders and non existing hashes

* fix: provide empty default value to select to avoid console warning

* style: remove body2 font variants

* fix: remove typo

* feat: disable folder upload, add website upload

* fix: disable showPreviews to avoid flickering

* feat(temp): remove folder upload

* fix: remove stuck focus on buttons even after rendering different buttons

* style: merge hover and focus styles, fix safari text wrap issue

* style: remove dropbox outline in safari
This commit is contained in:
Cafe137
2021-11-25 09:54:03 +01:00
committed by GitHub
parent 82cf6d9c01
commit 635621b04a
31 changed files with 1187 additions and 183 deletions
+24
View File
@@ -0,0 +1,24 @@
export class SwarmFile {
public name: string
public path: string
public type: string
public size: number
public webkitRelativePath: string
public arrayBuffer: () => Promise<ArrayBuffer>
private data: Promise<ArrayBuffer>
constructor(file: File) {
const path = Reflect.get(file, 'path') || file.webkitRelativePath || file.name
this.path = path.startsWith('/') ? path.slice(1) : path
this.webkitRelativePath = this.path
this.name = file.name
this.type = file.type
this.size = file.size
this.data = file.arrayBuffer()
this.arrayBuffer = async () => {
const data = await this.data
return data
}
}
}
+57
View File
@@ -0,0 +1,57 @@
import { detectIndexHtml } from './file'
describe('file utils', () => {
it('detectIndexHtml should find index.html', () => {
expect(
detectIndexHtml([
{ name: 'swarm.png', path: 'swarm.png' },
{ name: 'index.html', path: 'index.html' },
]),
).toBe('index.html')
})
it('detectIndexHtml should find index.htm', () => {
expect(
detectIndexHtml([
{ name: 'index.htm', path: 'index.htm' },
{ name: 'swarm.png', path: 'swarm.png' },
]),
).toBe('index.htm')
})
it('detectIndexHtml should find nested index.html', () => {
expect(
detectIndexHtml([
{ name: 'swarm.png', path: 'sample-folder/swarm.png' },
{ name: 'index.html', path: 'sample-folder/index.html' },
]),
).toBe('index.html')
})
it('detectIndexHtml should not find nested index.htm when ambigous', () => {
expect(
detectIndexHtml([
{ name: 'index.htm', path: 'sample-folder/index.htm' },
{ name: 'swarm.png', path: 'other-folder/swarm.png' },
]),
).toBe(false)
})
it('detectIndexHtml should not find deep index.html', () => {
expect(
detectIndexHtml([
{ name: 'index.html', path: 'sample-folder/index.html' },
{ name: 'swarm.png', path: 'swarm.png' },
]),
).toBe(false)
})
it('detectIndexHtml should return false when no matches appear', () => {
expect(
detectIndexHtml([
{ name: 'swarm.png', path: 'swarm.png' },
{ name: 'swarm.jpg', path: 'swarm.jpg' },
]),
).toBe(false)
})
})
+51
View File
@@ -0,0 +1,51 @@
import { FileData } from '@ethersphere/bee-js'
import { SwarmFile } from './SwarmFile'
const indexHtmls = ['index.html', 'index.htm']
export function detectIndexHtml(files: SwarmFile[]): string | false {
if (!files.length) {
return false
}
const exactMatch = files.find(x => indexHtmls.includes(x.path))
if (exactMatch) {
return exactMatch.name
}
const prefix = files[0].path.split('/')[0] + '/'
const allStartWithSamePrefix = files.every(x => x.path.startsWith(prefix))
if (allStartWithSamePrefix) {
const match = files.find(x => indexHtmls.map(y => prefix + y).includes(x.path))
if (match) {
return match.name
}
}
return false
}
export function getHumanReadableFileSize(bytes: number): string {
if (bytes >= 1e6) {
return (bytes / 1e6).toFixed(2) + ' MB'
}
if (bytes >= 1e3) {
return (bytes / 1e3).toFixed(2) + ' kB'
}
return bytes + ' bytes'
}
export function convertBeeFileToBrowserFile(file: FileData<ArrayBuffer>): Partial<File> {
return {
name: file.name,
size: file.data.byteLength,
type: file.contentType,
arrayBuffer: () => new Promise(resolve => resolve(file.data)),
}
}
+47 -1
View File
@@ -1,5 +1,5 @@
import BigNumber from 'bignumber.js'
import { isInteger, makeBigNumber } from './index'
import { extractSwarmHash, isInteger, makeBigNumber } from './index'
describe('utils', () => {
describe('isInteger', () => {
@@ -57,4 +57,50 @@ describe('utils', () => {
})
})
})
describe('extractSwarmHash', () => {
test('should return 64 hash', () => {
expect(extractSwarmHash('7f0fe712cdd78bdea52d040369eb32b6af5ecd01fa5ae49b7506412abdd81ac3')).toBe(
'7f0fe712cdd78bdea52d040369eb32b6af5ecd01fa5ae49b7506412abdd81ac3',
)
})
test('should return 128 hash', () => {
expect(
extractSwarmHash(
'd1829242c4d08e9f914fedfb1f68aacd62826d75370a9a57a80c9e6e6a49983c767c013be9aa4319e34fd8323ef0f2a57426b30e66c87e219f6f6359e2595e7f',
),
).toBe(
'd1829242c4d08e9f914fedfb1f68aacd62826d75370a9a57a80c9e6e6a49983c767c013be9aa4319e34fd8323ef0f2a57426b30e66c87e219f6f6359e2595e7f',
)
})
test('should return 64 hash from url', () => {
expect(
extractSwarmHash('http://localhost:1633/bzz/7f0fe712cdd78bdea52d040369eb32b6af5ecd01fa5ae49b7506412abdd81ac3/'),
).toBe('7f0fe712cdd78bdea52d040369eb32b6af5ecd01fa5ae49b7506412abdd81ac3')
})
test('should return 128 hash from url', () => {
expect(
extractSwarmHash(
'http://localhost:1633/bzz/d1829242c4d08e9f914fedfb1f68aacd62826d75370a9a57a80c9e6e6a49983c767c013be9aa4319e34fd8323ef0f2a57426b30e66c87e219f6f6359e2595e7f/',
),
).toBe(
'd1829242c4d08e9f914fedfb1f68aacd62826d75370a9a57a80c9e6e6a49983c767c013be9aa4319e34fd8323ef0f2a57426b30e66c87e219f6f6359e2595e7f',
)
})
test('should return null when nothing is found', () => {
expect(extractSwarmHash('Bee Dashboard')).toBe(null)
})
test('should return null when length is incorrect', () => {
expect(extractSwarmHash('7f0fe712cdd78bdea52d040369eb32b6af5ecd01fa5ae49b7506412abdd81a')).toBe(null)
})
test('should return null when alphanumeric', () => {
expect(extractSwarmHash('gkQ6duo5iHJ099g908P0t17ZWFf8Ke2klrywLP5BGtLkcaEC5W0kLEfbe4wUnDI6')).toBe(null)
})
})
})
+6
View File
@@ -106,3 +106,9 @@ export function makeRetriablePromise<T>(fn: () => Promise<T>, maxRetries = 3, de
}
})
}
export function extractSwarmHash(string: string): string | null {
const matches = string.match(/[a-fA-F0-9]{64,128}/)
return (matches && matches[0]) || null
}