Skip to content

Commit 373b396

Browse files
committed
fix: fix import table data csv
1 parent 48d2c72 commit 373b396

File tree

10 files changed

+55
-27
lines changed

10 files changed

+55
-27
lines changed

apps/frontend/src/lib/components/blocks/base/base-detail.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
export let getTableUrl: (tableId: string) => string
2121
</script>
2222

23-
<main class="h-full flex-1 px-4 py-4">
23+
<main class="flex flex-1 flex-col overflow-hidden px-4 py-4">
2424
{#if $hasPermission("table:create")}
2525
<div class="flex items-center gap-4">
2626
<button
@@ -50,19 +50,19 @@
5050
</div>
5151
{/if}
5252

53-
<section class="pt-3">
53+
<section class="flex flex-1 flex-col overflow-hidden pt-3">
5454
<h3 class="text-xl font-normal text-gray-600">Tables</h3>
5555

56-
<Table.Root>
57-
<Table.Header>
58-
<Table.Row>
56+
<Table.Root class="flex w-full flex-1 flex-col overflow-y-auto">
57+
<Table.Header class="flex w-full">
58+
<Table.Row class="w-full">
5959
<Table.Head>Name</Table.Head>
6060
</Table.Row>
6161
</Table.Header>
62-
<Table.Body>
62+
<Table.Body class="w-full flex-1">
6363
{#each base.tables as table}
6464
{#if table}
65-
<Table.Row class="cursor-pointer" on:click={() => goto(getTableUrl(table.id))}>
65+
<Table.Row class="flex w-full cursor-pointer" on:click={() => goto(getTableUrl(table.id))}>
6666
<Table.Cell class="flex items-center font-medium">
6767
<DatabaseIcon class="mr-2 h-4 w-4" />
6868
{table.name}

apps/frontend/src/lib/components/blocks/import-table/import-table.svelte

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Label } from "$lib/components/ui/label"
55
import { Checkbox } from "$lib/components/ui/checkbox"
66
import { parse, type ImportDataExtensions, type SheetData } from "$lib/import/import.helper"
7-
import { FileIcon, XIcon, ArrowRightIcon, ArrowLeftIcon } from "lucide-svelte"
7+
import { FileIcon, XIcon, ArrowRightIcon, ArrowLeftIcon, LoaderCircleIcon } from "lucide-svelte"
88
import * as Table from "$lib/components/ui/table"
99
import { invalidate, goto } from "$app/navigation"
1010
import { baseId, currentBase } from "$lib/store/base.store"
@@ -17,7 +17,6 @@
1717
FieldIdVo,
1818
inferCreateFieldType,
1919
systemFieldNames,
20-
systemFieldTypes,
2120
TableIdVo,
2221
type ICreateRecordDTO,
2322
type ICreateSchemaDTO,
@@ -36,7 +35,6 @@
3635
let firstRowAsHeader = true
3736
let importData = true
3837
let tableName: string | undefined = undefined
39-
let rs: string[][] = []
4038
4139
const createRecords = createMutation({
4240
mutationKey: ["table", "import", "records"],
@@ -56,7 +54,8 @@
5654
const createTable = createMutation({
5755
mutationKey: ["table", "import"],
5856
mutationFn: trpc.table.create.mutate,
59-
async onSuccess(data) {
57+
async onSuccess(tableId) {
58+
const rs = data?.data.slice(1).map((r) => r.map((v) => String(v))) ?? []
6059
if (importData && rs.length) {
6160
const records = rs.map((r, i) => {
6261
const record: ICreateRecordDTO = { values: {} }
@@ -72,7 +71,7 @@
7271
})
7372
7473
$createRecords.mutate({
75-
tableId: data,
74+
tableId: tableId,
7675
records,
7776
})
7877
} else {
@@ -96,7 +95,10 @@
9695
tableId = TableIdVo.create().value
9796
file = f
9897
tableName = getNextName(tableNames, file.name)
98+
9999
let parsed = await parse(file)
100+
console.log(parsed)
101+
100102
if (firstRowAsHeader) {
101103
const names = parsed.data[0].reduce((acc, cur) => {
102104
if (!cur) {
@@ -161,7 +163,6 @@
161163
id: FieldIdVo.create().value,
162164
display: i === 0,
163165
})) as ICreateSchemaDTO
164-
$: console.log(schema)
165166
</script>
166167

167168
{#if step === 0}
@@ -248,10 +249,17 @@
248249
Back
249250
</Button>
250251
{/if}
251-
<Button disabled={(step === 0 && !file) || (step === 1 && schema.length < 1)} on:click={handleClickImport} size="sm">
252+
<Button
253+
disabled={(step === 0 && !file) || (step === 1 && schema.length < 1) || $createTable.isPending}
254+
on:click={handleClickImport}
255+
size="sm"
256+
>
252257
{#if step === 0}
253258
Next step <ArrowRightIcon class="ml-2 h-4 w-4" />
254259
{:else}
260+
{#if $createTable.isPending}
261+
<LoaderCircleIcon class="mr-2 h-4 w-4 animate-spin" />
262+
{/if}
255263
Import
256264
{/if}
257265
</Button>

apps/frontend/src/lib/components/blocks/tables-nav/tables-nav.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@
134134
>
135135
<a
136136
href={`/t/${table.id}`}
137+
title={table.name}
137138
class={cn(
138-
"text-primary flex h-full flex-1 items-center font-normal",
139+
"text-primary flex h-full flex-1 items-center truncate font-normal",
139140
active && !viewId && "text-background font-medium",
140141
)}
141142
>

apps/frontend/src/lib/import/import.helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const parseCsv = async (file: File, options?: ParseDataOption): Promise<SheetDat
2323
return new Promise((resolve) => {
2424
Papa.parse<string[]>(file, {
2525
complete(results) {
26-
resolve(results.data)
26+
resolve(results.data.filter((r) => !(r.length === 1 && r[0] === "")))
2727
},
2828
})
2929
})

apps/frontend/src/routes/(authed)/(space)/bases/[baseId]/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616
</script>
1717

18-
<main class="flex flex-col">
18+
<main class="flex h-screen flex-col">
1919
{#if base}
2020
<BaseHeader {base} />
2121
<UpdateBaseDialog {base} />

packages/persistence/src/record/record.mutate-visitor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCurrentUserId, mustGetCurrentSpaceId } from "@undb/context/server"
2-
import type { ISpecification, ISpecVisitor } from "@undb/domain"
1+
import { getCurrentUserId,mustGetCurrentSpaceId } from "@undb/context/server"
2+
import type { ISpecification,ISpecVisitor } from "@undb/domain"
33
import {
44
CurrencyEqual,
55
DateIsEmpty,
@@ -53,10 +53,10 @@ import {
5353
type UserEmpty,
5454
type UserEqual,
5555
} from "@undb/table"
56-
import { sql, type ExpressionBuilder } from "kysely"
56+
import { sql,type ExpressionBuilder } from "kysely"
5757
import { unique } from "radash"
5858
import { AbstractQBMutationVisitor } from "../abstract-qb.visitor"
59-
import type { IQueryBuilder, IRecordQueryBuilder } from "../qb"
59+
import type { IQueryBuilder,IRecordQueryBuilder } from "../qb"
6060
import { JoinTable } from "../underlying/reference/join-table"
6161

6262
export class RecordMutateVisitor extends AbstractQBMutationVisitor implements IRecordVisitor {

packages/table/src/modules/schema/fields/field-value.factory.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { None, Option, Some } from "@undb/domain"
22
import { match } from "ts-pattern"
33
import type { JsonValue } from "type-fest"
44
import type { Field, FieldValue, MutableFieldValue } from "./field.type"
5-
import type { IOptionId } from "./option/option-id.vo"
65
import {
76
AutoIncrementFieldValue,
87
ButtonFieldValue,
@@ -27,7 +26,7 @@ import { EmailFieldValue } from "./variants/email-field"
2726
import { LongTextFieldValue } from "./variants/long-text-field/long-text-field-value.vo"
2827
import { PercentageFieldValue } from "./variants/percentage-field" // 新增导入
2928
import { RatingFieldValue } from "./variants/rating-field"
30-
import { SelectFieldValue } from "./variants/select-field"
29+
import { type SelectField, SelectFieldValue } from "./variants/select-field"
3130
import { UserFieldValue } from "./variants/user-field"
3231

3332
export class FieldValueFactory {
@@ -36,7 +35,11 @@ export class FieldValueFactory {
3635
.with({ type: "number" }, (field) => Some(new NumberFieldValue(field.valueSchema.parse(value))))
3736
.with({ type: "rating" }, (field) => Some(new RatingFieldValue(field.valueSchema.parse(value))))
3837
.with({ type: "string" }, (field) => Some(new StringFieldValue(field.valueSchema.parse(value))))
39-
.with({ type: "select" }, (field) => Some(new SelectFieldValue(field.valueSchema.parse(value))))
38+
.with({ type: "select" }, (field) => {
39+
const parsedValue = SelectFieldValue.parseValue(value, field as SelectField)
40+
console.log(parsedValue)
41+
return Some(new SelectFieldValue(field.valueSchema.parse(parsedValue)))
42+
})
4043
.with({ type: "reference" }, (field) => Some(new ReferenceFieldValue(field.valueSchema.parse(value))))
4144
.with({ type: "email" }, (field) => Some(new EmailFieldValue(field.valueSchema.parse(value))))
4245
.with({ type: "url" }, (field) => Some(new UrlFieldValue(field.valueSchema.parse(value))))
@@ -66,7 +69,7 @@ export class FieldValueFactory {
6669
.with("updatedBy", () => Some(new UpdatedByFieldValue(value as string)))
6770
.with("reference", () => Some(new ReferenceFieldValue(value as string[])))
6871
.with("rollup", () => Some(new RollupFieldValue(value as number | Date)))
69-
.with("select", () => Some(new SelectFieldValue(value as IOptionId)))
72+
.with("select", () => Some(new SelectFieldValue(SelectFieldValue.parseValue(value, field as SelectField))))
7073
.with("email", () => Some(new EmailFieldValue(value as string)))
7174
.with("url", () => Some(new UrlFieldValue(value as string)))
7275
.with("attachment", () => Some(new AttachmentFieldValue(value as IAttachmentFieldValue)))

packages/table/src/modules/schema/fields/field.util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export const inferCreateFieldType = (values: (string | number | null | object |
5757
options: Options.fromStrings(distinctValues.map((value) => value.toString() ?? "")).toJSON(),
5858
},
5959
type: "select",
60+
constraint: {
61+
max: 1,
62+
},
6063
}) as Omit<ICreateSelectFieldDTO, "id" | "name">,
6164
)
6265
.with(P.array(P.string), () => ({ type: "string" }))

packages/table/src/modules/schema/fields/variants/select-field/select-field-value.vo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ export class SelectFieldValue extends FieldValueObject<ISelectFieldValue> {
1111
super(Array.isArray(option) ? option : { value: option })
1212
}
1313

14+
static parseValue(value: string | string[] | null, field: SelectField): IOptionId | IOptionId[] | null {
15+
if (value === null || value === undefined) {
16+
return null
17+
}
18+
if (Array.isArray(value)) {
19+
return value
20+
.map((v) => field.options.find((op) => op.id === v || op.name === v))
21+
.filter((v) => !!v)
22+
.map((v) => v.id)
23+
}
24+
return field.options.find((op) => op.id === value || op.name === value)?.id ?? null
25+
}
26+
1427
isEmpty() {
1528
if (Array.isArray(this.props)) {
1629
return this.props.length === 0

packages/trpc/src/trpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export const p = t.procedure
3636
requestId,
3737
responseTime,
3838
type,
39-
input,
40-
rawInput,
39+
// input,
40+
// rawInput,
4141
path,
4242
}
4343
if (result.ok) {

0 commit comments

Comments
 (0)