MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / saveRemoteWorkspaceProfile

Function saveRemoteWorkspaceProfile

apps/desktop/src/main/index.ts:1592–1663  ·  view source on GitHub ↗
(
  input: RemoteWorkspaceProfileInput & { lastConnectedAt?: number | null }
)

Source from the content-addressed store, hash-verified

1590}
1591
1592async function saveRemoteWorkspaceProfile(
1593 input: RemoteWorkspaceProfileInput & { lastConnectedAt?: number | null }
1594): Promise<RemoteWorkspaceProfile> {
1595 const normalizedId = input.id?.trim() || randomUUID()
1596 await updateConfig((cfg) => {
1597 const normalizedBaseUrl = normalizeRemoteBaseUrl(input.baseUrl)
1598 const trimmedName = input.name?.trim() || ''
1599 const normalizedVaultPath = input.vaultPath?.trim() || null
1600 if (!normalizedId || !normalizedBaseUrl) {
1601 throw new Error('Remote workspace profiles need a server URL.')
1602 }
1603 const nextNormalized: PersistedRemoteWorkspaceProfile = {
1604 id: normalizedId,
1605 name:
1606 trimmedName ||
1607 deriveRemoteWorkspaceProfileName(
1608 {
1609 id: normalizedId,
1610 baseUrl: normalizedBaseUrl,
1611 vaultPath: normalizedVaultPath
1612 },
1613 cfg.remoteWorkspaceProfiles
1614 ),
1615 baseUrl: normalizedBaseUrl,
1616 vaultPath: normalizedVaultPath,
1617 lastConnectedAt:
1618 typeof input.lastConnectedAt === 'number' && Number.isFinite(input.lastConnectedAt)
1619 ? input.lastConnectedAt
1620 : null
1621 }
1622 const others = cfg.remoteWorkspaceProfiles.filter((entry) => entry.id !== nextNormalized.id)
1623 const nextProfiles = [...others, nextNormalized].sort((a, b) => a.name.localeCompare(b.name))
1624 let nextCurrentProfileId = cfg.remoteWorkspaceProfileId
1625 if (remoteWorkspaceConfig) {
1626 if (
1627 profileMatchesConnection(nextNormalized, remoteWorkspaceConfig, currentVault?.root ?? null)
1628 ) {
1629 nextCurrentProfileId = nextNormalized.id
1630 } else if (cfg.remoteWorkspaceProfileId === nextNormalized.id) {
1631 nextCurrentProfileId = null
1632 }
1633 }
1634 currentRemoteWorkspaceProfileId = nextCurrentProfileId
1635 return {
1636 ...cfg,
1637 remoteWorkspaceProfiles: nextProfiles,
1638 remoteWorkspaceProfileId: nextCurrentProfileId
1639 }
1640 })
1641 if (input.clearAuthToken) {
1642 await deleteRemoteWorkspaceSecret(normalizedId)
1643 } else if (typeof input.authToken === 'string' && input.authToken.trim()) {
1644 await setRemoteWorkspaceSecret(normalizedId, input.authToken.trim())
1645 }
1646 const cfg = await loadConfig()
1647 const normalized = findRemoteProfileById(cfg.remoteWorkspaceProfiles, normalizedId)
1648 if (!normalized) {
1649 throw new Error('Remote workspace profile could not be saved.')

Callers 2

registerIpcFunction · 0.70
SettingsModalFunction · 0.50

Calls 9

updateConfigFunction · 0.90
setRemoteWorkspaceSecretFunction · 0.90
loadConfigFunction · 0.90
getRemoteWorkspaceSecretFunction · 0.90
normalizeRemoteBaseUrlFunction · 0.85
profileMatchesConnectionFunction · 0.85
findRemoteProfileByIdFunction · 0.85

Tested by

no test coverage detected