write-and-cite-settings-migration.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import customLocalStorage from '@/infrastructure/local-storage'
  2. import getMeta from '@/utils/meta'
  3. import {
  4. RefProviderSettings,
  5. UserSettings,
  6. } from '../../../../types/user-settings'
  7. type RefProvider = 'mendeley' | 'zotero' | 'papers'
  8. const providers: RefProvider[] = ['mendeley', 'zotero', 'papers']
  9. const buildLegacyKey = (userId: string, provider: RefProvider, key: string) =>
  10. `user.${userId}.write-and-cite.${provider}.${key}`
  11. const isBoolean = (value: unknown): value is boolean =>
  12. typeof value === 'boolean'
  13. const parseGroups = (value: unknown): { id: string }[] | undefined => {
  14. return Array.isArray(value)
  15. ? (value as { id: string | number }[]).map(group => ({
  16. id: typeof group.id === 'number' ? String(group.id) : group.id,
  17. }))
  18. : undefined
  19. }
  20. export type LegacyWriteAndCiteMigration = {
  21. patch: Partial<Pick<UserSettings, 'mendeley' | 'zotero' | 'papers'>>
  22. keysToRemove: string[]
  23. }
  24. export const getLegacyWriteAndCiteMigration = (
  25. userSettings: UserSettings
  26. ): LegacyWriteAndCiteMigration => {
  27. const userId = getMeta('ol-user_id')
  28. if (!userId) {
  29. return {
  30. patch: {},
  31. keysToRemove: [],
  32. }
  33. }
  34. const patch: LegacyWriteAndCiteMigration['patch'] = {}
  35. const keysToRemove: string[] = []
  36. for (const provider of providers) {
  37. const currentProviderSettings = userSettings[provider]
  38. if (currentProviderSettings.migrated) {
  39. continue
  40. }
  41. const enabledKey = buildLegacyKey(userId, provider, 'enabled')
  42. const groupsKey = buildLegacyKey(userId, provider, 'groups')
  43. const disablePersonalLibraryKey = buildLegacyKey(
  44. userId,
  45. provider,
  46. 'disablePersonalLibrary'
  47. )
  48. const enabledValue = customLocalStorage.getItem(enabledKey)
  49. const groupsValue = customLocalStorage.getItem(groupsKey)
  50. const disablePersonalLibraryValue = customLocalStorage.getItem(
  51. disablePersonalLibraryKey
  52. )
  53. // Storage.getItem returns null if the key does not exist
  54. const hasEnabledValue = enabledValue !== null
  55. const hasGroupsValue = groupsValue !== null
  56. const hasDisablePersonalLibraryValue = disablePersonalLibraryValue !== null
  57. if (
  58. !hasEnabledValue &&
  59. !hasGroupsValue &&
  60. !hasDisablePersonalLibraryValue
  61. ) {
  62. continue
  63. }
  64. const nextProviderSettings: RefProviderSettings = {
  65. enabled: isBoolean(enabledValue)
  66. ? enabledValue
  67. : currentProviderSettings.enabled,
  68. groups: parseGroups(groupsValue) ?? currentProviderSettings.groups,
  69. disablePersonalLibrary: isBoolean(disablePersonalLibraryValue)
  70. ? disablePersonalLibraryValue
  71. : currentProviderSettings.disablePersonalLibrary,
  72. migrated: true,
  73. }
  74. patch[provider] = nextProviderSettings
  75. keysToRemove.push(enabledKey, groupsKey, disablePersonalLibraryKey)
  76. }
  77. return {
  78. patch,
  79. keysToRemove: [],
  80. }
  81. }