Files
TREK/client/src/store/docSyncOfferStore.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

49 lines
2.0 KiB
TypeScript

import { create } from 'zustand'
import { docSyncRepo } from '../repo/docSyncRepo'
/**
* Whether a trip's Files screen offers document sync, remembered for the session.
*
* The seed ships every document provider switched off, so a button on every
* trip led, straight after the upgrade, into "No document providers are
* available". It shows where there is something behind it: a trip that is
* already bound (every member needs to see where their documents go), or
* somebody who may bind it on an instance that offers a provider.
*
* Remembered so that coming back to the tab renders the last answer at once,
* instead of the button popping in after a request every time.
*/
interface DocSyncOfferState {
/** Per trip id, whether it has a binding. Missing until asked. */
bound: Record<string, boolean>
/** Whether the instance offers any provider. Null until asked. */
providers: boolean | null
refresh: (tripId: number | string, canManage: boolean) => Promise<void>
}
export const useDocSyncOfferStore = create<DocSyncOfferState>((set) => ({
bound: {},
providers: null,
refresh: async (tripId, canManage) => {
const key = String(tripId)
try {
// Somebody who may bind the trip gets the button as soon as a provider
// is on, bound or not, so the second request is for everybody else.
if (canManage) {
const providers = await docSyncRepo.offersProvider(tripId)
set({ providers })
if (providers) return
}
const bound = await docSyncRepo.isBound(tripId)
set(s => ({ bound: { ...s.bound, [key]: bound } }))
} catch (err) {
// 403 is the Documents addon switched off and 404 a trip this person no
// longer reaches: both a real "no". Anything else, offline included, is
// no answer at all, and the last one stands.
const status = (err as { response?: { status?: number } })?.response?.status
if (status === 403 || status === 404) set(s => ({ bound: { ...s.bound, [key]: false } }))
}
},
}))