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

Typing remaining grid stores #15266

Merged
merged 18 commits into from
Dec 31, 2024
Merged
2 changes: 1 addition & 1 deletion packages/bbui/src/ProgressCircle/ProgressCircle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
}

export let overBackground
export let overBackground = false
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
Expand Down
7 changes: 7 additions & 0 deletions packages/client/svelte.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"

const config = {
preprocess: vitePreprocess(),
}

export default config
11 changes: 5 additions & 6 deletions packages/frontend-core/src/components/grid/layout/Grid.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script lang="ts">
import { setContext, onMount } from "svelte"
import { writable, derived } from "svelte/store"
import { fade } from "svelte/transition"
Expand Down Expand Up @@ -53,17 +53,16 @@
const gridID = `grid-${Math.random().toString().slice(2)}`

// Store props in a store for reference in other stores
const props = writable($$props)
const props: any = writable($$props)

// Build up context
let context = {
let context = attachStores({
API: API || createAPIClient(),
Constants,
gridID,
props,
}
context = { ...context, ...createEventManagers() }
context = attachStores(context)
...createEventManagers(),
})

// Reference some stores for local use
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { createEventDispatcher } from "svelte"

export const createEventManagers = () => {
const svelteDispatch = createEventDispatcher()
let subscribers = {}
let subscribers: Record<string, ((...params: any) => void)[]> = {}

// Dispatches an event, notifying subscribers and also emitting a normal
// svelte event
const dispatch = (event, payload) => {
const dispatch = (event: string, payload: any) => {
svelteDispatch(event, payload)
const subs = subscribers[event] || []
for (let i = 0; i < subs.length; i++) {
Expand All @@ -15,7 +15,7 @@ export const createEventManagers = () => {
}

// Subscribes to events
const subscribe = (event, callback) => {
const subscribe = (event: string, callback: () => void) => {
const subs = subscribers[event] || []
subscribers[event] = [...subs, callback]

Expand Down
16 changes: 0 additions & 16 deletions packages/frontend-core/src/components/grid/stores/bounds.js

This file was deleted.

29 changes: 29 additions & 0 deletions packages/frontend-core/src/components/grid/stores/bounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { derived, Readable, Writable, writable } from "svelte/store"

interface BoundsStore {
bounds: Writable<{
left: number
top: number
width: number
height: number
}>
height: Readable<number>
width: Readable<number>
}

export type Store = BoundsStore

export const createStores = (): BoundsStore => {
const bounds = writable({
left: 0,
top: 0,
width: 0,
height: 0,
})

// Derive height and width as primitives to avoid wasted computation
const width = derived(bounds, $bounds => $bounds.width, 0)
const height = derived(bounds, $bounds => $bounds.height, 0)

return { bounds, height, width }
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
export const createActions = context => {
import { FindTableResponse } from "@budibase/types"
import { Store as StoreContext } from "."

interface CacheActionStore {
cache: {
actions: {
getPrimaryDisplayForTableId: (tableId: string) => Promise<string>
getTable: (tableId: string) => Promise<FindTableResponse>
resetCache: () => any
}
}
}

export type Store = CacheActionStore

export const createActions = (context: StoreContext): CacheActionStore => {
const { API } = context

// Cache for the primary display columns of different tables.
// If we ever need to cache table definitions for other purposes then we can
// expand this to be a more generic cache.
let tableCache = {}
let tableCache: Record<string, Promise<FindTableResponse>> = {}

const resetCache = () => {
tableCache = {}
}

const fetchTable = async tableId => {
const fetchTable = async (tableId: string) => {
// If we've never encountered this tableId before then store a promise that
// resolves to the primary display so that subsequent invocations before the
// promise completes can reuse this promise
Expand All @@ -21,13 +36,13 @@ export const createActions = context => {
return await tableCache[tableId]
}

const getPrimaryDisplayForTableId = async tableId => {
const getPrimaryDisplayForTableId = async (tableId: string) => {
const table = await fetchTable(tableId)
const display = table?.primaryDisplay || table?.schema?.[0]?.name
return display
}

const getTable = async tableId => {
const getTable = async (tableId: string) => {
const table = await fetchTable(tableId)
return table
}
Expand All @@ -43,7 +58,7 @@ export const createActions = context => {
}
}

export const initialise = context => {
export const initialise = (context: StoreContext) => {
const { datasource, cache } = context

// Wipe the caches whenever the datasource changes to ensure we aren't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const initialise = (context: StoreContext) => {
}
$fetch?.update({
sortOrder: $sort.order || SortOrder.ASCENDING,
sortColumn: $sort.column,
sortColumn: $sort.column ?? undefined,
})
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const initialise = (context: StoreContext) => {
}
$fetch.update({
sortOrder: $sort.order || SortOrder.ASCENDING,
sortColumn: $sort.column,
sortColumn: $sort.column ?? undefined,
})
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const initialise = (context: StoreContext) => {
await datasource.actions.saveDefinition({
...$view,
sort: {
field: $sort.column,
field: $sort.column!,
order: $sort.order || SortOrder.ASCENDING,
},
})
Expand All @@ -187,7 +187,7 @@ export const initialise = (context: StoreContext) => {
}
$fetch.update({
sortOrder: $sort.order,
sortColumn: $sort.column,
sortColumn: $sort.column ?? undefined,
})
})
)
Expand Down
28 changes: 15 additions & 13 deletions packages/frontend-core/src/components/grid/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable, Writable } from "svelte/store"
import { Writable } from "svelte/store"
import type { APIClient } from "../../../api/types"

import * as Bounds from "./bounds"
Expand All @@ -25,6 +25,7 @@ import * as NonPlus from "./datasources/nonPlus"
import * as Cache from "./cache"
import * as Conditions from "./conditions"
import { SortOrder, UIDatasource, UISearchFilter } from "@budibase/types"
import * as Constants from "../lib/constants"

const DependencyOrderedStores = [
Sort,
Expand Down Expand Up @@ -73,12 +74,16 @@ export interface BaseStoreProps {
canEditColumns?: boolean
canExpandRows?: boolean
canSaveSchema?: boolean
minHeight?: number
}

export interface BaseStore {
API: APIClient
gridID: string
props: Writable<BaseStoreProps>
subscribe: any
dispatch: (event: string, data: any) => any
Constants: typeof Constants
}

export type Store = BaseStore &
Expand All @@ -93,22 +98,19 @@ export type Store = BaseStore &
Filter.Store &
UI.Store &
Clipboard.Store &
Scroll.Store & {
// TODO while typing the rest of stores
sort: Writable<any>
subscribe: any
dispatch: (event: string, data: any) => any
notifications: Writable<any>
width: Writable<number>
bounds: Readable<any>
height: Readable<number>
} & Rows.Store &
Scroll.Store &
Rows.Store &
Reorder.Store &
Resize.Store &
Config.Store &
Conditions.Store
Conditions.Store &
Cache.Store &
Viewport.Store &
Notifications.Store &
Sort.Store &
Bounds.Store

export const attachStores = (context: Store): Store => {
export const attachStores = (context: BaseStore): Store => {
// Atomic store creation
for (let store of DependencyOrderedStores) {
if ("createStores" in store) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { notifications as BBUINotifications } from "@budibase/bbui"
import { derived } from "svelte/store"
import { derived, Readable } from "svelte/store"
import { Store as StoreContext } from "."

export const createStores = context => {
interface NotificationStore {
notifications: Readable<{
success: (message: string) => void
error: (message: string) => void
}>
}

export type Store = NotificationStore

export const createStores = (context: StoreContext): NotificationStore => {
const { notifySuccess, notifyError } = context

// Normally we would not derive a store in "createStores" as it should be
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { derived } from "svelte/store"
import { Store as StoreContext } from "."

export const initialise = context => {
export const initialise = (context: StoreContext) => {
const { scrolledRowCount, rows, visualRowCapacity } = context

// Derive how many rows we have in total
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { derived, get } from "svelte/store"
import { derived, get, Writable } from "svelte/store"
import { memo } from "../../../utils"
import { SortOrder } from "@budibase/types"
import { Store as StoreContext } from "."

export const createStores = context => {
interface SortStore {
sort: Writable<{
column: string | null | undefined
order: SortOrder
}>
}

export type Store = SortStore

export const createStores = (context: StoreContext): SortStore => {
const { props } = context
const $props = get(props)

Expand All @@ -17,7 +27,7 @@ export const createStores = context => {
}
}

export const initialise = context => {
export const initialise = (context: StoreContext) => {
const { sort, initialSortColumn, initialSortOrder, schema } = context

// Reset sort when initial sort props change
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { derived } from "svelte/store"
import { derived, Readable } from "svelte/store"
import { MinColumnWidth } from "../lib/constants"
import { Store as StoreContext } from "."
import { Row } from "@budibase/types"

export const deriveStores = context => {
interface ViewportDerivedStore {
scrolledRowCount: Readable<number>
visualRowCapacity: Readable<number>
renderedRows: Readable<Row>
columnRenderMap: Readable<Record<string, true>>
}

export type Store = ViewportDerivedStore

export const deriveStores = (context: StoreContext): ViewportDerivedStore => {
const {
rowHeight,
scrollableColumns,
Expand Down Expand Up @@ -77,7 +88,7 @@ export const deriveStores = context => {
leftEdge += $scrollableColumns[endColIdx].width
endColIdx++
}
let next = {}
let next: Record<string, true> = {}
$scrollableColumns
.slice(Math.max(0, startColIdx), endColIdx)
.forEach(col => {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"server-destroy": "1.0.1",
"snowflake-sdk": "^1.15.0",
"socket.io": "4.8.1",
"svelte": "^4.2.10",
"svelte": "4.2.19",
"tar": "6.2.1",
"tmp": "0.2.3",
"to-json-schema": "0.2.5",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"@budibase/pro": ["./packages/pro/src"],
"@budibase/string-templates": ["./packages/string-templates/src"],
"@budibase/string-templates/*": ["./packages/string-templates/*"],
"@budibase/frontend-core": ["./packages/frontend-core/src"]
"@budibase/frontend-core": ["./packages/frontend-core/src"],
"@budibase/bbui": ["./packages/bbui/src"]
}
},
"exclude": []
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18991,7 +18991,7 @@ svelte-spa-router@^4.0.1:
dependencies:
regexparam "2.0.2"

svelte@4.2.19, svelte@^4.2.10:
svelte@4.2.19:
version "4.2.19"
resolved "https://registry.yarnpkg.com/svelte/-/svelte-4.2.19.tgz#4e6e84a8818e2cd04ae0255fcf395bc211e61d4c"
integrity sha512-IY1rnGr6izd10B0A8LqsBfmlT5OILVuZ7XsI0vdGPEvuonFV7NYEUK4dAkm9Zg2q0Um92kYjTpS1CAP3Nh/KWw==
Expand Down
Loading