Remember for each user which Kanban cards have their task list open

The open cards are kept in the user's board preferences (expanded_cards,
at most 500), saved without redrawing the board and restored on return.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-30 11:25:58 +02:00
co-authored by Claude Opus 5.5
parent e274a8d2b1
commit 9eac4c4d4e
10 changed files with 172 additions and 21 deletions
@@ -12,20 +12,26 @@ use App\Modules\Shared\Domain\ValueObjects\HexColor;
/** /**
* One user's Kanban board settings: card density, the urgent threshold, * One user's Kanban board settings: card density, the urgent threshold,
* and their own colour and order for each column. A column without a * their own colour and order for each column, and which cards they keep
* colour of its own uses the design's default colour. * open (task list shown). A column without a colour of its own uses the
* design's default colour.
*/ */
final readonly class BoardPreferences final readonly class BoardPreferences
{ {
/** At most this many open cards are remembered, the latest kept. */
public const int MAX_EXPANDED_CARDS = 500;
/** /**
* @param array<string, HexColor> $columnColors by BoardColumn value * @param array<string, HexColor> $columnColors by BoardColumn value
* @param array<string, ColumnSort> $columnSorts by BoardColumn value * @param array<string, ColumnSort> $columnSorts by BoardColumn value
* @param list<string> $expandedCards card keys ("job-12", "task-34"), oldest first
*/ */
public function __construct( public function __construct(
public BoardDensity $density = BoardDensity::Normal, public BoardDensity $density = BoardDensity::Normal,
public UrgentThreshold $urgentThreshold = UrgentThreshold::Urgent, public UrgentThreshold $urgentThreshold = UrgentThreshold::Urgent,
public array $columnColors = [], public array $columnColors = [],
public array $columnSorts = [], public array $columnSorts = [],
public array $expandedCards = [],
) {} ) {}
public static function defaults(): self public static function defaults(): self
@@ -33,14 +39,20 @@ final readonly class BoardPreferences
return new self; return new self;
} }
public static function isCardKey(string $key): bool
{
return preg_match('/^(job|task)-\d+$/', $key) === 1;
}
/** /**
* Rebuilds stored settings, dropping unknown columns and invalid values * Rebuilds stored settings, dropping unknown columns and invalid values
* so a bad stored value never breaks the board. * so a bad stored value never breaks the board.
* *
* @param array<array-key, mixed> $colors * @param array<array-key, mixed> $colors
* @param array<array-key, mixed> $sorts * @param array<array-key, mixed> $sorts
* @param array<array-key, mixed> $expanded
*/ */
public static function fromStored(?string $density, ?string $threshold, array $colors, array $sorts): self public static function fromStored(?string $density, ?string $threshold, array $colors, array $sorts, array $expanded = []): self
{ {
$columnColors = []; $columnColors = [];
@@ -60,11 +72,14 @@ final readonly class BoardPreferences
} }
} }
$expandedCards = array_values(array_unique(array_filter($expanded, fn ($key): bool => is_string($key) && self::isCardKey($key))));
return new self( return new self(
BoardDensity::tryFrom((string) $density) ?? BoardDensity::Normal, BoardDensity::tryFrom((string) $density) ?? BoardDensity::Normal,
UrgentThreshold::tryFrom((string) $threshold) ?? UrgentThreshold::Urgent, UrgentThreshold::tryFrom((string) $threshold) ?? UrgentThreshold::Urgent,
$columnColors, $columnColors,
$columnSorts, $columnSorts,
array_slice($expandedCards, -self::MAX_EXPANDED_CARDS),
); );
} }
@@ -83,14 +98,19 @@ final readonly class BoardPreferences
return $this->columnSorts[$column->value] ?? ColumnSort::Manual; return $this->columnSorts[$column->value] ?? ColumnSort::Manual;
} }
public function isCardExpanded(string $key): bool
{
return in_array($key, $this->expandedCards, true);
}
public function withDensity(BoardDensity $density): self public function withDensity(BoardDensity $density): self
{ {
return new self($density, $this->urgentThreshold, $this->columnColors, $this->columnSorts); return new self($density, $this->urgentThreshold, $this->columnColors, $this->columnSorts, $this->expandedCards);
} }
public function withUrgentThreshold(UrgentThreshold $threshold): self public function withUrgentThreshold(UrgentThreshold $threshold): self
{ {
return new self($this->density, $threshold, $this->columnColors, $this->columnSorts); return new self($this->density, $threshold, $this->columnColors, $this->columnSorts, $this->expandedCards);
} }
/** /**
@@ -106,7 +126,7 @@ final readonly class BoardPreferences
$colors[$column->value] = $color; $colors[$column->value] = $color;
} }
return new self($this->density, $this->urgentThreshold, $colors, $this->columnSorts); return new self($this->density, $this->urgentThreshold, $colors, $this->columnSorts, $this->expandedCards);
} }
public function withColumnSort(BoardColumn $column, ColumnSort $sort): self public function withColumnSort(BoardColumn $column, ColumnSort $sort): self
@@ -114,7 +134,26 @@ final readonly class BoardPreferences
$sorts = $this->columnSorts; $sorts = $this->columnSorts;
$sorts[$column->value] = $sort; $sorts[$column->value] = $sort;
return new self($this->density, $this->urgentThreshold, $this->columnColors, $sorts); return new self($this->density, $this->urgentThreshold, $this->columnColors, $sorts, $this->expandedCards);
}
/**
* Opens or closes a card's task list for this user. A card key that is
* not one ("job-12" / "task-34") changes nothing.
*/
public function withCardExpanded(string $key, bool $expanded): self
{
if (! self::isCardKey($key)) {
return $this;
}
$cards = array_values(array_diff($this->expandedCards, [$key]));
if ($expanded) {
$cards[] = $key;
}
return new self($this->density, $this->urgentThreshold, $this->columnColors, $this->columnSorts, array_slice($cards, -self::MAX_EXPANDED_CARDS));
} }
/** /**
@@ -15,9 +15,10 @@ use Illuminate\Database\Eloquent\Model;
* @property string $urgent_threshold * @property string $urgent_threshold
* @property array<string, string>|null $column_colors * @property array<string, string>|null $column_colors
* @property array<string, string>|null $column_sorts * @property array<string, string>|null $column_sorts
* @property list<string>|null $expanded_cards
*/ */
#[Table('job_board_preferences')] #[Table('job_board_preferences')]
#[Fillable(['user_id', 'density', 'urgent_threshold', 'column_colors', 'column_sorts'])] #[Fillable(['user_id', 'density', 'urgent_threshold', 'column_colors', 'column_sorts', 'expanded_cards'])]
class JobBoardPreferenceModel extends Model class JobBoardPreferenceModel extends Model
{ {
/** /**
@@ -29,6 +30,7 @@ class JobBoardPreferenceModel extends Model
'user_id' => 'integer', 'user_id' => 'integer',
'column_colors' => 'array', 'column_colors' => 'array',
'column_sorts' => 'array', 'column_sorts' => 'array',
'expanded_cards' => 'array',
]; ];
} }
} }
@@ -38,7 +38,7 @@ class EloquentBoardRepository implements BoardRepository
return $model === null return $model === null
? BoardPreferences::defaults() ? BoardPreferences::defaults()
: BoardPreferences::fromStored($model->density, $model->urgent_threshold, $model->column_colors ?? [], $model->column_sorts ?? []); : BoardPreferences::fromStored($model->density, $model->urgent_threshold, $model->column_colors ?? [], $model->column_sorts ?? [], $model->expanded_cards ?? []);
} }
public function savePreferences(int $userId, BoardPreferences $preferences): void public function savePreferences(int $userId, BoardPreferences $preferences): void
@@ -48,6 +48,7 @@ class EloquentBoardRepository implements BoardRepository
'urgent_threshold' => $preferences->urgentThreshold->value, 'urgent_threshold' => $preferences->urgentThreshold->value,
'column_colors' => $preferences->colorValues(), 'column_colors' => $preferences->colorValues(),
'column_sorts' => $preferences->sortValues(), 'column_sorts' => $preferences->sortValues(),
'expanded_cards' => $preferences->expandedCards,
]); ]);
} }
} }
@@ -36,6 +36,7 @@ use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed; use Livewire\Attributes\Computed;
use Livewire\Attributes\On; use Livewire\Attributes\On;
use Livewire\Attributes\Renderless;
use Livewire\Attributes\Url; use Livewire\Attributes\Url;
use Livewire\Component; use Livewire\Component;
@@ -391,6 +392,20 @@ new class extends Component
$this->statusMessage = __('jobs.board.customer_color_saved', ['customer' => $card->customerName ?? '']); $this->statusMessage = __('jobs.board.customer_color_saved', ['customer' => $card->customerName ?? '']);
} }
/**
* A card's task list opened or closed: remembered for this user only.
* Renderless, so opening a card does not redraw the board.
*/
#[Renderless]
public function setCardExpanded(string $key, bool $expanded): void
{
if ($this->findCard($key) === null) {
return;
}
$this->savePreferences($this->preferences->withCardExpanded($key, $expanded));
}
public function setColumnColor(string $columnValue, ?string $color): void public function setColumnColor(string $columnValue, ?string $color): void
{ {
$column = BoardColumn::tryFrom($columnValue); $column = BoardColumn::tryFrom($columnValue);
@@ -772,6 +787,7 @@ new class extends Component
@elseif ($card->isOngoing()) @elseif ($card->isOngoing())
<x-jobs.board.ongoing-card <x-jobs.board.ongoing-card
:card="$card" :card="$card"
:expanded="$this->preferences->isCardExpanded($card->key())"
:compact="$compact" :compact="$compact"
:can-move="$canMove" :can-move="$canMove"
:can-color="$canColor" :can-color="$canColor"
@@ -781,6 +797,7 @@ new class extends Component
@else @else
<x-jobs.board.job-card <x-jobs.board.job-card
:card="$card" :card="$card"
:expanded="$this->preferences->isCardExpanded($card->key())"
:compact="$compact" :compact="$compact"
:can-move="$canMove" :can-move="$canMove"
:can-color="$canColor" :can-color="$canColor"
@@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Cards whose task list the user keeps open on the board ("job-12", "task-34").
*/
public function up(): void
{
Schema::table('job_board_preferences', function (Blueprint $table) {
$table->json('expanded_cards')->nullable()->after('column_sorts');
});
}
public function down(): void
{
Schema::table('job_board_preferences', function (Blueprint $table) {
$table->dropColumn('expanded_cards');
});
}
};
@@ -4,7 +4,7 @@
due date, hours and the job's tasks, which open inside the card and can due date, hours and the job's tasks, which open inside the card and can
be marked done. Compact cards have three rows. be marked done. Compact cards have three rows.
--}} --}}
@props(['card', 'compact' => false, 'canMove' => false, 'canColor' => false, 'canCheck' => false]) @props(['card', 'expanded' => false, 'compact' => false, 'canMove' => false, 'canColor' => false, 'canCheck' => false])
@use('App\Modules\Jobs\Presentation\Support\BoardFormat') @use('App\Modules\Jobs\Presentation\Support\BoardFormat')
@@ -15,7 +15,7 @@
<article <article
aria-labelledby="card-{{ $card->key() }}-title" aria-labelledby="card-{{ $card->key() }}-title"
x-data="{ expanded: false }" x-data="{ expanded: {{ $expanded ? 'true' : 'false' }} }"
class="overflow-hidden rounded-xl border border-neutral-300 bg-white shadow-sm transition dark:border-[#36393f] dark:bg-[#282a2e]" class="overflow-hidden rounded-xl border border-neutral-300 bg-white shadow-sm transition dark:border-[#36393f] dark:bg-[#282a2e]"
> >
<x-jobs.board.card-header :card="$card" :can-move="$canMove" :can-color="$canColor" /> <x-jobs.board.card-header :card="$card" :can-move="$canMove" :can-color="$canColor" />
@@ -40,7 +40,8 @@
@if ($total > 0) @if ($total > 0)
<button <button
type="button" type="button"
x-on:click="expanded = ! expanded" x-on:click="expanded = ! expanded; $wire.setCardExpanded('{{ $card->key() }}', expanded)"
aria-expanded="{{ $expanded ? 'true' : 'false' }}"
x-bind:aria-expanded="expanded.toString()" x-bind:aria-expanded="expanded.toString()"
aria-controls="tasks-{{ $card->key() }}" aria-controls="tasks-{{ $card->key() }}"
aria-label="{{ __('jobs.board.tasks_compact', ['done' => $done, 'total' => $total]) }}" aria-label="{{ __('jobs.board.tasks_compact', ['done' => $done, 'total' => $total]) }}"
@@ -78,7 +79,8 @@
<div class="border-t border-neutral-200 pt-1 dark:border-[#36393f]"> <div class="border-t border-neutral-200 pt-1 dark:border-[#36393f]">
<button <button
type="button" type="button"
x-on:click="expanded = ! expanded" x-on:click="expanded = ! expanded; $wire.setCardExpanded('{{ $card->key() }}', expanded)"
aria-expanded="{{ $expanded ? 'true' : 'false' }}"
x-bind:aria-expanded="expanded.toString()" x-bind:aria-expanded="expanded.toString()"
aria-controls="tasks-{{ $card->key() }}" aria-controls="tasks-{{ $card->key() }}"
class="flex min-h-6 w-full items-center justify-between rounded px-1.5 text-[11px] font-semibold text-neutral-800 transition hover:bg-neutral-100 focus-visible:outline-2 focus-visible:outline-primary-600 dark:text-neutral-100 dark:hover:bg-[#32353b] dark:focus-visible:outline-primary-300" class="flex min-h-6 w-full items-center justify-between rounded px-1.5 text-[11px] font-semibold text-neutral-800 transition hover:bg-neutral-100 focus-visible:outline-2 focus-visible:outline-primary-600 dark:text-neutral-100 dark:hover:bg-[#32353b] dark:focus-visible:outline-primary-300"
@@ -91,7 +93,7 @@
@endif @endif
@if ($total > 0) @if ($total > 0)
<div id="tasks-{{ $card->key() }}" x-show="expanded" x-cloak @class(['border-t border-neutral-200 pt-1 dark:border-[#36393f]' => $compact, 'mt-1.5' => ! $compact])> <div id="tasks-{{ $card->key() }}" x-show="expanded" @unless ($expanded) x-cloak @endunless @class(['border-t border-neutral-200 pt-1 dark:border-[#36393f]' => $compact, 'mt-1.5' => ! $compact])>
<x-jobs.board.task-list :card="$card" :can-check="$canCheck" :compact="$compact" /> <x-jobs.board.task-list :card="$card" :can-check="$canCheck" :compact="$compact" />
</div> </div>
@endif @endif
@@ -3,7 +3,7 @@
open task with a one-click "mark done", lifting tasks into the pipeline open task with a one-click "mark done", lifting tasks into the pipeline
(PIN), and all periods and tasks with a quick add. (PIN), and all periods and tasks with a quick add.
--}} --}}
@props(['card', 'compact' => false, 'canMove' => false, 'canColor' => false, 'canCheck' => false, 'canManage' => false]) @props(['card', 'expanded' => false, 'compact' => false, 'canMove' => false, 'canColor' => false, 'canCheck' => false, 'canManage' => false])
@php @php
$total = count($card->tasks); $total = count($card->tasks);
@@ -13,7 +13,7 @@
<article <article
aria-labelledby="card-{{ $card->key() }}-title" aria-labelledby="card-{{ $card->key() }}-title"
x-data="{ expanded: false }" x-data="{ expanded: {{ $expanded ? 'true' : 'false' }} }"
class="overflow-hidden rounded-xl border border-neutral-300 bg-white shadow-sm dark:border-[#36393f] dark:bg-[#282a2e]" class="overflow-hidden rounded-xl border border-neutral-300 bg-white shadow-sm dark:border-[#36393f] dark:bg-[#282a2e]"
> >
<x-jobs.board.card-header :card="$card" :can-move="$canMove" :can-color="$canColor" /> <x-jobs.board.card-header :card="$card" :can-move="$canMove" :can-color="$canColor" />
@@ -37,7 +37,8 @@
@if ($compact) @if ($compact)
<button <button
type="button" type="button"
x-on:click="expanded = ! expanded" x-on:click="expanded = ! expanded; $wire.setCardExpanded('{{ $card->key() }}', expanded)"
aria-expanded="{{ $expanded ? 'true' : 'false' }}"
x-bind:aria-expanded="expanded.toString()" x-bind:aria-expanded="expanded.toString()"
aria-controls="tasks-{{ $card->key() }}" aria-controls="tasks-{{ $card->key() }}"
aria-label="{{ __('jobs.board.all_periods', ['done' => $done, 'total' => $total]) }}" aria-label="{{ __('jobs.board.all_periods', ['done' => $done, 'total' => $total]) }}"
@@ -87,7 +88,8 @@
<div class="border-t border-neutral-200 pt-1 dark:border-[#36393f]"> <div class="border-t border-neutral-200 pt-1 dark:border-[#36393f]">
<button <button
type="button" type="button"
x-on:click="expanded = ! expanded" x-on:click="expanded = ! expanded; $wire.setCardExpanded('{{ $card->key() }}', expanded)"
aria-expanded="{{ $expanded ? 'true' : 'false' }}"
x-bind:aria-expanded="expanded.toString()" x-bind:aria-expanded="expanded.toString()"
aria-controls="tasks-{{ $card->key() }}" aria-controls="tasks-{{ $card->key() }}"
class="flex min-h-6 w-full items-center justify-between rounded px-1.5 text-[11px] font-semibold text-neutral-800 transition hover:bg-neutral-100 focus-visible:outline-2 focus-visible:outline-primary-600 dark:text-neutral-100 dark:hover:bg-[#32353b] dark:focus-visible:outline-primary-300" class="flex min-h-6 w-full items-center justify-between rounded px-1.5 text-[11px] font-semibold text-neutral-800 transition hover:bg-neutral-100 focus-visible:outline-2 focus-visible:outline-primary-600 dark:text-neutral-100 dark:hover:bg-[#32353b] dark:focus-visible:outline-primary-300"
@@ -98,7 +100,7 @@
</div> </div>
@endunless @endunless
<div id="tasks-{{ $card->key() }}" x-show="expanded" x-cloak class="space-y-1.5 border-t border-neutral-200 pt-1 dark:border-[#36393f]"> <div id="tasks-{{ $card->key() }}" x-show="expanded" @unless ($expanded) x-cloak @endunless class="space-y-1.5 border-t border-neutral-200 pt-1 dark:border-[#36393f]">
<x-jobs.board.task-list :card="$card" :can-check="$canCheck" :can-manage="$canManage" ongoing :compact="$compact" /> <x-jobs.board.task-list :card="$card" :can-check="$canCheck" :can-manage="$canManage" ongoing :compact="$compact" />
</div> </div>
</div> </div>
+19
View File
@@ -193,6 +193,25 @@ it('scrolls only the body of the job editor when a task has many comments', func
->assertNoAccessibilityIssues(level: 2); ->assertNoAccessibilityIssues(level: 2);
}); });
it('keeps the task list of a card open when the board is opened again', function () {
$this->job->update(['status' => 'in_production']);
TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'title' => 'Leikkaus']);
$toggle = '[aria-controls="tasks-job-'.$this->job->id.'"]';
$panel = 'document.getElementById("tasks-job-'.$this->job->id.'")';
visit(route('jobs.board'))
->on()->desktop()
->click($toggle)
->assertScript($panel.'.offsetParent !== null', true)
->wait(1);
visit(route('jobs.board'))
->on()->desktop()
->assertAttribute($toggle, 'aria-expanded', 'true')
->assertScript($panel.'.offsetParent !== null', true)
->assertNoAccessibilityIssues(level: 2);
});
it('marks a task done from the task panel of a card', function () { it('marks a task done from the task panel of a card', function () {
$this->job->update(['status' => 'in_production']); $this->job->update(['status' => 'in_production']);
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'title' => 'Leikkaus', 'status' => 'in_progress']); $task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'title' => 'Leikkaus', 'status' => 'in_progress']);
@@ -266,3 +266,23 @@ it('shows a subcontractor only their cards, without moving or changing colours',
expect($theirs->fresh()->status->value)->toBe('order'); expect($theirs->fresh()->status->value)->toBe('order');
}); });
it('remembers for each user which cards have their tasks open', function () {
$job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]);
TaskModel::factory()->create(['customer_job_id' => $job->id, 'title' => 'Vedos']);
Livewire::test('jobs::board')
->assertSeeHtml('aria-controls="tasks-job-'.$job->id.'"')
->assertDontSeeHtml('aria-expanded="true"')
->call('setCardExpanded', 'job-'.$job->id, true)
->call('setCardExpanded', 'job-999', true);
expect(json_decode((string) DB::table('job_board_preferences')->where('user_id', $this->employee->id)->value('expanded_cards')))->toBe(['job-'.$job->id]);
Livewire::test('jobs::board')->assertSeeHtml('aria-expanded="true"');
$colleague = User::factory()->create();
$job->update(['responsible_user_id' => $colleague->id]);
$this->actingAs($colleague);
Livewire::test('jobs::board')->assertDontSeeHtml('aria-expanded="true"');
});
@@ -237,3 +237,27 @@ describe('moving cards', function () {
$this->pin->execute($jobId, $taskId, true); $this->pin->execute($jobId, $taskId, true);
})->throws(TaskCannotBePinned::class); })->throws(TaskCannotBePinned::class);
}); });
describe('open cards', function () {
it('remembers which cards are open', function () {
$preferences = BoardPreferences::defaults()
->withCardExpanded('job-12', true)
->withCardExpanded('task-34', true)
->withCardExpanded('job-12', false)
->withCardExpanded('nonsense', true);
expect($preferences->isCardExpanded('task-34'))->toBeTrue()
->and($preferences->isCardExpanded('job-12'))->toBeFalse()
->and($preferences->expandedCards)->toBe(['task-34']);
});
it('keeps only valid keys and the latest cards from stored settings', function () {
$stored = array_map(fn (int $id): string => 'job-'.$id, range(1, BoardPreferences::MAX_EXPANDED_CARDS + 10));
$preferences = BoardPreferences::fromStored(null, null, [], [], [...$stored, 'drop table', 42, 'job-1']);
expect($preferences->expandedCards)->toHaveCount(BoardPreferences::MAX_EXPANDED_CARDS)
->and($preferences->isCardExpanded('job-1'))->toBeFalse()
->and($preferences->isCardExpanded('job-'.(BoardPreferences::MAX_EXPANDED_CARDS + 10)))->toBeTrue()
->and($preferences->isCardExpanded('drop table'))->toBeFalse();
});
});