Skip to content

Commit 0c31ea6

Browse files
committed
Working on recent pages
1 parent 32e2faf commit 0c31ea6

6 files changed

Lines changed: 93 additions & 26 deletions

File tree

src/CmdPalNotionExtension/CmdPalNotionExtensionCommandsProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ internal partial class CmdPalNotionExtensionCommandsProvider : CommandProvider
1414
private readonly TokenService _tokenService;
1515
private readonly SignInPage _signInPage;
1616
private readonly SignOutPage _signOutPage;
17-
private readonly RecentPagesPage _recentPagesPage;
17+
private readonly RecentPage _recentPagesPage;
1818
private readonly Resources _resources;
1919
private bool _isSignedIn;
2020

2121
public CmdPalNotionExtensionCommandsProvider(
2222
TokenService tokenService,
23-
RecentPagesPage recentPagesPage,
23+
RecentPage recentPagesPage,
2424
Resources resources,
2525
SignInPage signInPage,
2626
SignOutPage signOutPage)

src/CmdPalNotionExtension/Controls/Pages/RecentPagesPage.cs renamed to src/CmdPalNotionExtension/Controls/Pages/RecentPage.cs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
using CmdPalNotionExtension.Helpers;
2-
using CmdPalNotionExtension.ListItems;
3-
using CmdPalNotionExtension.Notion;
1+
using System.Collections.Generic;
2+
using System.Linq;
43
using Microsoft.CommandPalette.Extensions;
54
using Microsoft.CommandPalette.Extensions.Toolkit;
6-
using System.Collections.Generic;
7-
using System.Linq;
5+
6+
using CmdPalNotionExtension.Helpers;
7+
using CmdPalNotionExtension.ListItems;
8+
using CmdPalNotionExtension.Notion;
89

910
namespace CmdPalNotionExtension.Controls.Pages;
1011

