-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): store current view id in local-storage (#8838)
- Loading branch information
Showing
4 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
12 changes: 9 additions & 3 deletions
12
packages/affine/data-view/src/widget-presets/views-bar/index.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,5 +1,11 @@ | ||
import { createUniComponentFromWebComponent } from '../../core/index.js'; | ||
import { | ||
createUniComponentFromWebComponent, | ||
type DataViewWidgetProps, | ||
} from '../../core/index.js'; | ||
import { DataViewHeaderViews } from './views-view.js'; | ||
|
||
export const widgetViewsBar = | ||
createUniComponentFromWebComponent(DataViewHeaderViews); | ||
export const widgetViewsBar = createUniComponentFromWebComponent< | ||
DataViewWidgetProps & { | ||
onChangeView?: (viewId: string) => void; | ||
} | ||
>(DataViewHeaderViews); |
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { z } from 'zod'; | ||
|
||
const currentViewListSchema = z.array( | ||
z.object({ | ||
blockId: z.string(), | ||
viewId: z.string(), | ||
}) | ||
); | ||
const maxLength = 20; | ||
const currentViewListKey = 'blocksuite:databaseBlock:view:currentViewList'; | ||
const createCurrentViewStorage = () => { | ||
const getList = () => { | ||
const string = localStorage.getItem(currentViewListKey); | ||
if (!string) { | ||
return; | ||
} | ||
try { | ||
const result = currentViewListSchema.safeParse(JSON.parse(string)); | ||
if (result.success) { | ||
return result.data; | ||
} | ||
} catch (_) { | ||
// do nothing | ||
} | ||
return; | ||
}; | ||
const saveList = () => { | ||
localStorage.setItem(currentViewListKey, JSON.stringify(list)); | ||
}; | ||
|
||
const list = getList() ?? []; | ||
|
||
return { | ||
getCurrentView: (blockId: string) => { | ||
return list.find(item => item.blockId === blockId)?.viewId; | ||
}, | ||
setCurrentView: (blockId: string, viewId: string) => { | ||
const configIndex = list.findIndex(item => item.blockId === blockId); | ||
if (configIndex >= 0) { | ||
list.splice(configIndex, 1); | ||
} | ||
if (list.length >= maxLength) { | ||
list.pop(); | ||
} | ||
list.unshift({ blockId, viewId }); | ||
saveList(); | ||
}, | ||
}; | ||
}; | ||
|
||
export const currentViewStorage = createCurrentViewStorage(); |