Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRD-599] Reset local data #9

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"folder": "/",
"index": "index.html",
"public": false
},
"/reset": {
"folder": "/reset",
"index": "index.html",
"public": true
}
},
"intents": [
Expand All @@ -27,14 +32,16 @@
],
"permissions": {
"apps": {
"description": "Required by the cozy-bar to display the icons of the apps",
"description": "Required to search in apps",
"type": "io.cozy.apps",
"verbs": ["GET"]
},
"files": {
"description": "Required to search in files",
"type": "io.cozy.files"
},
"contacts": {
"description": "Required to search in contacts",
"type": "io.cozy.contacts"
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"dependencies": {
"comlink": "^4.4.1",
"cozy-app-publish": "^0.34.0",
"cozy-client": "^49.8.0",
"cozy-client": "^50.1.0",
"cozy-device-helper": "^3.1.0",
"cozy-flags": "^4.0.0",
"cozy-logger": "^1.10.4",
"cozy-minilog": "^3.3.1",
"cozy-pouch-link": "^49.8.0",
"cozy-pouch-link": "^50.1.0",
"cozy-realtime": "^5.0.2",
"cozy-tsconfig": "^1.2.0",
"flexsearch": "^0.7.43",
Expand Down
14 changes: 11 additions & 3 deletions rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default defineConfig({
output: {
cleanDistPath: true,
distPath: {
root: 'build'
}
root: 'build',
},
},
html: {
template: './src/targets/browser/index.ejs',
Expand Down Expand Up @@ -49,7 +49,15 @@ export default defineConfig({
},
{
from: 'icon.svg'
}
},
{
from: 'src/targets/public/index.html',
to: 'reset/index.html'
},
{
from: 'src/targets/public/reset.js',
to: 'reset/reset.js'
},
],
})
]
Expand Down
2 changes: 1 addition & 1 deletion src/dataproxy/common/DataProxyInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type { SearchIndexes } from '@/search/types'

export interface DataProxyWorker {
search: (query: string) => unknown
setClient: (clientData: ClientData) => Promise<void>
setup: (clientData: ClientData) => Promise<void>
}

export interface DataProxyWorkerPartialState {
Expand Down
7 changes: 6 additions & 1 deletion src/dataproxy/worker/SharedWorkerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DataProxyWorkerPartialState
} from '@/dataproxy/common/DataProxyInterface'
import { TabCountSync } from '@/dataproxy/common/TabCountSync'
import { removeStaleLocalData } from '@/dataproxy/worker/utils'

const log = Minilog('👷‍♂️ [SharedWorkerProvider]')

Expand Down Expand Up @@ -44,7 +45,11 @@ export const SharedWorkerProvider = React.memo(
if (!client) return

const doAsync = async (): Promise<void> => {
// Cleanup any remaining local data
await removeStaleLocalData()

log.debug('Init SharedWorker')

const workerInst = new SharedWorker(
new URL('./shared-worker.ts', import.meta.url),
{
Expand All @@ -57,7 +62,7 @@ export const SharedWorkerProvider = React.memo(
log.debug('Provide CozyClient data to SharedWorker')
const { uri, token } = client.getStackClient()

obj.setClient({
obj.setup({
uri,
token: token.token,
instanceOptions: client.instanceOptions,
Expand Down
16 changes: 16 additions & 0 deletions src/dataproxy/worker/platformWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ const storage = {
resolve()
}

request.onerror = (): void => {
reject(request.error)
}
})
},
destroy: async (): Promise<void> => {
return new Promise((resolve, reject) => {
if (db) {
db.close()
db = null
}
const request = indexedDB.deleteDatabase(dbName)

request.onsuccess = (): void => {
resolve()
}
request.onerror = (): void => {
reject(request.error)
}
Expand Down
6 changes: 3 additions & 3 deletions src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ let searchEngine: SearchEngine
const broadcastChannel = new BroadcastChannel('DATA_PROXY_BROADCAST_CHANANEL')

const dataProxy: DataProxyWorker = {
// FIXME: change setClient name
setClient: async (clientData: ClientData) => {
log.debug('Received data for setting client')
setup: async (clientData: ClientData) => {
log.debug('Received data for setting up client')
if (client) return
updateState()

Expand Down Expand Up @@ -63,6 +62,7 @@ const dataProxy: DataProxyWorker = {

searchEngine = new SearchEngine(client)

log.debug('Setup done')
updateState()
},

Expand Down
26 changes: 26 additions & 0 deletions src/dataproxy/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Minilog from 'cozy-minilog'

import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/search/consts'
const log = Minilog('👷‍♂️ [Worker utils]')

const deleteDatabases = async (): Promise<void> => {
const databases = await window.indexedDB.databases()
// Remove all indexedDB databases
for (const db of databases) {
if (db.name) {
window.indexedDB.deleteDatabase(db.name)
log.info('Deleted indexedDB database : ', db.name)
}
}
}

export const removeStaleLocalData = async (): Promise<void> => {
// Check flag existence proving the reset process was incomplete
const hasStaleData = localStorage.getItem(LOCALSTORAGE_KEY_DELETING_DATA)
if (hasStaleData) {
log.info('Found stale data: remove it')
await deleteDatabases()
localStorage.removeItem(LOCALSTORAGE_KEY_DELETING_DATA)
}
return
}
2 changes: 2 additions & 0 deletions src/search/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ export const DOCTYPE_ORDER = {
[CONTACTS_DOCTYPE]: 1,
[FILES_DOCTYPE]: 2
}

export const LOCALSTORAGE_KEY_DELETING_DATA = 'deletingLocalData'
1 change: 0 additions & 1 deletion src/targets/browser/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/manifest.json" crossOrigin="use-credentials">
<meta name="color-scheme" content="light dark" />
<meta name="theme-color" content="#ffffff">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
6 changes: 6 additions & 0 deletions src/targets/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<script src="reset/reset.js">
</script>
</head>
</html>
22 changes: 22 additions & 0 deletions src/targets/public/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */

function resetData() {
// Remove localstorage
window.localStorage.clear()
console.log('Deleted localStorage')

// Add this flag for future check
window.localStorage.setItem('deletingLocalData', new Date().toISOString())

// Remove all indexedDB databases
window.indexedDB.databases().then(function (databases) {
databases.forEach(function (db) {
window.indexedDB.deleteDatabase(db.name)
console.log('Deleted indexedDB database : ', db.name)
})
// Everything is done, remove the flag
window.localStorage.removeItem('deletingLocalData')
})
}

resetData()
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2248,16 +2248,16 @@ cozy-app-publish@^0.34.0:
tar "^6.1.11"
verror "^1.10.1"

cozy-client@^49.8.0:
version "49.8.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-49.8.0.tgz#d4f6b3a2fb26dc6bfa561046b075eb6e221af73e"
integrity sha512-peb2n+buOihzqqNN3/zULmytQI8wOtQZsIqLWEaw2WfNXha1T23wNBrMuE7SaMOof361f011MrM5+WVYhKj+VA==
cozy-client@^50.1.0:
version "50.1.0"
resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-50.1.0.tgz#f21be140f97dbece22c6ffde3f2dc2a0104fe8ec"
integrity sha512-1RJ5nJGxZy4MrJ6ZOiyiYCLDHScqKtJ8Aja89M28zvzgg5e0HUyEjTQEgnh9f0cpfKuiUOQtLmoMCtd2OWIOXA==
dependencies:
"@cozy/minilog" "1.0.0"
"@types/jest" "^26.0.20"
"@types/lodash" "^4.14.170"
btoa "^1.2.1"
cozy-stack-client "^49.8.0"
cozy-stack-client "^50.1.0"
date-fns "2.29.3"
json-stable-stringify "^1.0.1"
lodash "^4.17.13"
Expand Down Expand Up @@ -2302,12 +2302,12 @@ cozy-minilog@^3.3.1:
dependencies:
microee "0.0.6"

cozy-pouch-link@^49.8.0:
version "49.8.0"
resolved "https://registry.yarnpkg.com/cozy-pouch-link/-/cozy-pouch-link-49.8.0.tgz#a5361bbc0ed23b9bc4d804d31869c615fdf1d13e"
integrity sha512-c5+dWx5BNi37JuLVpil9SB91vy3EtB7B8Qa6Krx+znzPYcRoK3Bqjuns5c7hXG5rD8MV2qls1yvxQWUrfy82kA==
cozy-pouch-link@^50.1.0:
version "50.1.0"
resolved "https://registry.yarnpkg.com/cozy-pouch-link/-/cozy-pouch-link-50.1.0.tgz#b9aa635b9347a9d0a30c2b803d57b67f066bbdeb"
integrity sha512-M3/wbX85ccM1qzUdOnRkLdhQ7XTmv3K1cYBhOx6DN/dSzhADkmDl4OeqmhJJmV3GyncGZVEnjOWkPi1J/Wc7gg==
dependencies:
cozy-client "^49.8.0"
cozy-client "^50.1.0"
pouchdb-browser "^7.2.2"
pouchdb-find "^7.2.2"

Expand All @@ -2319,10 +2319,10 @@ cozy-realtime@^5.0.2:
"@cozy/minilog" "^1.0.0"
cozy-device-helper "^3.1.0"

cozy-stack-client@^49.8.0:
version "49.8.0"
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-49.8.0.tgz#c57dfefe50e47f228fee7e1921c438d35f4e0877"
integrity sha512-sYJL2o+DsNs7V5eQghXpWKcMzxc39QAKtM8zAdmWl2MMCyiqO3lBehRomhstcJHtuZrMLXXPQPr1A0ONBlMmZg==
cozy-stack-client@^50.1.0:
version "50.1.0"
resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-50.1.0.tgz#ec48a55732efc87956739f63a211719c3dd87b32"
integrity sha512-WHkNznIiUJa6nKlRgbhDiypMLTRv+DCEfcz9Kp6bRA5VlSc8Lk0igNjclS+bglrXBz7cTUF4Q0QSCQlWcFEpiQ==
dependencies:
detect-node "^2.0.4"
mime "^2.4.0"
Expand Down