better code
Massimo Melina committed
Dec 2, 2023 at 19:51 UTC
54bd3af08228e5bd4fde644209c7a6b720798304
4 files changed
+13
-11
dev.md
+4
-1
@@ -47,11 +47,14 @@ Additionally, you have the following folders:
47
48
# Guidelines
49
50
-- For strings, I'm trying to use double-quotes or backticks for text that's read by the user, and single-quotes elsewhere.
50
+- For strings, I'm using double-quotes for text that's read by the user, and single-quotes elsewhere. Backticks can be any.
51
- All objects that go in yaml should use snake_case.
52
- Reason: we want something that is both easy for the user and maps directly in our code.
53
Spaces and kebab-case don't play well with javascript and camel is less readable for the user.
54
- API names should start with get_ if and only if they provide information without making changes.
55
+- All parameters that contain a *uri* should have a name that starts with `uri`.
56
+- React parts don't use JSX. I used JSX for a couple of years before deciding that it is not good enough to pay the
57
+ price of using an extra language that is also necessary to switched in and out multiple times when stuff is nested.
58
59
# Project design
60
frontend/src/BrowseFiles.ts
+6
-7
@@ -22,6 +22,10 @@ export function BrowseFiles() {
22
const { error } = useSnapState()
23
const navigate = useNavigate()
24
const { props, tile_size=0 } = useSnapState()
25
+ const propsDropFiles = useMemo(() => acceptDropFiles(files =>
26
+ props?.can_upload ? enqueue(files.map(file => ({ file })))
27
+ : alertDialog(t("Upload not available"), 'warning') ),
28
+ [props])
29
if (!useAuthorized())
30
return h(CustomCode, { name: 'unauthorized',
31
ifEmpty: () => h('h1', {
@@ -29,11 +33,7 @@ export function BrowseFiles() {
33
onClick: () => loginDialog(navigate)
34
}, t`Unauthorized`)
35
})
32
- return h('div', { // element dedicated to drop-files to cover full screen
33
- ...acceptDropFiles(files =>
34
- props?.can_upload ? enqueue(files.map(file => ({ file })))
35
- : alertDialog(t("Upload not available"), 'warning') )
36
- },
36
+ return h('div', propsDropFiles, // element dedicated to drop-files to cover full screen
37
h('div', {
38
className: 'list-wrapper ' + (tile_size ? 'tiles-mode' : 'list-mode'),
39
style: { '--tile-size': tile_size },
@@ -145,7 +145,7 @@ const Paging = memo(({ nPages, current, pageSize, pageChange, atBottom }: Paging
145
const shrink = nPages > 20
146
const from = _.floor(current, -1)
147
const to = from + 10
148
- return h('div', { id:'paging' },
148
+ return h('div', { id: 'paging' },
149
h('button', {
150
className: !current ? 'toggled' : undefined,
151
onClick() { pageChange(0) },
@@ -206,7 +206,6 @@ const Entry = memo(({ entry, midnight, separator }: EntryProps) => {
206
disabled: isLink,
207
value: selected[uri],
208
onChange(v){
209
- debugger
209
if (v)
210
return state.selected[uri] = true
211
delete state.selected[uri]
src/comments.ts
+2
-2
@@ -3,7 +3,7 @@ import { dirname, join } from 'path'
3
import { basename } from './cross'
4
import { parseFile, parseFileCache } from './util-files'
5
import { writeFile } from 'fs/promises'
6
-import { singleFromBatch } from './misc'
6
+import { singleWorkerFromBatchWorker } from './misc'
7
import _ from 'lodash'
8
import iconv from 'iconv-lite'
9
@@ -16,7 +16,7 @@ export async function getCommentFor(path?: string) {
16
: readDescription(dirname(path)).then(x => x.get(basename(path)), () => undefined)
17
}
18
19
-export const setCommentFor = singleFromBatch(async (jobs: [path: string, comment: string][]) => {
19
+export const setCommentFor = singleWorkerFromBatchWorker(async (jobs: [path: string, comment: string][]) => {
20
const byFolder = _.groupBy(jobs, job => dirname(job[0]))
21
return Promise.allSettled(_.map(byFolder, async (jobs, folder) => {
22
const comments = await readDescription(folder).catch(() => new Map())
src/debounceAsync.ts
+1
-1
@@ -69,7 +69,7 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
69
}
70
71
// given a function that works on a batch of requests, returns the function that works on a single request
72
-export function singleFromBatch<Args extends any[]>(batchWorker: (batch: Args[]) => unknown) {
72
+export function singleWorkerFromBatchWorker<Args extends any[]>(batchWorker: (batch: Args[]) => unknown) {
73
let batch: Args[] = []
74
const debounced = debounceAsync(async () => {
75
const ret = batchWorker(batch)