Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-editor): improvements for sheets from issues. (#5408)

Liu Ji12dccf588d41
diff --git a/frontend/src/components/Issue/logic/base.ts b/frontend/src/components/Issue/logic/base.ts
index 2d7c3d845b..59433aceff 100644
--- a/frontend/src/components/Issue/logic/base.ts
+++ b/frontend/src/components/Issue/logic/base.ts
@@ -18,6 +18,7 @@ import {
idFromSlug,
indexFromSlug,
issueSlug,
+ maybeSetSheetBacktracePayloadByIssue,
stageSlug,
taskSlug,
} from "@/utils";
@@ -59,6 +60,7 @@ export const useBaseIssueLogic = (params: {
const createdIssue = await issueStore.createIssue(issue);
await maybeCreateBackTraceComments(createdIssue);
+ await maybeSetSheetBacktracePayloadByIssue(createdIssue);
// Use replace to omit the new issue url in the navigation history.
router.replace(`/issue/${issueSlug(createdIssue.name, createdIssue.id)}`);
diff --git a/frontend/src/components/Issue/logic/common.ts b/frontend/src/components/Issue/logic/common.ts
index 25719599a9..566ce61b3f 100644
--- a/frontend/src/components/Issue/logic/common.ts
+++ b/frontend/src/components/Issue/logic/common.ts
@@ -29,7 +29,6 @@ import {
MigrationType,
TaskDatabaseSchemaBaselinePayload,
DatabaseId,
- TaskDatabaseSchemaUpdateGhostSyncPayload,
SheetId,
MigrationContext,
dialectOfEngine,
@@ -346,38 +345,6 @@ export const statementOfTask = (task: Task) => {
}
};
-export const sheetIdOfTask = (task: Task) => {
- switch (task.type) {
- case "bb.task.database.create":
- return (
- ((task as Task).payload as TaskDatabaseCreatePayload).sheetId ||
- undefined
- );
- case "bb.task.database.schema.update":
- return (
- ((task as Task).payload as TaskDatabaseSchemaUpdatePayload).sheetId ||
- undefined
- );
- case "bb.task.database.schema.update-sdl":
- return (
- ((task as Task).payload as TaskDatabaseSchemaUpdateSDLPayload)
- .sheetId || undefined
- );
- case "bb.task.database.data.update":
- return (
- ((task as Task).payload as TaskDatabaseDataUpdatePayload).sheetId ||
- undefined
- );
- case "bb.task.database.schema.update.ghost.sync":
- return (
- ((task as Task).payload as TaskDatabaseSchemaUpdateGhostSyncPayload)
- .sheetId || undefined
- );
- default:
- return undefined;
- }
-};
-
export const isTaskEditable = (task: Task): boolean => {
if (task.status === "PENDING_APPROVAL" || task.status === "FAILED") {
return true;
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 8bc3b0c401..ebff98be90 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -4,7 +4,7 @@
<script lang="ts" setup>
import { uniqBy } from "lodash-es";
-import { reactive, onMounted, computed, watch } from "vue";
+import { onMounted, computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
useCurrentUser,
@@ -20,7 +20,6 @@ import {
useSettingStore,
} from "@/store";
import {
- Database,
Connection,
ConnectionAtom,
ConnectionTreeMode,
@@ -41,18 +40,10 @@ import {
} from "@/utils";
import { useI18n } from "vue-i18n";
-type LocalState = {
- databaseList: Database[];
-};
-
const route = useRoute();
const router = useRouter();
const { t } = useI18n();
-const state = reactive<LocalState>({
- databaseList: [],
-});
-
const currentUser = useCurrentUser();
const instanceStore = useInstanceStore();
const databaseStore = useDatabaseStore();
@@ -90,7 +81,7 @@ const prepareAccessibleDatabaseList = async () => {
currentUser.value
)
);
- state.databaseList = databaseList;
+ connectionTreeStore.tree.databaseList = databaseList;
};
const prepareConnectionTree = async () => {
@@ -104,7 +95,7 @@ const prepareConnectionTree = async () => {
connectionTreeStore.tree.mode = ConnectionTreeMode.PROJECT;
return;
}
- const { databaseList } = state;
+ const { databaseList } = connectionTreeStore.tree;
const instanceList = uniqBy(
databaseList.map((db) => db.instance),
(instance) => instance.id
@@ -138,7 +129,7 @@ const prepareConnectionTree = async () => {
}
connectionTreeStore.tree.data = connectionTree;
} else {
- const databaseList = state.databaseList.filter((db) => {
+ const databaseList = connectionTreeStore.tree.databaseList.filter((db) => {
return db.project.id !== DEFAULT_PROJECT_ID;
});
const projectList = uniqBy(
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 02f68a7731..2d91bc006e 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1838,7 +1838,9 @@
"confirm-to-sync-sheet": "Confirm to sync sheets?",
"confirm-to-delete-this-sheet": "Confirm to delete this sheet?",
"confirm-to-duplicate-sheet": "Confirm to duplicate this sheet?"
- }
+ },
+ "linked-issue": "Linked issue",
+ "from-sheet-warning": "The sheet comes from issue {issue}. It is read-only and only part of it is displayed."
},
"engine": {
"mysql": "MySQL",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 83a4b106c2..42169008fd 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1837,7 +1837,9 @@
"confirm-to-sync-sheet": "确定要同步工作表?",
"confirm-to-delete-this-sheet": "确定删除这条工作表?",
"confirm-to-duplicate-sheet": "确定要复制这条工作表?"
- }
+ },
+ "linked-issue": "关联工单",
+ "from-sheet-warning": "这个工作表来自工单 {issue}。它是只读的且只显示了部分内容。"
},
"engine": {
"mysql": "MySQL",
diff --git a/frontend/src/store/modules/connectionTree.ts b/frontend/src/store/modules/connectionTree.ts
index b4fbbc160c..def39792dd 100644
--- a/frontend/src/store/modules/connectionTree.ts
+++ b/frontend/src/store/modules/connectionTree.ts
@@ -42,6 +42,7 @@ export const useConnectionTreeStore = defineStore("connectionTree", () => {
// states
const accessControlPolicyList = ref<Policy[]>([]);
const tree = reactive({
+ databaseList: [] as Database[],
data: [] as ConnectionAtom[],
mode: treeModeInLocalStorage.value,
state: ConnectionTreeState.UNSET,
diff --git a/frontend/src/types/sheet.ts b/frontend/src/types/sheet.ts
index a5e58e8fc9..0e14e42ea2 100644
--- a/frontend/src/types/sheet.ts
+++ b/frontend/src/types/sheet.ts
@@ -7,6 +7,7 @@ import {
ProjectId,
RowStatus,
PrincipalId,
+ IssueId,
} from ".";
export type SheetVisibility = "PRIVATE" | "PROJECT" | "PUBLIC";
@@ -24,10 +25,23 @@ interface SheetVCSPayload {
lastSyncTs: number;
}
+/**
+ * Mark a link from sheets to issues if a sheet is created in an issue via
+ * "Upload SQL"
+ */
+export type SheetIssueBacktracePayload = {
+ type: "bb.sheet.issue-backtrace";
+ issueId: IssueId;
+ issueName: string;
+};
+
// eslint-disable-next-line @typescript-eslint/ban-types
type SheetEmptyPayload = {};
-export type SheetPayload = SheetVCSPayload | SheetEmptyPayload;
+export type SheetPayload =
+ | SheetVCSPayload
+ | SheetIssueBacktracePayload
+ | SheetEmptyPayload;
export interface Sheet {
id: SheetId;
diff --git a/frontend/src/utils/sheet.ts b/frontend/src/utils/sheet.ts
index 286206eb2f..1974ba12b2 100644
--- a/frontend/src/utils/sheet.ts
+++ b/frontend/src/utils/sheet.ts
@@ -1,4 +1,21 @@
-import { Principal, Sheet, SheetPayload, SheetSource } from "@/types";
+import { isUndefined, uniq } from "lodash-es";
+
+import { useSheetStore } from "@/store";
+import {
+ Issue,
+ Principal,
+ Sheet,
+ SheetId,
+ SheetIssueBacktracePayload,
+ SheetPayload,
+ SheetSource,
+ Task,
+ TaskDatabaseCreatePayload,
+ TaskDatabaseDataUpdatePayload,
+ TaskDatabaseSchemaUpdateGhostSyncPayload,
+ TaskDatabaseSchemaUpdatePayload,
+ TaskDatabaseSchemaUpdateSDLPayload,
+} from "@/types";
import { hasProjectPermission, hasWorkspacePermission } from "../utils";
export const isSheetReadable = (sheet: Sheet, currentUser: Principal) => {
@@ -37,6 +54,11 @@ export const isSheetReadable = (sheet: Sheet, currentUser: Principal) => {
};
export const isSheetWritable = (sheet: Sheet, currentUser: Principal) => {
+ // If the sheet is linked to an issue, it's NOT writable
+ if (getSheetIssueBacktracePayload(sheet)) {
+ return false;
+ }
+
// writable to
// PRIVATE: the creator only
// PROJECT: the creator or project role can manage sheet, workspace Owner and DBA
@@ -93,3 +115,86 @@ export const getDefaultSheetPayloadWithSource = (
// For those sheet from VCS, we create and patch them in backend.
return {};
};
+
+export const sheetIdOfTask = (task: Task) => {
+ switch (task.type) {
+ case "bb.task.database.create":
+ return (
+ ((task as Task).payload as TaskDatabaseCreatePayload).sheetId ||
+ undefined
+ );
+ case "bb.task.database.schema.update":
+ return (
+ ((task as Task).payload as TaskDatabaseSchemaUpdatePayload).sheetId ||
+ undefined
+ );
+ case "bb.task.database.schema.update-sdl":
+ return (
+ ((task as Task).payload as TaskDatabaseSchemaUpdateSDLPayload)
+ .sheetId || undefined
+ );
+ case "bb.task.database.data.update":
+ return (
+ ((task as Task).payload as TaskDatabaseDataUpdatePayload).sheetId ||
+ undefined
+ );
+ case "bb.task.database.schema.update.ghost.sync":
+ return (
+ ((task as Task).payload as TaskDatabaseSchemaUpdateGhostSyncPayload)
+ .sheetId || undefined
+ );
+ default:
+ return undefined;
+ }
+};
+
+/**
+ * If the created issue contains SQL files uploaded as sheets
+ * we should patch the sheets' payloads with issueID and taskID
+ * to make a sheet backtrace-able to the issue/task it belongs to.
+ * Then we can display the backtrace issue link in the sheet list.
+ */
+export const maybeSetSheetBacktracePayloadByIssue = async (issue: Issue) => {
+ const sheetIdList: SheetId[] = [];
+
+ issue.pipeline.stageList.forEach((stage) => {
+ stage.taskList.forEach((task) => {
+ const sheetId = sheetIdOfTask(task);
+ if (sheetId) {
+ sheetIdList.push(sheetId);
+ }
+ });
+ });
+
+ const store = useSheetStore();
+ const requests = uniq(sheetIdList).map((sheetId) => {
+ const payload: SheetIssueBacktracePayload = {
+ type: "bb.sheet.issue-backtrace",
+ issueId: issue.id,
+ issueName: issue.name,
+ };
+ return store.patchSheetById({
+ id: sheetId,
+ payload,
+ });
+ });
+
+ try {
+ await Promise.all(requests);
+ } catch {
+ // nothing
+ }
+};
+
+export const getSheetIssueBacktracePayload = (sheet: Sheet) => {
+ const maybePayload = (sheet.payload ?? {}) as SheetIssueBacktracePayload;
+ if (
+ maybePayload.type === "bb.sheet.issue-backtrace" &&
+ !isUndefined(maybePayload.issueId) &&
+ !isUndefined(maybePayload.issueName)
+ ) {
+ return maybePayload;
+ }
+
+ return undefined;
+};
diff --git a/frontend/src/views/SheetDashboard.vue b/frontend/src/views/SheetDashboard.vue
index 07dc42f32d..bbdf0d081d 100644
--- a/frontend/src/views/SheetDashboard.vue
+++ b/frontend/src/views/SheetDashboard.vue
@@ -67,59 +67,13 @@
</n-button>
</div>
</div>
- <div class="w-full grid grid-cols-1">
- <div
- class="sheet-list-container text-sm text-gray-400"
- :class="currentSubPath"
- >
- <span
- v-for="header in getSheetTableHeaderLabelList()"
- :key="header.key"
- >{{ header.label }}</span
- >
- </div>
- <div
- v-if="state.isLoading"
- class="w-full flex flex-col py-6 justify-start items-center"
- >
- <span class="text-sm leading-6 text-gray-500">{{
- $t("sql-editor.loading-data")
- }}</span>
- </div>
- <div
- v-for="sheet in shownSheetList"
- :key="sheet.id"
- class="sheet-list-container text-sm cursor-pointer hover:bg-gray-100"
- :class="currentSubPath"
- @click="handleSheetClick(sheet)"
- >
- <span
- v-for="value in getSheetTableContentValueList(sheet)"
- :key="value.key"
- class="truncate w-5/6"
- >{{ value.value }}</span
- >
- <div class="flex flex-row justify-end items-center" @click.stop>
- <n-dropdown
- trigger="click"
- :options="getSheetDropDownOptions(sheet)"
- @select="(key: string) => handleDropDownActionBtnClick(key, sheet)"
- >
- <heroicons-outline:dots-horizontal
- class="w-6 h-auto border border-gray-300 bg-white p-1 rounded outline-none shadow"
- />
- </n-dropdown>
- </div>
- </div>
- <div
- v-show="!state.isLoading && shownSheetList.length === 0"
- class="w-full flex flex-col py-6 justify-start items-center"
- >
- <heroicons-outline:inbox class="w-12 h-auto text-gray-500" />
- <span class="text-sm leading-6 text-gray-500">{{
- $t("common.no-data")
- }}</span>
- </div>
+ <div class="w-full">
+ <SheetTable
+ :view="currentSheetViewMode"
+ :sheet-list="shownSheetList"
+ :loading="state.isLoading"
+ @refresh="fetchSheetData"
+ />
</div>
</div>
</div>
@@ -133,23 +87,23 @@
<script lang="ts" setup>
import { computed, onMounted, reactive, ref, watch } from "vue";
-import { useRoute, useRouter } from "vue-router";
+import { useRoute } from "vue-router";
import { last } from "lodash-es";
import { useDialog } from "naive-ui";
import { t } from "@/plugins/i18n";
-import dayjs from "@/plugins/dayjs";
+
import {
hasFeature,
useCurrentUser,
useProjectStore,
useSheetStore,
} from "@/store";
-import { Sheet, SheetCreate, SheetOrganizerUpsert } from "@/types";
+import { Sheet } from "@/types";
import {
- getDefaultSheetPayloadWithSource,
- isSheetWritable,
- sheetSlug,
-} from "@/utils";
+ type SheetViewMode,
+ SheetTable,
+ SheetViewModeList,
+} from "./sql-editor/Sheet";
interface LocalState {
isLoading: boolean;
@@ -158,7 +112,6 @@ interface LocalState {
}
const route = useRoute();
-const router = useRouter();
const dialog = useDialog();
const state = reactive<LocalState>({
isLoading: true,
@@ -250,17 +203,21 @@ const shouldShowClearSearchBtn = computed(() => {
return projectSelectorValue.value !== "" || sheetSearchValue.value !== "";
});
-const currentSubPath = computed(() => {
+const currentSheetViewMode = computed((): SheetViewMode => {
const { path } = route;
- return last(path.split("/")) || "";
+ const subPath = (last(path.split("/")) || "my") as SheetViewMode;
+ if (SheetViewModeList.includes(subPath)) {
+ return subPath;
+ }
+ return "my";
});
const fetchSheetData = async () => {
- if (currentSubPath.value === "my") {
+ if (currentSheetViewMode.value === "my") {
state.sheetList = await sheetStore.fetchMySheetList();
- } else if (currentSubPath.value === "starred") {
+ } else if (currentSheetViewMode.value === "starred") {
state.sheetList = await sheetStore.fetchStarredSheetList();
- } else if (currentSubPath.value === "shared") {
+ } else if (currentSheetViewMode.value === "shared") {
state.sheetList = await sheetStore.fetchSharedSheetList();
}
};
@@ -280,15 +237,6 @@ watch(
}
);
-const handleSheetClick = (sheet: Sheet) => {
- router.push({
- name: "sql-editor.share",
- params: {
- sheetSlug: sheetSlug(sheet),
- },
- });
-};
-
const handleClearSearchBtnClick = () => {
projectSelectorValue.value = "";
sheetSearchValue.value = "";
@@ -323,177 +271,10 @@ const handleSyncSheetFromVCS = () => {
maskClosable: false,
});
};
-
-const handleDropDownActionBtnClick = async (key: string, sheet: Sheet) => {
- if (key === "delete") {
- const dialogInstance = dialog.create({
- title: t("sheet.hint-tips.confirm-to-delete-this-sheet"),
- type: "info",
- async onPositiveClick() {
- await sheetStore.deleteSheetById(sheet.id);
- dialogInstance.destroy();
- },
- onNegativeClick() {
- dialogInstance.destroy();
- },
- negativeText: t("common.cancel"),
- positiveText: t("common.confirm"),
- showIcon: true,
- });
- } else if (key === "star" || key === "unstar") {
- const sheetOrganizerUpsert: SheetOrganizerUpsert = {
- sheeId: sheet.id,
- };
-
- if (key === "star") {
- sheetOrganizerUpsert.starred = true;
- } else if (key === "unstar") {
- sheetOrganizerUpsert.starred = false;
- }
-
- await sheetStore.upsertSheetOrganizer(sheetOrganizerUpsert);
- await fetchSheetData();
- } else if (key === "duplicate") {
- const dialogInstance = dialog.create({
- title: t("sheet.hint-tips.confirm-to-duplicate-sheet"),
- type: "info",
- async onPositiveClick() {
- const sheetCreate: SheetCreate = {
- projectId: sheet.projectId,
- name: sheet.name,
- statement: sheet.statement,
- visibility: "PRIVATE",
- payload: getDefaultSheetPayloadWithSource("BYTEBASE"),
- };
- if (sheet.databaseId) {
- sheetCreate.databaseId = sheet.databaseId;
- }
- await sheetStore.createSheet(sheetCreate);
- dialogInstance.destroy();
- },
- onNegativeClick() {
- dialogInstance.destroy();
- },
- negativeText: t("common.cancel"),
- positiveText: t("common.confirm"),
- showIcon: true,
- });
- }
-};
-
-const getSheetDropDownOptions = (sheet: Sheet) => {
- const options = [];
-
- if (sheet.starred) {
- options.push({
- key: "unstar",
- label: t("common.unstar"),
- });
- } else {
- options.push({
- key: "star",
- label: t("common.star"),
- });
- }
-
- if (currentSubPath.value === "my") {
- options.push({
- key: "delete",
- label: t("common.delete"),
- });
- } else if (currentSubPath.value === "shared") {
- const canDeleteSheet = isSheetWritable(sheet, currentUser.value);
- if (canDeleteSheet) {
- options.push({
- key: "delete",
- label: t("common.delete"),
- });
- }
-
- options.push({
- key: "duplicate",
- label: t("common.duplicate"),
- });
- }
-
- return options;
-};
-
-const getSheetTableHeaderLabelList = () => {
- const labelList = [
- {
- key: "name",
- label: t("common.name").toUpperCase(),
- },
- {
- key: "project",
- label: t("common.project").toUpperCase(),
- },
- {
- key: "visibility",
- label: t("common.visibility").toUpperCase(),
- },
- ];
-
- if (currentSubPath.value === "shared" || currentSubPath.value === "starred") {
- labelList.push({
- key: "creator",
- label: t("common.creator").toUpperCase(),
- });
- }
-
- labelList.push({
- key: "updated",
- label: t("common.updated-at").toUpperCase(),
- });
-
- return labelList;
-};
-
-const getSheetTableContentValueList = (sheet: Sheet) => {
- const valueList = [
- {
- key: "name",
- value: sheet.name,
- },
- {
- key: "project",
- value: sheet.project.name,
- },
- {
- key: "visibility",
- value: sheet.visibility,
- },
- ];
-
- if (currentSubPath.value === "shared" || currentSubPath.value === "starred") {
- valueList.push({
- key: "creator",
- value: sheet.creator.name,
- });
- }
-
- valueList.push({
- key: "updated",
- value: dayjs.duration(sheet.updatedTs * 1000 - Date.now()).humanize(true),
- });
-
- return valueList;
-};
</script>
<style scoped>
.active-link {
@apply bg-gray-100 text-accent;
}
-.sheet-list-container {
- @apply w-full grid py-3 px-4 border-b text-sm leading-6 select-none;
-}
-.sheet-list-container.my {
- grid-template-columns: 2fr repeat(3, 1fr) 32px;
-}
-.sheet-list-container.shared,
-.sheet-list-container.starred {
- grid-template-columns: 2fr repeat(4, 1fr) 32px;
-}
</style>
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index c9aa51af7b..8307e9dae7 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -5,11 +5,7 @@
<div
class="action-left gap-x-2 flex overflow-x-auto sm:overflow-x-hidden items-center"
>
- <NButton
- type="primary"
- :disabled="isEmptyStatement || isExecutingSQL"
- @click="handleRunQuery"
- >
+ <NButton type="primary" :disabled="!allowQuery" @click="handleRunQuery">
<mdi:play class="h-5 w-5 -ml-1.5" />
<span>
{{
@@ -19,18 +15,12 @@
<span class="hidden sm:inline">(⌘+⏎)</span>
</NButton>
- <NButton
- :disabled="isEmptyStatement || isExecutingSQL"
- @click="handleExplainQuery"
- >
+ <NButton :disabled="!allowQuery" @click="handleExplainQuery">
<mdi:play class="h-5 w-5 -ml-1.5" />
<span>Explain</span>
<span class="hidden sm:inline">(⌘+E)</span>
</NButton>
- <NButton
- :disabled="isEmptyStatement || isExecutingSQL"
- @click="handleFormatSQL"
- >
+ <NButton :disabled="!allowQuery" @click="handleFormatSQL">
<span>{{ $t("sql-editor.format") }}</span>
<span class="hidden sm:inline">(⇧+⌥+F)</span>
</NButton>
@@ -113,6 +103,9 @@ const webTerminalStore = useWebTerminalStore();
const connection = computed(() => tabStore.currentTab.connection);
+const isDisconnected = computed(() => {
+ return tabStore.isDisconnected;
+});
const isEmptyStatement = computed(
() => !tabStore.currentTab || tabStore.currentTab.statement === ""
);
@@ -135,6 +128,13 @@ const showRunSelected = computed(() => {
);
});
+const allowQuery = computed(() => {
+ if (isDisconnected.value) return false;
+ if (isEmptyStatement.value) return false;
+ if (isExecutingSQL.value) return false;
+ return true;
+});
+
const allowSave = computed(() => {
if (!showSheetsFeature.value) {
return false;
diff --git a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
index c9776c1d84..28cefd5775 100644
--- a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
@@ -5,7 +5,9 @@
<ConnectionPathBar />
- <template v-if="!tabStore.isDisconnected">
+ <SheetForIssueTipsBar />
+
+ <template v-if="!tabStore.isDisconnected || sheetBacktracePayload">
<SQLEditor @execute="handleExecute" @save-sheet="trySaveSheet" />
</template>
<template v-else>
@@ -25,10 +27,15 @@
</template>
<script lang="ts" setup>
-import { ref } from "vue";
+import { computed, ref } from "vue";
import type { Connection, ExecuteConfig, ExecuteOption } from "@/types";
-import { useCurrentTab, useInstanceStore, useTabStore } from "@/store";
+import {
+ useCurrentTab,
+ useInstanceStore,
+ useSheetStore,
+ useTabStore,
+} from "@/store";
import SQLEditor from "./SQLEditor.vue";
import {
EditorAction,
@@ -37,13 +44,23 @@ import {
ExecutingHintModal,
SaveSheetModal,
} from "../EditorCommon";
+import SheetForIssueTipsBar from "./SheetForIssueTipsBar.vue";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { AIChatToSQL } from "@/plugins/ai";
+import { getSheetIssueBacktracePayload } from "@/utils";
const tabStore = useTabStore();
+const sheetStore = useSheetStore();
const saveSheetModal = ref<InstanceType<typeof SaveSheetModal>>();
const tab = useCurrentTab();
+const sheetBacktracePayload = computed(() => {
+ const sheetId = tabStore.currentTab.sheetId;
+ if (!sheetId) return undefined;
+ const sheet = sheetStore.getSheetById(sheetId);
+ return getSheetIssueBacktracePayload(sheet);
+});
+
const { execute } = useExecuteSQL();
const handleExecute = (
diff --git a/frontend/src/views/sql-editor/EditorPanel/SheetForIssueTipsBar.vue b/frontend/src/views/sql-editor/EditorPanel/SheetForIssueTipsBar.vue
new file mode 100644
index 0000000000..6029d8b95e
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorPanel/SheetForIssueTipsBar.vue
@@ -0,0 +1,82 @@
+<template>
+ <div v-if="payload" class="w-full p-4 flex bg-yellow-50">
+ <div class="flex-shrink-0">
+ <heroicons-solid:information-circle class="h-5 w-5 text-yellow-400" />
+ </div>
+ <div class="ml-3">
+ <i18n-t
+ tag="h3"
+ keypath="sheet.from-sheet-warning"
+ class="text-sm font-medium text-yellow-800"
+ >
+ <template #issue>
+ <NTooltip :disabled="loading || !issue">
+ <template #trigger>
+ <a
+ v-if="issue"
+ class="normal-link"
+ href="`/issue/${issueSlug(issue.name, issue.id)}`"
+ >
+ #{{ payload.issueId }}
+ </a>
+ <span v-else>#{{ payload.issueId }}</span>
+ </template>
+
+ <div class="max-w-[20rem]">
+ {{ issue?.name }}
+ </div>
+ </NTooltip>
+ </template>
+ </i18n-t>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { useIssueStore, useSheetStore, useTabStore } from "@/store";
+import { Issue } from "@/types";
+import { getSheetIssueBacktracePayload } from "@/utils";
+import { NTooltip } from "naive-ui";
+import { shallowRef } from "vue";
+import { computed, ref, watch } from "vue";
+
+const tabStore = useTabStore();
+const sheetStore = useSheetStore();
+const issueStore = useIssueStore();
+const tab = computed(() => tabStore.currentTab);
+const loading = ref(true);
+const issue = shallowRef<Issue>();
+
+const sheet = computed(() => {
+ const { sheetId } = tab.value;
+ if (!sheetId) return undefined;
+ return sheetStore.getSheetById(sheetId);
+});
+
+const payload = computed(() => {
+ if (!sheet.value) return undefined;
+ return getSheetIssueBacktracePayload(sheet.value);
+});
+
+watch(
+ payload,
+ (payload) => {
+ loading.value = true;
+ const finish = (_issue: Issue | undefined) => {
+ loading.value = false;
+ issue.value = _issue;
+ };
+
+ if (!payload) {
+ return finish(undefined);
+ }
+ const { issueId } = payload;
+ issueStore.getOrFetchIssueById(issueId).then((_issue) => {
+ if (_issue.id === issueId) {
+ return finish(_issue);
+ }
+ });
+ },
+ { immediate: true }
+);
+</script>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
new file mode 100644
index 0000000000..5e2a5d5838
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/SheetTable/Dropdown.vue
@@ -0,0 +1,136 @@
+<template>
+ <NDropdown
+ v-if="options.length > 0"
+ trigger="click"
+ :options="options"
+ @select="handleAction"
+ >
+ <heroicons-outline:dots-horizontal
+ class="w-6 h-auto border border-gray-300 bg-white p-1 rounded outline-none shadow"
+ />
+ </NDropdown>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+import { useI18n } from "vue-i18n";
+import { type DropdownOption, NDropdown, useDialog } from "naive-ui";
+
+import type { Sheet, SheetCreate, SheetOrganizerUpsert } from "@/types";
+import type { SheetViewMode } from "../types";
+import { getDefaultSheetPayloadWithSource, isSheetWritable } from "@/utils";
+import { useCurrentUser, useSheetStore } from "@/store";
+
+const props = defineProps<{
+ view: SheetViewMode;
+ sheet: Sheet;
+}>();
+
+const emit = defineEmits<{
+ (event: "refresh"): void;
+}>();
+
+const { t } = useI18n();
+const currentUser = useCurrentUser();
+const sheetStore = useSheetStore();
+const dialog = useDialog();
+
+const options = computed(() => {
+ const options: DropdownOption[] = [];
+ const { sheet, view } = props;
+
+ if (sheet.starred) {
+ options.push({
+ key: "unstar",
+ label: t("common.unstar"),
+ });
+ } else {
+ options.push({
+ key: "star",
+ label: t("common.star"),
+ });
+ }
+
+ const canDeleteSheet = isSheetWritable(sheet, currentUser.value);
+ if (view === "my") {
+ if (canDeleteSheet) {
+ options.push({
+ key: "delete",
+ label: t("common.delete"),
+ });
+ }
+ } else if (view === "shared") {
+ if (canDeleteSheet) {
+ options.push({
+ key: "delete",
+ label: t("common.delete"),
+ });
+ }
+
+ options.push({
+ key: "duplicate",
+ label: t("common.duplicate"),
+ });
+ }
+
+ return options;
+});
+
+const handleAction = async (key: string) => {
+ const { sheet } = props;
+ if (key === "delete") {
+ const dialogInstance = dialog.create({
+ title: t("sheet.hint-tips.confirm-to-delete-this-sheet"),
+ type: "info",
+ async onPositiveClick() {
+ await sheetStore.deleteSheetById(sheet.id);
+ dialogInstance.destroy();
+ },
+ onNegativeClick() {
+ dialogInstance.destroy();
+ },
+ negativeText: t("common.cancel"),
+ positiveText: t("common.confirm"),
+ showIcon: true,
+ });
+ } else if (key === "star" || key === "unstar") {
+ const sheetOrganizerUpsert: SheetOrganizerUpsert = {
+ sheeId: sheet.id,
+ };
+
+ if (key === "star") {
+ sheetOrganizerUpsert.starred = true;
+ } else if (key === "unstar") {
+ sheetOrganizerUpsert.starred = false;
+ }
+
+ await sheetStore.upsertSheetOrganizer(sheetOrganizerUpsert);
+ emit("refresh");
+ } else if (key === "duplicate") {
+ const dialogInstance = dialog.create({
+ title: t("sheet.hint-tips.confirm-to-duplicate-sheet"),
+ type: "info",
+ async onPositiveClick() {
+ const sheetCreate: SheetCreate = {
+ projectId: sheet.projectId,
+ name: sheet.name,
+ statement: sheet.statement,
+ visibility: "PRIVATE",
+ payload: getDefaultSheetPayloadWithSource("BYTEBASE"),
+ };
+ if (sheet.databaseId) {
+ sheetCreate.databaseId = sheet.databaseId;
+ }
+ await sheetStore.createSheet(sheetCreate);
+ dialogInstance.destroy();
+ },
+ onNegativeClick() {
+ dialogInstance.destroy();
+ },
+ negativeText: t("common.cancel"),
+ positiveText: t("common.confirm"),
+ showIcon: true,
+ });
+ }
+};
+</script>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/IssueLink.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/IssueLink.vue
new file mode 100644
index 0000000000..874d186d87
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/SheetTable/IssueLink.vue
@@ -0,0 +1,33 @@
+<template>
+ <NTooltip v-if="payload">
+ <template #trigger>
+ <a
+ class="normal-link"
+ target="_blank"
+ :href="`/issue/${issueSlug(payload.issueName, payload.issueId)}`"
+ @click.stop=""
+ >#{{ payload.issueId }}</a
+ >
+ </template>
+
+ <div class="max-w-[20rem]">
+ {{ payload.issueName }}
+ </div>
+ </NTooltip>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+import { NTooltip } from "naive-ui";
+
+import { Sheet } from "@/types";
+import { getSheetIssueBacktracePayload, issueSlug } from "@/utils";
+
+const props = defineProps<{
+ sheet: Sheet;
+}>();
+
+const payload = computed(() => {
+ return getSheetIssueBacktracePayload(props.sheet);
+});
+</script>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue b/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue
new file mode 100644
index 0000000000..3e9d6c64d6
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/SheetTable/SheetTable.vue
@@ -0,0 +1,172 @@
+<template>
+ <div class="w-full grid grid-cols-1">
+ <div class="sheet-list-container text-sm text-gray-400" :class="view">
+ <span v-for="header in headers" :key="header.key" class="uppercase">{{
+ header.label
+ }}</span>
+ </div>
+ <div
+ v-if="loading"
+ class="w-full flex flex-col py-6 justify-start items-center"
+ >
+ <span class="text-sm leading-6 text-gray-500">{{
+ $t("sql-editor.loading-data")
+ }}</span>
+ </div>
+ <div
+ v-for="sheet in sheetList"
+ :key="sheet.id"
+ class="sheet-list-container text-sm cursor-pointer hover:bg-gray-100"
+ :class="view"
+ @click="handleSheetClick(sheet)"
+ >
+ <span
+ v-for="value in getValueList(sheet)"
+ :key="value.key"
+ class="truncate w-5/6"
+ >{{ value.value }}</span
+ >
+ <div>
+ <IssueLink :sheet="sheet" />
+ </div>
+ <div class="flex flex-row justify-end items-center" @click.stop>
+ <Dropdown :sheet="sheet" :view="view" @refresh="$emit('refresh')" />
+ </div>
+ </div>
+
+ <div
+ v-show="!loading && sheetList.length === 0"
+ class="w-full flex flex-col py-6 justify-start items-center"
+ >
+ <heroicons-outline:inbox class="w-12 h-auto text-gray-500" />
+ <span class="text-sm leading-6 text-gray-500">{{
+ $t("common.no-data")
+ }}</span>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+import { useI18n } from "vue-i18n";
+import dayjs from "dayjs";
+
+import type { Sheet } from "@/types";
+import { SheetViewMode } from "../types";
+import IssueLink from "./IssueLink.vue";
+import Dropdown from "./Dropdown.vue";
+import { useRouter } from "vue-router";
+import { sheetSlug } from "@/utils";
+
+const props = withDefaults(
+ defineProps<{
+ view: SheetViewMode;
+ sheetList: Sheet[];
+ loading?: boolean;
+ }>(),
+ {
+ view: "my",
+ loading: false,
+ }
+);
+
+defineEmits<{
+ (event: "refresh"): void;
+}>();
+
+const { t } = useI18n();
+const router = useRouter();
+
+const showCreator = computed(() => {
+ return props.view === "shared" || props.view === "starred";
+});
+
+const handleSheetClick = (sheet: Sheet) => {
+ router.push({
+ name: "sql-editor.share",
+ params: {
+ sheetSlug: sheetSlug(sheet),
+ },
+ });
+};
+
+const headers = computed(() => {
+ const labelList = [
+ {
+ key: "name",
+ label: t("common.name"),
+ },
+ {
+ key: "project",
+ label: t("common.project"),
+ },
+ {
+ key: "visibility",
+ label: t("common.visibility"),
+ },
+ ];
+
+ if (showCreator.value) {
+ labelList.push({
+ key: "creator",
+ label: t("common.creator"),
+ });
+ }
+
+ labelList.push({
+ key: "updated",
+ label: t("common.updated-at"),
+ });
+
+ labelList.push({
+ key: "linked-issue",
+ label: t("sheet.linked-issue"),
+ });
+
+ return labelList;
+});
+
+const getValueList = (sheet: Sheet) => {
+ const valueList = [
+ {
+ key: "name",
+ value: sheet.name,
+ },
+ {
+ key: "project",
+ value: sheet.project.name,
+ },
+ {
+ key: "visibility",
+ value: sheet.visibility,
+ },
+ ];
+
+ if (showCreator.value) {
+ valueList.push({
+ key: "creator",
+ value: sheet.creator.name,
+ });
+ }
+
+ valueList.push({
+ key: "updated",
+ value: dayjs.duration(sheet.updatedTs * 1000 - Date.now()).humanize(true),
+ });
+
+ return valueList;
+};
+</script>
+
+<style scoped lang="postcss">
+.sheet-list-container {
+ @apply w-full grid py-3 px-4 border-b text-sm leading-6 select-none;
+}
+.sheet-list-container.my {
+ grid-template-columns: 2fr repeat(4, 1fr) 32px;
+}
+.sheet-list-container.shared,
+.sheet-list-container.starred {
+ grid-template-columns: 2fr repeat(5, 1fr) 32px;
+}
+</style>
diff --git a/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts b/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts
new file mode 100644
index 0000000000..7c7bac0e73
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/SheetTable/index.ts
@@ -0,0 +1,3 @@
+import SheetTable from "./SheetTable.vue";
+
+export default SheetTable;
diff --git a/frontend/src/views/sql-editor/Sheet/index.ts b/frontend/src/views/sql-editor/Sheet/index.ts
new file mode 100644
index 0000000000..0da44a2cb6
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/index.ts
@@ -0,0 +1,4 @@
+import SheetTable from "./SheetTable";
+
+export * from "./types";
+export { SheetTable };
diff --git a/frontend/src/views/sql-editor/Sheet/types.ts b/frontend/src/views/sql-editor/Sheet/types.ts
new file mode 100644
index 0000000000..139c7743cc
--- /dev/null
+++ b/frontend/src/views/sql-editor/Sheet/types.ts
@@ -0,0 +1,2 @@
+export const SheetViewModeList = ["my", "shared", "starred"] as const;
+export type SheetViewMode = typeof SheetViewModeList[number];
diff --git a/frontend/tailwind.config.js b/frontend/tailwind.config.js
index 4f98f4922d..6b82079dae 100644
--- a/frontend/tailwind.config.js
+++ b/frontend/tailwind.config.js
@@ -27,6 +27,7 @@ module.exports = {
{ pattern: /^text-yellow-/ },
{ pattern: /^text-red-/ },
{ pattern: /^grid-cols-/ },
+ { pattern: /^(uppercase|lowercase|capitalize)$/ },
],
theme: {
extend: {