Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@
/** Whom the commit composers suggest for an @mention, as the web's composers ask. */
public const string MentionSuggestionsPath = 'search/mentions';

/** The people matching a name or handle, as the web's search finds them, with whether you follow each one. */
public const string UserSearchPath = 'search/users';
/**
* The people and the organizations with a page matching a name or handle, as the web's search finds them, with
* whether you follow each person.
*/
public const string ProfileSearchPath = 'search/profiles';

/** The commits nearest in meaning to a term, closest first, as the web's search finds them. */
public const string CommitSearchPath = 'search/commits';
Expand Down
28 changes: 20 additions & 8 deletions app/NativeComponents/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Devnull\Client;
use App\Devnull\Data\Commit;
use App\Devnull\Data\Connection;
use App\Devnull\Data\User;
use App\Devnull\RowCache;
use App\Devnull\Stash;
use App\Devnull\Timeline;
Expand All @@ -21,9 +22,9 @@
/**
* The web's search, as its phone header opens it: one field for developers or
* commits. As you type, the developers whose name or handle matches come
* first, with Follow beside them, then the commits nearest in meaning to what
* you typed, closest first. Before you type it offers the web's searches to
* try.
* first, with Follow beside them, then the organizations that have a page on
* devnull, then the commits nearest in meaning to what you typed, closest
* first. Before you type it offers the web's searches to try.
*
* Both are asked for on a background thread, and each answer only lands
* under the term it was found for. What's showing stays while the next term's
Expand Down Expand Up @@ -74,7 +75,14 @@ class Search extends NativeComponent
public array $people = [];

/**
* The term the people were found for, so a slow answer never lands under a newer one.
* The organizations found with the people, which open their page as the web's do.
*
* @var list<User>
*/
public array $organizations = [];

/**
* The term the people and organizations were found for, so a slow answer never lands under a newer one.
*/
public ?string $peopleFoundFor = null;

Expand Down Expand Up @@ -181,7 +189,8 @@ protected function searchesCommits(string $term): bool
}

