Skip to content

Commit 0843cbd

Browse files
authored
Merge pull request #38 from litencatt/title-quick-search-by-filter
Title quick search by filter
2 parents d4cacbd + 63a728c commit 0843cbd

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/index.ts

+26-9
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ app.action("open-modal-button", async ({ ack, body, client, logger }) => {
5454

5555
if (dbId == undefined) {
5656
const dbs = await cache.getDatabases()
57-
const metaData = {
57+
const metaData: MetaData = {
5858
channel_id: body.channel.id,
5959
thread_ts: body.message.thread_ts,
60+
filter_values: [],
6061
}
6162
await client.views.open({
6263
trigger_id: body.trigger_id,
@@ -70,6 +71,7 @@ app.action("open-modal-button", async ({ ack, body, client, logger }) => {
7071
thread_ts: body.message.thread_ts,
7172
selected_db_id: dbId,
7273
selected_db_name: dbTitle,
74+
filter_values: [],
7375
}
7476

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

153155
// Set search string to metaData
154156
metaData.search_string = body.actions[0].value
155-
157+
metaData.filters = null
158+
if (metaData.search_string) {
159+
const titlePropName = await notion.getDbPropNameByType(metaData.selected_db_id, "title")
160+
metaData.filters = {
161+
and: [
162+
{
163+
property: titlePropName,
164+
title: {
165+
contains: metaData.search_string,
166+
},
167+
},
168+
],
169+
}
170+
}
156171
const params = {
157172
database_id: metaData.selected_db_id,
158173
page_size: 10,
159174
}
160175
if (metaData.filters != null) {
161176
params["filter"] = metaData.filters as QueryDatabaseParameters["filter"]
162177
}
178+
console.dir(params, { depth: null })
163179
const res = await notion.client.databases.query(params)
164180
const urls = await notion.getPageUrls(res, metaData.search_string)
165181
if (urls.length == 0) {
166-
urls.push("該当するページはありませんでした")
182+
urls.push("No Results")
167183
}
168184
metaData.next_cursor = res.has_more ? res.next_cursor : ""
169185

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

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

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

436452
app.action("clear_filter-action", async ({ ack, body, client, logger }) => {
437-
logger.info("add_filter action called")
453+
logger.info("clear_filter action called")
438454
ack()
439455

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

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

@@ -536,7 +553,7 @@ app.action("next_result-action", async ({ ack, body, client, logger }) => {
536553
const res = await notion.client.databases.query(params)
537554
const urls = await notion.getPageUrls(res, metaData.search_string)
538555
if (urls.length == 0) {
539-
urls.push("該当するページはありませんでした")
556+
urls.push("No Results")
540557
}
541558
const nextCursor = res.has_more ? res.next_cursor : ""
542559
metaData.next_cursor = nextCursor
@@ -567,7 +584,7 @@ app.view("search-db-modal", async ({ ack, view, client, logger }) => {
567584
})
568585
const urls = await notion.getPageUrls(res)
569586
if (urls.length == 0) {
570-
urls.push("該当するページはありませんでした")
587+
urls.push("No Results")
571588
}
572589

573590
// Reply result

src/notion.ts

+16
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ export const buildFilterPropertyOptions = (db: GetDatabaseResponse) => {
400400
const propOptions = []
401401
Object.entries(db.properties).forEach(([_, prop]) => {
402402
// Hide not supported types
403+
// title is not supported because it is filter by quick search
403404
switch (prop.type) {
404405
case "date":
405406
case "created_time":
@@ -412,6 +413,7 @@ export const buildFilterPropertyOptions = (db: GetDatabaseResponse) => {
412413
case "people":
413414
case "rollup":
414415
case "unique_id":
416+
case "title":
415417
return
416418
}
417419

@@ -491,3 +493,17 @@ export const buildDatabaseQueryFilter = (fv: FilterValue): QueryDatabaseParamete
491493
}
492494
return filter
493495
}
496+
497+
export const getDbPropNameByType = async (dbId: string, targetType: string) => {
498+
const { properties } = await client.databases.retrieve({
499+
database_id: dbId,
500+
})
501+
502+
let name = null
503+
Object.entries(properties).forEach(([_, prop]) => {
504+
if (prop.type == targetType) {
505+
name = prop.name
506+
}
507+
})
508+
return name
509+
}

src/slack.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const searchPagesResultView = (metaData: any, urls: any[]) => {
146146
type: "button",
147147
text: {
148148
type: "plain_text",
149-
text: "Clear",
149+
text: "Clear All",
150150
},
151151
style: "danger",
152152
action_id: "clear_filter-action",
@@ -249,14 +249,14 @@ export const selectFilterPropertyView = (metaData: any, propOptions: any[]) => {
249249
type: "static_select",
250250
placeholder: {
251251
type: "plain_text",
252-
text: "Select a property",
252+
text: "Pick an option",
253253
},
254254
options: propOptions,
255255
action_id: "select_prop-action",
256256
},
257257
label: {
258258
type: "plain_text",
259-
text: "フィルター用プロパティ選択",
259+
text: "Property",
260260
},
261261
},
262262
],
@@ -314,7 +314,7 @@ export const selectFilterPropertyFieldView = (
314314
},
315315
label: {
316316
type: "plain_text",
317-
text: "フィルタープロパティのフィールド選択",
317+
text: "Field",
318318
},
319319
},
320320
],
@@ -362,7 +362,7 @@ export const selectFilterValueInputView = (
362362
block_id: "select_prop_field",
363363
text: {
364364
type: "plain_text",
365-
text: `field: ${selectedPropertyField}`,
365+
text: `Field: ${selectedPropertyField}`,
366366
emoji: true,
367367
},
368368
},
@@ -380,7 +380,7 @@ export const selectFilterValueInputView = (
380380
},
381381
label: {
382382
type: "plain_text",
383-
text: "フィルター値入力",
383+
text: "Value",
384384
},
385385
},
386386
],

0 commit comments

Comments
 (0)