From a66c1955940fa65382a295d26da22b86d6793b29 Mon Sep 17 00:00:00 2001 From: nichenqin Date: Sat, 31 Aug 2024 14:47:50 +0800 Subject: [PATCH 1/8] fix: fix ts issue --- packages/table/src/modules/schema/schema.vo.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/table/src/modules/schema/schema.vo.ts b/packages/table/src/modules/schema/schema.vo.ts index ac6c6a226..633001711 100644 --- a/packages/table/src/modules/schema/schema.vo.ts +++ b/packages/table/src/modules/schema/schema.vo.ts @@ -13,6 +13,7 @@ import type { View } from "../views" import type { ICreateSchemaDTO } from "./dto" import type { ISchemaDTO } from "./dto/schema.dto" import { + ButtonField, CreatedByField, FieldIdVo, FieldNameVo, From 0fedc23ca3f5a312e5d83afaca4a44ba422f4368 Mon Sep 17 00:00:00 2001 From: nichenqin Date: Sat, 31 Aug 2024 14:49:39 +0800 Subject: [PATCH 2/8] fix: fix default button label --- .../schema/fields/variants/button-field/button-field.vo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/table/src/modules/schema/fields/variants/button-field/button-field.vo.ts b/packages/table/src/modules/schema/fields/variants/button-field/button-field.vo.ts index 2ae7e852f..7eef45f2d 100644 --- a/packages/table/src/modules/schema/fields/variants/button-field/button-field.vo.ts +++ b/packages/table/src/modules/schema/fields/variants/button-field/button-field.vo.ts @@ -68,7 +68,7 @@ export class ButtonField extends AbstractField { From 2ce558f85b1b94268a5eefd5f8541f54b2095c4f Mon Sep 17 00:00:00 2001 From: nichenqin Date: Sat, 31 Aug 2024 14:51:20 +0800 Subject: [PATCH 3/8] fix: fix options picker class --- .../src/lib/components/blocks/option/options-picker.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/frontend/src/lib/components/blocks/option/options-picker.svelte b/apps/frontend/src/lib/components/blocks/option/options-picker.svelte index 6c7414e56..b0a9cc211 100644 --- a/apps/frontend/src/lib/components/blocks/option/options-picker.svelte +++ b/apps/frontend/src/lib/components/blocks/option/options-picker.svelte @@ -33,9 +33,9 @@ variant="outline" role="combobox" aria-expanded={open} - class={cn("w-full justify-between overflow-hidden", $$restProps.class)} - {disabled} + {disabled} {...$$restProps} + class={cn("w-full justify-between overflow-hidden", $$restProps.class)} >
{#if selectedValue.length} From d4a726ed9c77131d3b2e14b2d95a81199c31bf6c Mon Sep 17 00:00:00 2001 From: nichenqin Date: Sat, 31 Aug 2024 16:36:51 +0800 Subject: [PATCH 4/8] feat: single user macro --- .../blocks/field-control/user-control.svelte | 6 ++++- .../field-options/user-field-option.svelte | 9 +++++-- .../variants/user-filter-input.svelte | 15 +++++++++-- .../components/blocks/user/user-macro.svelte | 14 ++++++++++ .../components/blocks/user/user-picker.svelte | 23 +++++++++++++++- packages/i18n/src/i18n/en/index.ts | 7 ++++- packages/i18n/src/i18n/i18n-types.ts | 12 +++++++++ .../src/record/record.filter-visitor.ts | 26 ++++++++++++++----- .../src/record/record.mutate-visitor.ts | 18 ++++++++++++- .../src/modules/schema/fields/field.macro.ts | 1 + .../src/modules/schema/fields/field.type.ts | 3 +++ .../fields/variants/user-field/index.ts | 1 + .../variants/user-field/user-field-macro.ts | 11 ++++++++ .../user-field/user-field-value.vo.ts | 10 ++++--- 14 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 apps/frontend/src/lib/components/blocks/user/user-macro.svelte create mode 100644 packages/table/src/modules/schema/fields/field.macro.ts create mode 100644 packages/table/src/modules/schema/fields/variants/user-field/user-field-macro.ts diff --git a/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte b/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte index a34c28006..ef84ceea4 100644 --- a/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte +++ b/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte @@ -1,11 +1,12 @@ + +{#if value} +
+ + {$LL.table.macros[value]()} +
+{/if} diff --git a/apps/frontend/src/lib/components/blocks/user/user-picker.svelte b/apps/frontend/src/lib/components/blocks/user/user-picker.svelte index 73836079d..5139932ac 100644 --- a/apps/frontend/src/lib/components/blocks/user/user-picker.svelte +++ b/apps/frontend/src/lib/components/blocks/user/user-picker.svelte @@ -9,7 +9,9 @@ import { GetUsersStore } from "$houdini" import { Skeleton } from "$lib/components/ui/skeleton" import * as Avatar from "$lib/components/ui/avatar" - import type { ISingleUserFieldValue } from "@undb/table" + import { isUserFieldMacro, userFieldMacros, type ISingleUserFieldValue } from "@undb/table" + import { UserIcon } from "lucide-svelte" + import UserMacro from "./user-macro.svelte" let q = "" $: store = new GetUsersStore() @@ -54,6 +56,9 @@ {selectedValue.user.username} {/if} + {#if value && isUserFieldMacro(value)} + + {/if}
@@ -76,6 +81,22 @@ {/if} + {#each userFieldMacros as macro} + { + value = currentValue === value ? null : currentValue + closeAndFocusTrigger(ids.trigger) + onValueChange(value) + }} + > + +
+ +
+
+ {/each} {#each users as user} = { @@ -102,6 +102,10 @@ const workspaceRoles: Record = { viewer: "Viewer" } +const macros: Record = { + "@me": "Current User" +} + const en = { table: { ops, @@ -109,6 +113,7 @@ const en = { rollupFns, aggregateFns, workspaceRoles, + macros }, } satisfies BaseTranslation diff --git a/packages/i18n/src/i18n/i18n-types.ts b/packages/i18n/src/i18n/i18n-types.ts index 70f81b7f3..5053db4b7 100644 --- a/packages/i18n/src/i18n/i18n-types.ts +++ b/packages/i18n/src/i18n/i18n-types.ts @@ -364,6 +364,12 @@ type RootTranslation = { */ viewer: string } + macros: { + /** + * C​u​r​r​e​n​t​ ​U​s​e​r + */ + '@me': string + } } } @@ -719,6 +725,12 @@ export type TranslationFunctions = { */ viewer: () => LocalizedString } + macros: { + /** + * Current User + */ + '@me': () => LocalizedString + } } } diff --git a/packages/persistence/src/record/record.filter-visitor.ts b/packages/persistence/src/record/record.filter-visitor.ts index 4b07a51e7..75adbb0c8 100644 --- a/packages/persistence/src/record/record.filter-visitor.ts +++ b/packages/persistence/src/record/record.filter-visitor.ts @@ -1,3 +1,4 @@ +import { getCurrentUserId } from "@undb/context/server" import { NotImplementException } from "@undb/domain" import { CurrencyEqual, @@ -6,6 +7,7 @@ import { LongTextEqual, SelectField, UrlEqual, + isUserFieldMacro, type AttachmentEmpty, type AttachmentEqual, type CheckboxEqual, @@ -117,12 +119,24 @@ export class RecordFilterVisitor extends AbstractQBVisitor implements const cond = this.eb.eb(this.getFieldId(spec), "=", null) this.addCond(cond) } else { - const cond = this.eb.eb( - this.getFieldId(spec), - "=", - isString(spec.value) ? spec.value : JSON.stringify(spec.value), - ) - this.addCond(cond) + function convertMacro(value: string) { + if (isUserFieldMacro(value)) { + if (value === "@me") { + return getCurrentUserId() + } + } + + return value + } + if (Array.isArray(spec.value)) { + const converted = spec.value.map(convertMacro) + const cond = this.eb.eb(this.getFieldId(spec), "=", JSON.stringify(converted)) + this.addCond(cond) + } else { + const converted = spec.value ? convertMacro(spec.value) : null + const cond = this.eb.eb(this.getFieldId(spec), "=", converted) + this.addCond(cond) + } } } userEmpty(spec: UserEmpty): void { diff --git a/packages/persistence/src/record/record.mutate-visitor.ts b/packages/persistence/src/record/record.mutate-visitor.ts index 35b085f4e..6de347811 100644 --- a/packages/persistence/src/record/record.mutate-visitor.ts +++ b/packages/persistence/src/record/record.mutate-visitor.ts @@ -4,6 +4,7 @@ import { CurrencyEqual, DateIsEmpty, ID_TYPE, + isUserFieldMacro, JsonContains, LongTextEqual, SelectContainsAnyOf, @@ -227,7 +228,22 @@ export class RecordMutateVisitor extends AbstractQBMutationVisitor implements IR const fieldValue = new UserFieldValue(spec.value) const value = fieldValue.getValue(field) - this.setData(spec.fieldId.value, Array.isArray(value) ? JSON.stringify(value) : value) + function convertMacro(value: string) { + if (isUserFieldMacro(value)) { + if (value === "@me") { + return getCurrentUserId() + } + } + + return value + } + + if (Array.isArray(value)) { + const converted = value.map(convertMacro) + this.setData(spec.fieldId.value, JSON.stringify(converted)) + } else { + this.setData(spec.fieldId.value, value ? convertMacro(value) : null) + } } userEmpty(spec: UserEmpty): void { this.setData(spec.fieldId.value, null) diff --git a/packages/table/src/modules/schema/fields/field.macro.ts b/packages/table/src/modules/schema/fields/field.macro.ts new file mode 100644 index 000000000..0bc6dd54c --- /dev/null +++ b/packages/table/src/modules/schema/fields/field.macro.ts @@ -0,0 +1 @@ +export type FieldMacroPattern = `@${string}` diff --git a/packages/table/src/modules/schema/fields/field.type.ts b/packages/table/src/modules/schema/fields/field.type.ts index af57851a6..f3d298a15 100644 --- a/packages/table/src/modules/schema/fields/field.type.ts +++ b/packages/table/src/modules/schema/fields/field.type.ts @@ -122,6 +122,7 @@ import type { UserField, UserFieldValue, } from "./variants/user-field" +import type { IUserFieldMacro } from "./variants/user-field/user-field-macro" export type Field = | StringField @@ -275,3 +276,5 @@ export type IFieldConstraint = | ICurrencyFieldConstraint export type IFieldOption = IReferenceFieldOption | IRollupFieldOption | ISelectFieldOption | IButtonFieldOption + +export type IFieldMacro = IUserFieldMacro diff --git a/packages/table/src/modules/schema/fields/variants/user-field/index.ts b/packages/table/src/modules/schema/fields/variants/user-field/index.ts index 73ff60869..5546afea7 100644 --- a/packages/table/src/modules/schema/fields/variants/user-field/index.ts +++ b/packages/table/src/modules/schema/fields/variants/user-field/index.ts @@ -1,4 +1,5 @@ export * from "./user-field-constraint.vo" +export * from "./user-field-macro" export * from "./user-field-value.visitor" export * from "./user-field-value.vo" export * from "./user-field.condition" diff --git a/packages/table/src/modules/schema/fields/variants/user-field/user-field-macro.ts b/packages/table/src/modules/schema/fields/variants/user-field/user-field-macro.ts new file mode 100644 index 000000000..44d8e1480 --- /dev/null +++ b/packages/table/src/modules/schema/fields/variants/user-field/user-field-macro.ts @@ -0,0 +1,11 @@ +import { z } from "@undb/zod" + +export const userFieldMacroSchema = z.enum(["@me"]) + +export const userFieldMacros = userFieldMacroSchema.options + +export type IUserFieldMacro = z.infer + +export function isUserFieldMacro(value: string): value is IUserFieldMacro { + return userFieldMacros.includes(value as any) +} diff --git a/packages/table/src/modules/schema/fields/variants/user-field/user-field-value.vo.ts b/packages/table/src/modules/schema/fields/variants/user-field/user-field-value.vo.ts index 1cb86f9eb..1d33fc9aa 100644 --- a/packages/table/src/modules/schema/fields/variants/user-field/user-field-value.vo.ts +++ b/packages/table/src/modules/schema/fields/variants/user-field/user-field-value.vo.ts @@ -1,16 +1,20 @@ import { z } from "@undb/zod" import { isEmpty } from "radash" import { FieldValueObject } from "../../field-value" +import { userFieldMacroSchema } from "./user-field-macro" import type { UserField } from "./user-field.vo" const userId = z.string() -export const userFieldValue = z.union([userId, userId.array()]).nullable().optional() +export const userFieldValue = z + .union([userId.or(userFieldMacroSchema), userId.or(userFieldMacroSchema).array()]) + .nullable() + .optional() -export const singleUserFieldValue = userId.nullable() +export const singleUserFieldValue = userId.or(userFieldMacroSchema).nullable() export type ISingleUserFieldValue = z.infer -export const multipleUserFieldValue = userId.array().nullable() +export const multipleUserFieldValue = userId.or(userFieldMacroSchema).array().nullable() export type IMultipleUserFieldValue = z.infer export type IUserFieldValue = z.infer From 612f2ca45dc80c2e5b31c7b94e3417d0bcdfadbb Mon Sep 17 00:00:00 2001 From: nichenqin Date: Sat, 31 Aug 2024 17:45:49 +0800 Subject: [PATCH 5/8] feat: user macro --- .../blocks/field-control/user-control.svelte | 8 +++++-- .../field-options/user-field-option.svelte | 8 +++++-- .../variants/user-filter-input.svelte | 5 +++++ .../grid-view/editable-cell/user-cell.svelte | 11 +++++++--- .../blocks/user/users-picker.svelte | 21 ++++++++++++++++++- .../src/record/record.filter-visitor.ts | 4 ++-- .../src/record/record.mutate-visitor.ts | 3 ++- 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte b/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte index ef84ceea4..33998dc99 100644 --- a/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte +++ b/apps/frontend/src/lib/components/blocks/field-control/user-control.svelte @@ -44,8 +44,12 @@ >
{#each value ?? [] as user, i} - {@const value = selected.find((u) => u.user.id === user)?.user ?? displayValue?.[i]} - + {#if isUserFieldMacro(user)} + + {:else} + {@const value = selected.find((u) => u.user.id === user)?.user ?? displayValue?.[i]} + + {/if} {/each}
diff --git a/apps/frontend/src/lib/components/blocks/field-options/user-field-option.svelte b/apps/frontend/src/lib/components/blocks/field-options/user-field-option.svelte index c4d01ba79..43020709e 100644 --- a/apps/frontend/src/lib/components/blocks/field-options/user-field-option.svelte +++ b/apps/frontend/src/lib/components/blocks/field-options/user-field-option.svelte @@ -84,8 +84,12 @@ {#if defaultValue?.length}
{#each defaultValue ?? [] as user, i} - {@const value = selected.find((u) => u.user.id === user)?.user} - + {#if isUserFieldMacro(user)} + + {:else} + {@const value = selected.find((u) => u.user.id === user)?.user} + + {/if} {/each}
{:else} diff --git a/apps/frontend/src/lib/components/blocks/filters-editor/variants/user-filter-input.svelte b/apps/frontend/src/lib/components/blocks/filters-editor/variants/user-filter-input.svelte index 27d59964a..85f93e056 100644 --- a/apps/frontend/src/lib/components/blocks/filters-editor/variants/user-filter-input.svelte +++ b/apps/frontend/src/lib/components/blocks/filters-editor/variants/user-filter-input.svelte @@ -52,6 +52,11 @@ builders={[builder]} class={cn("flex w-full items-center justify-between", $$restProps.class)} > + {#each value ?? [] as user} + {#if isUserFieldMacro(user)} + + {/if} + {/each} s.user)} /> diff --git a/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/user-cell.svelte b/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/user-cell.svelte index 4333c624e..c24c6b696 100644 --- a/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/user-cell.svelte +++ b/apps/frontend/src/lib/components/blocks/grid-view/editable-cell/user-cell.svelte @@ -1,5 +1,5 @@