mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
refactor(docsync): share the scope picker, lane switch and folder name between desktop and phone
useScopePicker holds the provider's containers, binding one, and making and binding a new one under the suggested name; the desktop dialog keeps its search filter. docSyncModel holds the direction a lane switch leads to and the folder name made from the trip title, which the panel and the phone sheet each carried a copy of.
This commit is contained in:
@@ -16,6 +16,8 @@ import {
|
||||
} from '../../../../components/Files/docsync/useDocSync'
|
||||
import { useConnectForm } from '../../../../components/Files/docsync/useConnectForm'
|
||||
import { conflictPolicyKey, nextConflictPolicy } from '../../../../components/Files/docsync/DocSyncBits'
|
||||
import { slugFor, toggledDirection } from '../../../../components/Files/docsync/docSyncModel'
|
||||
import { NEW_SCOPE, useScopePicker } from '../../../../components/Files/docsync/useScopePicker'
|
||||
import { useConflicts } from '../../../../components/Files/docsync/useConflicts'
|
||||
import { relativeTime } from '../../../../utils/relativeTime'
|
||||
|
||||
@@ -294,10 +296,8 @@ function DetailView({
|
||||
/** The last remaining direction cannot be switched off. */
|
||||
const toggle = (lane: 'push' | 'pull') => {
|
||||
if (!canManage) return
|
||||
const nextPush = lane === 'push' ? !pushOn : pushOn
|
||||
const nextPull = lane === 'pull' ? !pullOn : pullOn
|
||||
if (!nextPush && !nextPull) return
|
||||
void sync.updateLink(link.id, { direction: nextPush && nextPull ? 'both' : nextPush ? 'push' : 'pull' })
|
||||
const next = toggledDirection(link.direction, lane)
|
||||
if (next) void sync.updateLink(link.id, { direction: next })
|
||||
}
|
||||
|
||||
const ranAt = link.lastSyncAt ? Date.parse(link.lastSyncAt) : NaN
|
||||
@@ -547,44 +547,12 @@ function ScopeView({
|
||||
onBound: () => void
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const [scopes, setScopes] = useState<Awaited<ReturnType<typeof sync.loadScopes>>['scopes'] | null>(null)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [name, setName] = useState(suggestedName)
|
||||
const [working, setWorking] = useState<string | null>(null)
|
||||
|
||||
// `loadScopes`, not `sync`: the hook hands back a fresh object on every
|
||||
// render of the panel above, so depending on it re-listed the provider's
|
||||
// folders each time anything up there changed. The callback itself is
|
||||
// stable. Taken out of `sync` first, because calling it as `sync.loadScopes`
|
||||
// inside the effect makes the whole object a dependency again.
|
||||
const { loadScopes } = sync
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
void (async () => {
|
||||
const res = await loadScopes(connectionId)
|
||||
if (cancelled) return
|
||||
setScopes(res.scopes)
|
||||
setError(res.error ?? null)
|
||||
})()
|
||||
return () => { cancelled = true }
|
||||
}, [connectionId, loadScopes])
|
||||
|
||||
const bind = async (scope: { scopeKey: string; label: string; remoteRootId: string | null; remoteRootPath: string | null }) => {
|
||||
setWorking(scope.scopeKey)
|
||||
const ok = await sync.createLink({
|
||||
connectionId,
|
||||
scopeKey: scope.scopeKey,
|
||||
remoteRootId: scope.remoteRootId,
|
||||
remoteRootPath: scope.remoteRootPath,
|
||||
remoteLabel: scope.label,
|
||||
direction: 'both',
|
||||
deletePolicy: 'unlink',
|
||||
conflictPolicy: 'manual',
|
||||
syncEnabled: true,
|
||||
})
|
||||
setWorking(null)
|
||||
if (ok) onBound()
|
||||
}
|
||||
const { scopes, error, name, setName, working, bind, createAndBind } = useScopePicker({
|
||||
connectionId,
|
||||
suggestedName,
|
||||
sync,
|
||||
onBound,
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="mt-3 flex flex-col gap-3">
|
||||
@@ -599,18 +567,11 @@ function ScopeView({
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return
|
||||
setWorking('__new__')
|
||||
void sync.createScope(connectionId, trimmed)
|
||||
.then(scope => { if (scope) return bind(scope) })
|
||||
.finally(() => setWorking(null))
|
||||
}}
|
||||
onClick={() => void createAndBind()}
|
||||
disabled={!name.trim() || working !== null}
|
||||
className="flex h-11 flex-none items-center gap-2 rounded-2xl bg-m-act px-4 text-[0.8125rem] font-bold text-m-actfg disabled:opacity-50"
|
||||
>
|
||||
{working === '__new__' ? <Loader2 size={15} className="animate-spin" /> : <FolderPlus size={15} strokeWidth={2.2} />}
|
||||
{working === NEW_SCOPE ? <Loader2 size={15} className="animate-spin" /> : <FolderPlus size={15} strokeWidth={2.2} />}
|
||||
{t('docsync.scope.createAction')}
|
||||
</button>
|
||||
</div>
|
||||
@@ -811,18 +772,6 @@ function Empty({ text, hint }: { text: string; hint?: string }) {
|
||||
)
|
||||
}
|
||||
|
||||
/** A folder name from the trip's own title, so nobody has to invent one. */
|
||||
function slugFor(title: string | undefined, tripId: number | string): string {
|
||||
const base = (title || 'trek')
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[̀-ͯ]/g, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 40)
|
||||
return `${base || 'trek'}-${tripId}`
|
||||
}
|
||||
|
||||
/**
|
||||
* The documents that changed in both places, with the way out.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user