-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Front] Fix/multi wallet local storage issue (#333)
- Loading branch information
Showing
3 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 61 additions & 11 deletions
72
packages/demo/usecase-demo/src/utils/localStorageContentMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,87 @@ | ||
import { Address } from 'wagmi'; | ||
import { LOCAL_STORAGE_PREFIX } from '@/utils/localStorage.ts'; | ||
|
||
type TaskData = { | ||
wallet_id: Address; | ||
protected_data_address: string; | ||
completed_task_id: string; | ||
}; | ||
|
||
const STORED_DATA_KEY = `${LOCAL_STORAGE_PREFIX}_savedTaskId`; | ||
|
||
/** | ||
* localStorage cache for completed iExec tasks. | ||
* ie. Content is available on IPFS *but* the key needs not to have been re-generated in the meantime! | ||
*/ | ||
|
||
function getCompletedTasksCache(): TaskData[] { | ||
const tasksCache = localStorage.getItem(STORED_DATA_KEY); | ||
try { | ||
return tasksCache ? JSON.parse(tasksCache) : []; | ||
} catch (err) { | ||
console.error('[getCompletedTasksCache] ERROR', err); | ||
localStorage.removeItem(STORED_DATA_KEY); | ||
return []; | ||
} | ||
} | ||
|
||
export function saveCompletedTaskId({ | ||
walletId, | ||
protectedDataAddress, | ||
completedTaskId, | ||
}: { | ||
walletId: Address; | ||
protectedDataAddress: string; | ||
completedTaskId: string; | ||
}) { | ||
localStorage.setItem( | ||
`${LOCAL_STORAGE_PREFIX}_${protectedDataAddress}`, | ||
completedTaskId | ||
const tasks = getCompletedTasksCache(); | ||
|
||
const newCompletedTask: TaskData = { | ||
wallet_id: walletId, | ||
protected_data_address: protectedDataAddress, | ||
completed_task_id: completedTaskId, | ||
}; | ||
|
||
const existingTaskIndex = tasks.findIndex( | ||
(task) => | ||
task.wallet_id === walletId && | ||
task.protected_data_address === protectedDataAddress | ||
); | ||
|
||
if (existingTaskIndex > -1) { | ||
tasks[existingTaskIndex] = newCompletedTask; | ||
} else { | ||
tasks.push(newCompletedTask); | ||
} | ||
|
||
localStorage.setItem(STORED_DATA_KEY, JSON.stringify(tasks)); | ||
} | ||
|
||
export function getCompletedTaskId({ | ||
walletId, | ||
protectedDataAddress, | ||
}: { | ||
walletId: Address; | ||
protectedDataAddress: string; | ||
}) { | ||
return localStorage.getItem( | ||
`${LOCAL_STORAGE_PREFIX}_${protectedDataAddress}` | ||
}): string | null { | ||
const tasks = getCompletedTasksCache(); | ||
|
||
const existingTask = tasks.find( | ||
(task) => | ||
task.wallet_id === walletId && | ||
task.protected_data_address === protectedDataAddress | ||
); | ||
|
||
if (!existingTask || !existingTask.completed_task_id) { | ||
return null; | ||
} | ||
|
||
return existingTask.completed_task_id; | ||
} | ||
|
||
/** | ||
* Reset the cache of completed task IDs in localStorage. | ||
*/ | ||
export function resetCompletedTaskIdsCache() { | ||
for (const key in localStorage) { | ||
if (key.startsWith(`${LOCAL_STORAGE_PREFIX}_0x`)) { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
localStorage.removeItem(STORED_DATA_KEY); | ||
} |