/**
* Asks devnull for the developers and the commits matching the term. An emptied field shows the searches to try.
* Asks devnull for the developers, the organizations and the commits matching the term. An emptied field shows
* the searches to try.
*/
protected function search(): void
{
Expand All @@ -191,20 +200,23 @@ protected function search(): void

if ($term === '') {
$this->people = [];
$this->organizations = [];
$this->peopleFoundFor = null;
$this->commits = [];
$this->commitsFoundFor = null;

return;
}

$this->refresh(Client::UserSearchPath, ['q' => ltrim($term, '@')], function (array $json) use ($term): void {
$this->refresh(Client::ProfileSearchPath, ['q' => ltrim($term, '@')], function (array $json) use ($term): void {
if ($this->term() !== $term) {
return;
}

$people = is_array($json['data'] ?? null) ? $json['data'] : [];
$people = is_array($json['users'] ?? null) ? $json['users'] : [];
$organizations = is_array($json['organizations'] ?? null) ? $json['organizations'] : [];
$this->people = array_values(array_map(Connection::fromArray(...), array_filter($people, 'is_array')));
$this->organizations = array_values(array_map(fn (array $organization): User => User::fromArray(['type' => 'organization', ...$organization]), array_filter($organizations, 'is_array')));
$this->peopleFoundFor = $term;
});

Expand Down Expand Up @@ -236,7 +248,7 @@ protected function revalidate(): void

protected function showsContent(): bool
{
return $this->people !== [] || $this->commits !== [];
return $this->people !== [] || $this->organizations !== [] || $this->commits !== [];
}

/**
Expand Down
31 changes: 28 additions & 3 deletions resources/views/native/search.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{{--
The web's search: one field for developers or commits. Before you type, the web's searches to try. Then the
developers whose name or handle matches, then the commits nearest in meaning, on a rail of their own as the web's
results page draws them. A term with nothing near it answers in git's voice.
developers whose name or handle matches, then the organizations with a page: the square avatar, name, handle and bio
the web's search shows, laid out like a developer's row, and a tap opens their page. Then the commits nearest in
meaning, on a rail of their own as the web's results page draws them. A term with nothing near it answers in git's
voice.
--}}
<native:column class="w-full h-full bg-theme-surface items-center">
@include('native.partials.error')
Expand Down Expand Up @@ -40,6 +42,29 @@
@endforeach
@endif

@if ($organizations !== [])
<native:row ref="organizations-heading" class="w-full min-h-[44] px-4 py-3 border-b border-theme-hairline bg-theme-surface">
<native:text font="semibold" class="text-base text-theme-ink">Organizations</native:text>
</native:row>
@foreach ($organizations as $organization)
<native:pressable native:key="organization-{{ $organization->id }}" ref="organization-{{ $organization->username }}" @press="openOrganization('{{ $organization->username }}')" class="w-full bg-theme-surface" a11y-label="{{ $organization->name }}, {{ $organization->handle() }}">
<native:row class="w-full px-4 py-3 gap-3 items-start">
<native:stack :width="48" :height="48" center class="flex-shrink-0">
@include('native.partials.avatar', ['user' => $organization, 'size' => 40])
</native:stack>
<native:column class="flex-1 gap-[2]">
<native:text font="semibold" class="w-full text-[15px] text-theme-ink" :max-lines="1">{{ $organization->name }}</native:text>
<native:text font="mono" class="w-full text-[13px] text-theme-ink-muted" :max-lines="1">{{ $organization->handle() }}</native:text>
@if ($organization->bio)
<native:text class="w-full mt-1 text-sm text-theme-ink" :max-lines="2">{{ $organization->bio }}</native:text>
@endif
</native:column>
</native:row>
<native:column class="w-full h-[1] border-b border-theme-hairline" />
</native:pressable>
@endforeach
@endif

@if ($searchesCommits)
<native:row ref="commits-heading" class="w-full min-h-[44] px-4 py-3 gap-2 items-center border-b border-theme-hairline bg-theme-surface">
<native:text font="semibold" class="flex-1 text-base text-theme-ink">Most relevant commits</native:text>
Expand All @@ -62,7 +87,7 @@
@else
@include('native.partials.feed', ['rows' => $rows, 'rowCache' => $rowCache])
@endif
@elseif ($peopleFoundFor === $term && $people === [])
@elseif ($peopleFoundFor === $term && $people === [] && $organizations === [])
<native:column ref="people-none" class="w-full px-8 py-12 items-center bg-theme-surface">
<native:text font="mono" class="text-[13px] text-center text-theme-ink-muted" :max-lines="1">fatal: nothing matches '{{ $term }}'</native:text>
</native:column>
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Screens/PartialFramesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ function partialFrameInbox(array $groups, ?string $cursor = null): array
$found = fn (int $count, string $prefix): array => array_map(fn (int $n): array => commitJson(['id' => "{$prefix}{$n}", 'body' => "{$term} result {$n}"]), range(1, $count));

return match (parse_url($request->url(), PHP_URL_PATH)) {
'/api/v1/search/users' => Http::response(['data' => [userJson(['id' => 'id-ada', 'handle' => 'ada', 'name' => 'Ada', 'is_followed' => false, 'follows_you' => false])]]),
'/api/v1/search/profiles' => Http::response(['users' => [userJson(['id' => 'id-ada', 'handle' => 'ada', 'name' => 'Ada', 'is_followed' => false, 'follows_you' => false])], 'organizations' => []]),
// The same commits come back for two terms, in a different order, as a search can find them.
'/api/v1/search/commits' => Http::response(['data' => str_starts_with($term, 'php') ? $found(8, 'c') : array_reverse($found(8, 'c'))]),
default => Http::response(pageJson([])),
Expand Down
58 changes: 49 additions & 9 deletions tests/Feature/Screens/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ function foundPerson(string $username, array $overrides = []): array
}

/**
* Answers people searches with $people and commit searches with $commits. Anything else, such as the Log behind
* the search button, gets an empty page.
* An organization as devnull's profile search sends it, which has no id of its own.
*
* @param array<string, mixed> $overrides
* @return array<string, mixed>
*/
function foundOrganization(string $handle, array $overrides = []): array
{
return ['handle' => $handle, 'name' => ucfirst($handle), 'avatar_url' => null, 'bio' => null, 'html_url' => "http://devnull.test/@{$handle}", 'lane' => null, 'is_followed' => false, ...$overrides];
}

/**
* Answers profile searches with $people and $organizations, and commit searches with $commits. Anything else, such as
* the Log behind the search button, gets an empty page.
*
* @param list<array<string, mixed>> $people
* @param list<array<string, mixed>> $commits
* @param array<string, array{0: mixed, 1?: int}> $writes
* @param list<array<string, mixed>> $organizations
*/
function serveSearch(array $people = [], array $commits = [], array $writes = []): void
function serveSearch(array $people = [], array $commits = [], array $writes = [], array $organizations = []): void
{
serveApi(function (Request $request) use ($people, $commits, $writes) {
serveApi(function (Request $request) use ($people, $commits, $writes, $organizations) {
$route = $request->method().' '.parse_url($request->url(), PHP_URL_PATH);

return match (true) {
isset($writes[$route]) => Http::response($writes[$route][0], $writes[$route][1] ?? 200),
$route === 'GET /api/v1/search/users' => Http::response(['data' => $people]),
$route === 'GET /api/v1/search/profiles' => Http::response(['users' => $people, 'organizations' => $organizations]),
$route === 'GET /api/v1/search/commits' => Http::response(['data' => $commits]),
default => Http::response(pageJson([])),
};
});
}

/**
* The q each search of $kind ("users" or "commits") was sent with, in order.
* The q each search of $kind ("profiles" or "commits") was sent with, in order.
*
* @return list<string>
*/
Expand Down Expand Up @@ -110,10 +122,38 @@ function searchedFor(string $kind): array
->and(array_map(fn ($commit): string => $commit->id, $screen->instance()->commits))->toBe(['c2', 'c1'])
->and(dayMarkers($screen))->toBe([])
// A handle is found without its @, and commits by what was typed.
->and(searchedFor('users'))->toBe(['ada'])
->and(searchedFor('profiles'))->toBe(['ada'])
->and(searchedFor('commits'))->toBe(['@ada']);
});

it('finds the organizations with a page under the developers, and opens one on the same stack', function () {
serveSearch(
people: [foundPerson('taylor')],
organizations: [foundOrganization('laravel', ['bio' => 'The PHP framework for web artisans.'])],
);

$screen = Native::visit('/log/search')
->set('query', 'la')
->assertSee('Organizations')
->assertSee('Laravel')
->assertSee('@laravel')
->assertSee('The PHP framework for web artisans.');

expect(childRefs($screen, 'search-results'))->toBe(['developers-heading', 'person-taylor', 'organizations-heading', 'organization-laravel'])
->and(rendersRef($screen, 'follow-laravel'))->toBeFalse();

$screen->tap('organization-laravel')->assertNavigatedTo('/log/organizations/laravel');
});

it('says nothing matches only when no developer or organization does', function () {
serveSearch(organizations: [foundOrganization('laravel')]);

Native::visit('/log/search')
->set('query', 'la')
->assertSee('Laravel')
->assertDontSee("fatal: nothing matches 'la'");
});

it('searches commits for a tapped hashtag straight away, without taking focus', function () {
serveSearch(commits: [commitJson(['id' => 'c1', 'body' => 'Shipped it #ab with #laravel'])]);

Expand Down Expand Up @@ -142,7 +182,7 @@ function searchedFor(string $kind): array
->assertSee("fatal: nothing matches 'zz'")
->assertDontSee('Most relevant commits');

expect(searchedFor('users'))->toBe(['zz'])
expect(searchedFor('profiles'))->toBe(['zz'])
->and(searchedFor('commits'))->toBe([]);
});

Expand All @@ -161,7 +201,7 @@ function searchedFor(string $kind): array
serveApi(function (Request $request) {
parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query);

return Http::response(['data' => [foundPerson($query['q'])]]);
return Http::response(['users' => [foundPerson($query['q'])], 'organizations' => []]);
});

$screen = Native::visit('/log/search')->set('query', 'ad')->set('query', 'grace');
Expand Down