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

Sahil shubham wd 1351 bug purge stores properly on logout #84

Merged
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
55 changes: 39 additions & 16 deletions apps/extension/src/Components/Dibba/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { LinkCapture, parseSnippet, QuickLinkType, Snippet } from '@mexit/core'
import {
defaultContent,
LinkCapture,
mog,
parseBlock,
parseNode,
parseSnippet,
QuickLinkType,
SEPARATOR,
Snippet
} from '@mexit/core'
import React, { useEffect, useRef, useState } from 'react'
import { Icon } from '@iconify/react'
import fuzzysort from 'fuzzysort'
Expand All @@ -12,6 +22,8 @@ import { ActionTitle, ComboboxShortcuts, ComboSeperator, DisplayShortcut, Shortc
import { ElementTypeBasedShortcut } from '../../Editor/components/ComboBox'
import EditorPreviewRenderer from '../EditorPreviewRenderer'
import usePointerMovedSinceMount from '../../Hooks/usePointerMovedSinceMount'
import useDataStore from '../../Stores/useDataStore'
import { useContentStore } from '../../Stores/useContentStore'

// This functions provides the 'to be' range and text content
// Needed because keydown event happens before there is a selection or content change
Expand Down Expand Up @@ -42,18 +54,29 @@ export default function Dibba() {
const left = window.scrollX + dibbaState.coordinates.left
const [offsetTop, setOffsetTop] = useState(window.innerHeight < top + dibbaRef.current?.clientHeight)

const linkCaptures = useShortenerStore((store) => store.linkCaptures)
const linkCaptures = []
const ilinks = useDataStore((state) => state.ilinks).filter(
(item) => item.path.split(SEPARATOR)[0] === 'Links' && item.path.split(SEPARATOR).length > 1
)

const getContent = useContentStore((store) => store.getContent)
ilinks.forEach((item) => {
const _content = getContent(item.nodeid)

linkCaptures.push({
id: item.nodeid,
title: item.path.split(SEPARATOR).slice(-1)[0],
icon: 'ri:link',
content: _content?.content || defaultContent.content,
type: 'Links'
})
})

const snippets = useSnippets().getSnippets()
const pointerMoved = usePointerMovedSinceMount()

const data = [
// TODO: fix link captures after discussion
...linkCaptures.map((item) => ({
id: item.shortenedURL,
title: item.short,
icon: 'ri:link',
content: item.shortenedURL
})),
...linkCaptures,
...snippets.map((item) => ({
type: QuickLinkType.snippet,
icon: item?.icon || 'ri:quill-pen-line',
Expand All @@ -70,11 +93,12 @@ export default function Dibba() {
}

const insertLink = (item: any) => {
const link = document.createElement('a')
link.appendChild(document.createTextNode(item.title))
link.href = item.content
// const link = document.createElement('a')
// link.appendChild(document.createTextNode(item.title))
// link.href = item.content

dibbaState.extra.range.insertNode(link)
// dibbaState.extra.range.insertNode(link)
dibbaState.extra.range.insertNode(document.createTextNode(parseBlock(item.content)))
dibbaState.extra.range.collapse(false)

// Combining the inserted text node into one
Expand All @@ -97,7 +121,7 @@ export default function Dibba() {

if (item.type === QuickLinkType.snippet) {
insertSnippet(item as Snippet)
} else if (item.icon === 'ri:link') {
} else if (item.type === 'Links') {
// TODO: transform again to type linkCapture
insertLink(item)
}
Expand All @@ -114,8 +138,7 @@ export default function Dibba() {
res.push({
id: 'no-results',
title: 'No Results Found',
icon: 'ri:alert-line',
content: ''
icon: 'ri:alert-line'
})
}

Expand Down
6 changes: 6 additions & 0 deletions apps/extension/src/Editor/components/ComboBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export const ElementTypeBasedShortcut: Record<string, Record<string, Shortcut>>
...spotlightShortcuts.open,
title: 'to Insert'
}
},
['Links']: {
link: {
...spotlightShortcuts.open,
title: 'to Insert'
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions apps/webapp/src/Actions/Components/Shortener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export const Shortener = () => {
{
type: 'shortener',
status: response.status,
message: response.message || { ...reqBody, shortenedURL: response.data.shortenedURL }
message: response.message || { ...reqBody, shortenedURL: response.data.message }
},
'*'
)
copyToClipboard(response.data.shortenedURL)
copyToClipboard(response.data.message)
}

const handleEvent = (event: MessageEvent) => {
Expand Down
26 changes: 24 additions & 2 deletions apps/webapp/src/Stores/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { useContentStore } from './useContentStore'
import { useDataStore } from './useDataStore'
import { useSnippetStore } from './useSnippetStore'
import { useLayoutStore } from './useLayoutStore'
import { useApiStore } from './useApiStore'
import { usePublicNodeStore } from './usePublicNodes'
import { useRecentsStore } from './useRecentsStore'
import { useReminderStore } from './useReminderStore'
import { useTodoStore } from './useTodoStore'

export const useAuthStore = create<AuthStoreState>(persist(authStoreConstructor, { name: 'mexit-authstore' }))

Expand All @@ -29,6 +34,13 @@ export const useAuthentication = () => {
const setShowLoader = useLayoutStore((store) => store.setShowLoader)
const api = useApi()

const clearRequests = useApiStore().clearRequests
const resetDataStore = useDataStore().resetDataStore
const resetPublicNodes = usePublicNodeStore().reset
const clearRecents = useRecentsStore().clear
const clearReminders = useReminderStore().clearReminders
const clearTodos = useTodoStore().clearTodos

const login = async (
email: string,
password: string,
Expand Down Expand Up @@ -158,8 +170,18 @@ export const useAuthentication = () => {
const logout = async () => {
await signOut()
setUnAuthenticated()
localStorage.clear()
await IDBClear()

// Reseting all persisted stores explicitly because just clearing out local storage and indexed db doesn't work
// This is because zustand maintains it's state post logout as we don't go through a reload
// Which results in zustand recreating everything post logout
clearRequests()
initContents({})
resetDataStore()
resetPublicNodes()
clearRecents()
clearReminders()
initSnippets([])
clearTodos()
}

const registerDetails = (data: RegisterFormData): Promise<string> => {
Expand Down
6 changes: 6 additions & 0 deletions apps/webapp/src/Stores/usePublicNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PublicNodeStoreType {
nodes: Record<string, PublicNode>
addPublicNode: (node: PublicNode) => void
getPublicNode: (nodeID: string) => PublicNode
reset: () => void
}

export const usePublicNodeStore = create<PublicNodeStoreType>(
Expand All @@ -25,6 +26,11 @@ export const usePublicNodeStore = create<PublicNodeStoreType>(
},
getPublicNode: (nodeID: string) => {
return get().nodes[nodeID]
},
reset: () => {
set({
nodes: {}
})
}
}),
{ name: 'mexit-public-node-store', getStorage: () => IDBStorage }
Expand Down
3 changes: 3 additions & 0 deletions libs/core/src/Types/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export interface DataStoreState {

initializeDataStore: (initData: InitDataStoreType) => void

// Just to reset everything to initial data
resetDataStore: () => void

// adds the node
addILink: (props: AddILinkProps) => ILink | undefined

Expand Down
15 changes: 15 additions & 0 deletions libs/shared/src/Stores/dataStoreConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ export const dataStoreConstructor = (set, get) => ({
...initData
})
},
resetDataStore: () => {
set({
tags: [],
ilinks: [],
linkCache: {},
tagsCache: {},
bookmarks: [],
archive: [],
baseNodeId: '@',
slashCommands: {
default: defaultCommands,
internal: []
}
})
},

addTag: (tag) => {
const Tags = Settify([...get().tags.map((t) => t.value), tag])
Expand Down