Files
TREK/client/src/repo/docSyncRepo.ts
T
Maurice 843582863c fix(docsync): offer the sync button only where something is behind it
The Documents addon put a "Document sync" button on every trip's Files
tab, and the seed ships every provider switched off, so after the
upgrade each of those buttons led into "No document providers are
available". The button now shows on a trip that is already bound, to
every member, and otherwise only to somebody who may bind the trip on
an instance that offers a provider. Without it the Files toolbar is
the one from before the feature, trash flush right, on both shells.

/api/addons carries no document providers for a normal user, so the
answer comes from the docsync routes through a new online-only
docSyncRepo and is kept per session in docSyncOfferStore. A revisit
renders the last answer at once and checks it again; offline nothing
is asked and the last answer stands. Creating a binding, removing one
and removing a connection now ping the trip with docsync:changed and
zero counts, so a member already on the Files tab sees the button come
and go without leaving it.

Both shells keep listing a binding whose provider the admin has since
switched off instead of claiming there are no providers, and name its
store through one storeName() helper. A load that answers 403 or 404
clears the bindings as well, so they do not linger once the Documents
addon is gone.

The phone admin screen gets the Documents shelf the desktop one has:
the document providers as sub rows under the addon, toggled through the
same admin route and re-read after Documents itself is toggled, because
the server switches the providers off with it.

isTripOwner and the isOwner prop become canManageSync and canManage,
which is what canManageDocSync answers. Both folder pickers take
loadScopes out of the sync object before their effect, so
exhaustive-deps is satisfied without the effect running on every
render.
2026-09-19 13:36:33 +02:00

45 lines
1.6 KiB
TypeScript

import { docsyncApi } from '../api/client'
import { isEffectivelyOffline } from '../sync/networkMode'
/**
* The two reads behind the Files screen's "Document sync" button.
*
* Online-only with no Dexie table, for the reason `dawarichRepo` gives: both
* answers are the server's (which providers the admin switched on, whether the
* owner bound this trip), and the dialog the button opens cannot load offline
* either. The store in front of this keeps the last answer for the session.
*/
export class DocSyncOfflineError extends Error {
constructor() {
super('Document sync needs a connection')
this.name = 'DocSyncOfflineError'
}
}
function requireOnline(): void {
if (isEffectivelyOffline()) throw new DocSyncOfflineError()
}
export const docSyncRepo = {
/**
* Whether the instance offers a provider somebody could connect.
*
* The list is instance-wide; the route sits under a trip only because every
* document-sync route does. `available` is the same filter the dialog
* applies, so a provider row without an adapter does not bring up a button
* into an empty list.
*/
async offersProvider(tripId: number | string): Promise<boolean> {
requireOnline()
const providers = (await docsyncApi.providers(tripId)) as Array<{ available?: boolean }>
return providers.some(p => p.available)
},
/** Whether the trip is bound to at least one store. */
async isBound(tripId: number | string): Promise<boolean> {
requireOnline()
const links = (await docsyncApi.listLinks(tripId)) as unknown[]
return links.length > 0
},
}