Skip to content

Commit

Permalink
feat: Allow to reset local PouchDB using UniversalLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ldoppea committed Sep 24, 2024
1 parent dd41346 commit f57aba4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import 'cozy-client'
import { QueryDefinition } from 'cozy-client'
import { QueryDefinition, CozyLink } from 'cozy-client'
import {
FileDocument,
CozyClientDocument,
Expand Down Expand Up @@ -168,6 +168,7 @@ declare module 'cozy-client' {
queryDefinition: QueryDefinition,
options?: QueryOptions
) => Promise<QueryResult>
links: CozyLink[]
}

export const createMockClient = (options?: ClientOptions): CozyClient =>
Expand Down
16 changes: 16 additions & 0 deletions src/pouchdb/deeplinkHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import CozyClient from 'cozy-client'

import strings from '/constants/strings.json'
import { resetLinksAndRestart } from '/pouchdb/getLinks'
import { sendDbByEmail } from '/pouchdb/sendDbByEmail'

export const handleDbDeepLink = (url: string, client?: CozyClient): boolean => {
Expand All @@ -10,6 +11,12 @@ export const handleDbDeepLink = (url: string, client?: CozyClient): boolean => {
return true
}

if (isResetDbDeepLink(url)) {
void resetLinksAndRestart(client)

return true
}

return false
}

Expand All @@ -21,3 +28,12 @@ const isSendDbDeepLink = (url: string): boolean => {

return deepLinks.includes(url.toLowerCase())
}

const isResetDbDeepLink = (url: string): boolean => {
const deepLinks = [
`${strings.COZY_SCHEME}resetdb`,
`${strings.UNIVERSAL_LINK_BASE}/resetdb`
]

return deepLinks.includes(url.toLowerCase())
}
23 changes: 22 additions & 1 deletion src/pouchdb/getLinks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import RNRestart from 'react-native-restart'

import { platformReactNative } from '/pouchdb/platformReactNative'

import { CozyLink, StackLink } from 'cozy-client'
import CozyClient, { CozyLink, StackLink } from 'cozy-client'
import Minilog from 'cozy-minilog'
import { default as PouchLink } from 'cozy-pouch-link'

const log = Minilog('🔗 GetLinks')

export const offlineDoctypes = [
// cozy-home
'io.cozy.accounts',
Expand Down Expand Up @@ -53,3 +58,19 @@ export const getLinks = (): CozyLink[] => {

return [stackLink, pouchLink]
}

export const resetLinksAndRestart = async (
client?: CozyClient
): Promise<void> => {
if (!client) {
log.info('ResetLinksAndRestart called with no client, return')
return
}

for (const link of client.links) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await link.reset()
}

RNRestart.Restart()
}

0 comments on commit f57aba4

Please sign in to comment.