11-
internal sealed partial class RecentPagesPage : ListPage
12+
internal sealed partial class RecentPage : DynamicListPage
1213
{
1314
private readonly NotionDataProvider _dataProvider;
1415
private readonly ListItemFactory _listItemFactory;
1516
private readonly Resources _resources;
1617

1718
private string? _cursor = string.Empty;
19+
private bool _hasMore;
1820
private List<IListItem> _currentPages = new List<IListItem>();
1921

20-
public RecentPagesPage(
22+
public RecentPage(
2123
NotionDataProvider dataProvider,
2224
ListItemFactory listItemFactory,
2325
Resources resources)
@@ -32,17 +34,51 @@ public RecentPagesPage(
3234

3335
public override IListItem[] GetItems()
3436
{
35-
var res = _dataProvider.GetRecentNotionPagesAsync(_cursor).GetAwaiter().GetResult();
36-
37-
if (res != null)
37+
if (_cursor == string.Empty)
3838
{
39-
_cursor = res.NextCursor;
40-
_currentPages.AddRange(res.Results.Select(s => _listItemFactory.Create(s)));
39+
var res = GetSearchResult();
40+
if (res != null)
41+
{
42+
_cursor = res.NextCursor;
43+
_hasMore = res.HasMore;
44+
_currentPages.AddRange(res.Results.Select(s => _listItemFactory.Create(s)));
45+
}
4146
}
4247

4348
return _currentPages.ToArray();
4449
}
4550

51+
public override void UpdateSearchText(string oldSearch, string newSearch)
52+
{
53+
_currentPages = new List<IListItem>();
54+
_hasMore = false;
55+
_cursor = string.Empty;
56+
RaiseItemsChanged();
57+
}
58+
59+
public override void LoadMore()
60+
{
61+
if (_hasMore)
62+
{
63+
var res = GetSearchResult();
64+
if (res != null)
65+
{
66+
_cursor = res.NextCursor;
67+
_hasMore = res.HasMore;
68+
_currentPages.AddRange(res.Results.Select(s => _listItemFactory.Create(s)));
69+
}
70+
RaiseItemsChanged();
71+
72+
base.LoadMore();
73+
}
74+
}
75+
76+
private SearchResult GetSearchResult()
77+
{
78+
var res = _dataProvider.GetRecentNotionPagesAsync(SearchText, _cursor).GetAwaiter().GetResult();
79+
return res;
80+
}
81+
4682
public CommandItem ToCommandItem()
4783
{
4884
return new CommandItem(this)
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using CmdPalNotionExtension.Notion.Models;
1+
using System.Linq;
22
using Microsoft.CommandPalette.Extensions;
33
using Microsoft.CommandPalette.Extensions.Toolkit;
4-
using System.Linq;
4+
5+
using CmdPalNotionExtension.Notion.Models;
56
using NotionPage = CmdPalNotionExtension.Notion.Models.Page;
67

78
namespace CmdPalNotionExtension.ListItems;
@@ -12,19 +13,45 @@ internal sealed partial class NotionPageListItem : ListItem
1213

1314
public NotionPageListItem(NotionPage notionPage, ICommand command) : base(command)
1415
{
15-
var titleProp = notionPage.Properties?["Name"];
1616
var title = "Unknown page";
17+
var icon = new IconInfo("\uE7C3");
1718

18-
if (titleProp != null)
19+
if (notionPage.Properties.ContainsKey("Name"))
20+
{
21+
var titleProp = notionPage.Properties["Name"];
22+
title = string.Join(" ", ((TitleProperty)titleProp).TitleDetails.SelectMany(s => s.PlainText).ToArray());
23+
}
24+
else if (notionPage.Properties.ContainsKey("Title"))
1925
{
26+
var titleProp = notionPage.Properties["Title"];
2027
title = string.Join(" ", ((TitleProperty)titleProp).TitleDetails.SelectMany(s => s.PlainText).ToArray());
2128
}
2229

30+
if (notionPage.Icon != null)
31+
{
32+
switch (notionPage.Icon.Type)
33+
{
34+
case "emoji":
35+
var emojiIcon = (EmojiIcon)notionPage.Icon;
36+
icon = new(emojiIcon.Emoji);
37+
break;
38+
case "file":
39+
var fileIcon = (FileIcon)notionPage.Icon;
40+
icon = new(fileIcon.File?.Url);
41+
break;
42+
case "custom_emoji":
43+
var customEmojiIcon = (CustomEmojiIcon)notionPage.Icon;
44+
icon = new(customEmojiIcon.Emoji?.Url);
45+
break;
46+
case "external":
47+
var externalIcon = (ExternalIcon)notionPage.Icon;
48+
icon = new(externalIcon.External?.Url);
49+
break;
50+
}
51+
}
2352

2453
Title = title;
25-
//Tags = anime.Genres.Select(genre => new Tag
26-
//{
27-
// Text = genre,
28-
//}).Take(_maxNumberOfTags).ToArray();
54+
Icon = icon;
55+
2956
}
3057
}

src/CmdPalNotionExtension/Notion/NotionDataProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ private async Task<T> SendAsync<T>(HttpRequestMessage request)
6666
}
6767
}
6868

69-
public async Task<SearchResult> GetRecentNotionPagesAsync(string? cursor)
69+
public async Task<SearchResult> GetRecentNotionPagesAsync(string? searchFor, string? cursor)
7070
{
7171
var query = new Query() { PageSize = 20 };
7272

7373
if (!string.IsNullOrEmpty(cursor))
7474
{
7575
query.Cursor = cursor;
7676
}
77+
if (!string.IsNullOrEmpty(searchFor))
78+
{
79+
query.SearchFor = searchFor;
80+
}
7781

7882
var payload = JsonSerializer.Serialize(query, _jsonSerializerOptions);
7983

src/CmdPalNotionExtension/Notion/Query.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace CmdPalNotionExtension.Notion;
55
internal sealed partial record Query
66
{
77
[JsonPropertyName("query")]
8-
public string? SearchFor { get; init; }
8+
public string? SearchFor { get; set; }
99

1010
[JsonPropertyName("sort")]
1111
public static object? Sort { get => new { direction = NotionSortDirection.DESC, timestamp = "last_edited_time" }; }
1212

1313
[JsonPropertyName("filter")]
14-
public object? Filter { get; init; }
14+
public object? Filter { get; set; }
1515

1616
[JsonPropertyName("start_cursor")]
1717
public string? Cursor { get; set; }

src/CmdPalNotionExtension/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static async Task HandleCOMServerActivationAsync()
108108
var commandFactory = new CommandFactory(dataProvider);
109109
var listItemFactory = new ListItemFactory(commandFactory, tokenService, dataProvider);
110110

111-
var recentPagesPage = new RecentPagesPage(dataProvider, listItemFactory, resources);
111+
var recentPagesPage = new RecentPage(dataProvider, listItemFactory, resources);
112112

113113
var commandProvider = new CmdPalNotionExtensionCommandsProvider(
114114
tokenService, recentPagesPage, resources, signInPage, signOutPage);

0 commit comments

Comments
 (0)