Type user store

This commit is contained in:
Adria Navarro 2024-12-24 13:54:16 +01:00
parent f5ebc6b249
commit ad21b2e1fd
4 changed files with 38 additions and 9 deletions

View File

@ -61,7 +61,8 @@ export type Store = BaseStore &
ViewV2.Store &
NonPlus.Store &
Datasource.Store &
Validation.Store & {
Validation.Store &
Users.Store & {
// TODO while typing the rest of stores
fetch: Writable<any>
filter: Writable<any>

View File

@ -1,8 +1,29 @@
import { writable, get, derived } from "svelte/store"
import { writable, get, derived, Writable, Readable } from "svelte/store"
import { helpers } from "@budibase/shared-core"
import { Store as StoreContext } from "."
import { UIUser } from "@budibase/types"
export const createStores = () => {
const users = writable([])
interface UsersStore {
users: Writable<UIUser[]>
}
interface DerivedUsersStore {
userCellMap: Readable<Record<string, UIUser>>
}
interface ActionUserStore {
users: UsersStore["users"] & {
actions: {
updateUser: (user: UIUser) => void
removeUser: (sessionId: string) => void
}
}
}
export type Store = UsersStore & DerivedUsersStore
export const createStores = (): UsersStore => {
const users = writable<UIUser[]>([])
const enrichedUsers = derived(users, $users => {
return $users.map(user => ({
@ -20,7 +41,7 @@ export const createStores = () => {
}
}
export const deriveStores = context => {
export const deriveStores = (context: StoreContext): DerivedUsersStore => {
const { users, focusedCellId } = context
// Generate a lookup map of cell ID to the user that has it selected, to make
@ -28,7 +49,7 @@ export const deriveStores = context => {
const userCellMap = derived(
[users, focusedCellId],
([$users, $focusedCellId]) => {
let map = {}
let map: Record<string, UIUser> = {}
$users.forEach(user => {
const cellId = user.gridMetadata?.focusedCellId
if (cellId && cellId !== $focusedCellId) {
@ -44,10 +65,10 @@ export const deriveStores = context => {
}
}
export const createActions = context => {
export const createActions = (context: StoreContext): ActionUserStore => {
const { users } = context
const updateUser = user => {
const updateUser = (user: UIUser) => {
const $users = get(users)
if (!$users.some(x => x.sessionId === user.sessionId)) {
users.set([...$users, user])
@ -60,7 +81,7 @@ export const createActions = context => {
}
}
const removeUser = sessionId => {
const removeUser = (sessionId: string) => {
users.update(state => {
return state.filter(x => x.sessionId !== sessionId)
})

View File

@ -2,3 +2,4 @@ export * from "./columns"
export * from "./datasource"
export * from "./table"
export * from "./view"
export * from "./user"

View File

@ -0,0 +1,6 @@
import { User } from "@budibase/types"
export interface UIUser extends User {
sessionId: string
gridMetadata?: { focusedCellId?: string }
}