Add default sample data screen when creating a new app

This commit is contained in:
Andrew Kingston 2025-02-26 12:00:00 +00:00
parent 99f47c27ed
commit 6dd9c19d02
No known key found for this signature in database
2 changed files with 116 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import {
DocumentType,
generateAppID,
generateDevAppID,
generateScreenID,
getLayoutParams,
getScreenParams,
} from "../../db/utils"
@ -75,6 +76,7 @@ import sdk from "../../sdk"
import { builderSocket } from "../../websockets"
import { DefaultAppTheme, sdk as sharedCoreSDK } from "@budibase/shared-core"
import * as appMigrations from "../../appMigrations"
import { createSampleDataTableScreen } from "../../constants/screens"
// utility function, need to do away with this
async function getLayouts() {
@ -189,6 +191,13 @@ async function addSampleDataDocs() {
}
}
async function addSampleDataScreen() {
const db = context.getAppDB()
let screen = createSampleDataTableScreen()
screen._id = generateScreenID()
await db.put(screen)
}
export const addSampleData = async (
ctx: UserCtx<void, AddAppSampleDataResponse>
) => {
@ -295,11 +304,25 @@ async function performAppCreate(
return await context.doInAppContext(appId, async () => {
const instance = await createInstance(appId, instanceConfig)
const db = context.getAppDB()
const isImport = !!instanceConfig.file
const addSampleData = !isImport && !useTemplate
if (instanceConfig.useTemplate && !instanceConfig.file) {
await updateUserColumns(appId, db, ctx.user._id!)
}
// Add link to sample data page if required
const newLinks = addSampleData
? [
{
text: "Inventory",
url: "/inventory",
type: "link",
roleId: roles.BUILTIN_ROLE_IDS.BASIC,
},
]
: []
const newApplication: App = {
_id: DocumentType.APP_METADATA,
_rev: undefined,
@ -320,7 +343,7 @@ async function performAppCreate(
title: name,
navWidth: "Large",
navBackground: "var(--spectrum-global-color-gray-50)",
links: [],
links: newLinks,
},
theme: DefaultAppTheme,
customTheme: {
@ -334,7 +357,6 @@ async function performAppCreate(
creationVersion: undefined,
}
const isImport = !!instanceConfig.file
if (!isImport) {
newApplication.creationVersion = envCore.VERSION
}
@ -381,9 +403,10 @@ async function performAppCreate(
await uploadAppFiles(appId)
}
// Add sample datasource for all apps that aren't imports
if (!isImport && !useTemplate) {
// Add sample datasource and example screen for non-templates/non-imports
if (addSampleData) {
await addSampleDataDocs()
await addSampleDataScreen()
}
const latestMigrationId = appMigrations.getLatestEnabledMigrationId()

View File

@ -227,3 +227,92 @@ export function createQueryScreen(datasourceId: string, query: Query): Screen {
name: "screen-id",
}
}
export function createSampleDataTableScreen(): Screen {
return {
showNavigation: true,
width: "Large",
routing: { route: "/inventory", roleId: "BASIC", homeScreen: false },
name: "sample-data-inventory-screen",
props: {
_id: "c38f2b9f250fb4c33965ce47e12c02a80",
_component: "@budibase/standard-components/container",
_styles: { normal: {}, hover: {}, active: {}, selected: {} },
_children: [
{
_id: "cf600445f0b0048c79c0c81606b30d542",
_component: "@budibase/standard-components/gridblock",
_styles: {
normal: {
"--grid-desktop-col-start": 1,
"--grid-desktop-col-end": 13,
"--grid-desktop-row-start": 3,
"--grid-desktop-row-end": 21,
},
hover: {},
active: {},
selected: {},
},
_instanceName: "Inventory - Table",
_children: [],
table: {
label: "Inventory",
tableId: "ta_bb_inventory",
type: "table",
datasourceName: "Sample Data",
},
columns: [
{ label: "Item Tags", field: "Item Tags", active: true },
{ label: "Purchase Date", field: "Purchase Date", active: true },
{
label: "Purchase Price",
field: "Purchase Price",
active: true,
format:
// eslint-disable-next-line no-template-curly-in-string
"${{ [cf600445f0b0048c79c0c81606b30d542].[Purchase Price] }}",
},
{ label: "Notes", field: "Notes", active: true },
{
label: "Status",
field: "Status",
active: true,
conditions: [
{
target: "row",
metadataKey: "backgroundColor",
operator: "contains",
valueType: "array",
metadataValue: "var(--spectrum-global-color-red-100)",
noValue: false,
referenceValue: "Repair",
},
],
},
{ label: "SKU", field: "SKU", active: true },
{ label: "Item ID", field: "Item ID", active: true },
{ label: "Created At", field: "Created At", active: false },
{ label: "Updated At", field: "Updated At", active: false },
{ label: "Item Name", field: "Item Name", active: true },
],
initialSortColumn: "Item ID",
},
{
_id: "c09edf7de69be44ce8f0215c3f62e43a5",
_component: "@budibase/standard-components/textv2",
_styles: { normal: {}, hover: {}, active: {} },
_instanceName: "New Text",
align: "left",
text: "## Inventory",
},
],
_instanceName: "Inventory - List",
layout: "grid",
direction: "column",
hAlign: "stretch",
vAlign: "top",
size: "grow",
gap: "M",
},
}
}