Skip to content

Commit

Permalink
Merge pull request #38 from litencatt/title-quick-search-by-filter
Browse files Browse the repository at this point in the history
Title quick search by filter
  • Loading branch information
litencatt committed Dec 17, 2023
2 parents d4cacbd + 63a728c commit 0843cbd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
35 changes: 26 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ app.action("open-modal-button", async ({ ack, body, client, logger }) => {

if (dbId == undefined) {
const dbs = await cache.getDatabases()
const metaData = {
const metaData: MetaData = {
channel_id: body.channel.id,
thread_ts: body.message.thread_ts,
filter_values: [],
}
await client.views.open({
trigger_id: body.trigger_id,
Expand All @@ -70,6 +71,7 @@ app.action("open-modal-button", async ({ ack, body, client, logger }) => {
thread_ts: body.message.thread_ts,
selected_db_id: dbId,
selected_db_name: dbTitle,
filter_values: [],
}

const res = await notion.client.databases.query({
Expand Down Expand Up @@ -152,18 +154,32 @@ app.action("title_search_input-action", async ({ ack, body, client, logger }) =>

// Set search string to metaData
metaData.search_string = body.actions[0].value

metaData.filters = null
if (metaData.search_string) {
const titlePropName = await notion.getDbPropNameByType(metaData.selected_db_id, "title")
metaData.filters = {
and: [
{
property: titlePropName,
title: {
contains: metaData.search_string,
},
},
],
}
}
const params = {
database_id: metaData.selected_db_id,
page_size: 10,
}
if (metaData.filters != null) {
params["filter"] = metaData.filters as QueryDatabaseParameters["filter"]
}
console.dir(params, { depth: null })
const res = await notion.client.databases.query(params)
const urls = await notion.getPageUrls(res, metaData.search_string)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}
metaData.next_cursor = res.has_more ? res.next_cursor : ""

Expand Down Expand Up @@ -364,7 +380,7 @@ app.action("select_prop_value-action", async ({ ack, body, client, logger }) =>
})
const urls = await notion.getPageUrls(res, metaData.search_string)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}
metaData.next_cursor = res.has_more ? res.next_cursor : ""

Expand Down Expand Up @@ -414,7 +430,7 @@ app.action("select_prop_value_input-action", async ({ ack, body, client, logger
})
const urls = await notion.getPageUrls(res, metaData.search_string)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}

// プロパティ設定用モーダルに更新
Expand All @@ -434,13 +450,14 @@ app.view("set-filter-property", async ({ ack, view, client, logger }) => {
})

app.action("clear_filter-action", async ({ ack, body, client, logger }) => {
logger.info("add_filter action called")
logger.info("clear_filter action called")
ack()

try {
const metaData = JSON.parse(body.view.private_metadata) as MetaData
console.dir({ metaData }, { depth: null })

// Clear filters
metaData.filter_values = []
metaData.filters = null
metaData.search_string = null
Expand Down Expand Up @@ -503,7 +520,7 @@ app.action("filter-remove-action", async ({ ack, body, client, logger }) => {
const res = await notion.client.databases.query(params)
const urls = await notion.getPageUrls(res, metaData.search_string)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}
metaData.next_cursor = res.has_more ? res.next_cursor : ""

Expand Down Expand Up @@ -536,7 +553,7 @@ app.action("next_result-action", async ({ ack, body, client, logger }) => {
const res = await notion.client.databases.query(params)
const urls = await notion.getPageUrls(res, metaData.search_string)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}
const nextCursor = res.has_more ? res.next_cursor : ""
metaData.next_cursor = nextCursor
Expand Down Expand Up @@ -567,7 +584,7 @@ app.view("search-db-modal", async ({ ack, view, client, logger }) => {
})
const urls = await notion.getPageUrls(res)
if (urls.length == 0) {
urls.push("該当するページはありませんでした")
urls.push("No Results")
}

// Reply result
Expand Down
16 changes: 16 additions & 0 deletions src/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ export const buildFilterPropertyOptions = (db: GetDatabaseResponse) => {
const propOptions = []
Object.entries(db.properties).forEach(([_, prop]) => {
// Hide not supported types
// title is not supported because it is filter by quick search
switch (prop.type) {
case "date":
case "created_time":
Expand All @@ -412,6 +413,7 @@ export const buildFilterPropertyOptions = (db: GetDatabaseResponse) => {
case "people":
case "rollup":
case "unique_id":
case "title":
return
}

Expand Down Expand Up @@ -491,3 +493,17 @@ export const buildDatabaseQueryFilter = (fv: FilterValue): QueryDatabaseParamete
}
return filter
}

export const getDbPropNameByType = async (dbId: string, targetType: string) => {
const { properties } = await client.databases.retrieve({
database_id: dbId,
})

let name = null
Object.entries(properties).forEach(([_, prop]) => {
if (prop.type == targetType) {
name = prop.name
}
})
return name
}
12 changes: 6 additions & 6 deletions src/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const searchPagesResultView = (metaData: any, urls: any[]) => {
type: "button",
text: {
type: "plain_text",
text: "Clear",
text: "Clear All",
},
style: "danger",
action_id: "clear_filter-action",
Expand Down Expand Up @@ -249,14 +249,14 @@ export const selectFilterPropertyView = (metaData: any, propOptions: any[]) => {
type: "static_select",
placeholder: {
type: "plain_text",
text: "Select a property",
text: "Pick an option",
},
options: propOptions,
action_id: "select_prop-action",
},
label: {
type: "plain_text",
text: "フィルター用プロパティ選択",
text: "Property",
},
},
],
Expand Down Expand Up @@ -314,7 +314,7 @@ export const selectFilterPropertyFieldView = (
},
label: {
type: "plain_text",
text: "フィルタープロパティのフィールド選択",
text: "Field",
},
},
],
Expand Down Expand Up @@ -362,7 +362,7 @@ export const selectFilterValueInputView = (
block_id: "select_prop_field",
text: {
type: "plain_text",
text: `field: ${selectedPropertyField}`,
text: `Field: ${selectedPropertyField}`,
emoji: true,
},
},
Expand All @@ -380,7 +380,7 @@ export const selectFilterValueInputView = (
},
label: {
type: "plain_text",
text: "フィルター値入力",
text: "Value",
},
},
],
Expand Down

0 comments on commit 0843cbd

Please sign in to comment.