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,
* and their own colour and order for each column. A column without a
* colour of its own uses the design's default colour.
* their own colour and order for each column, and which cards they keep
* open (task list shown). A column without a colour of its own uses the
* design's default colour.
*/
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, ColumnSort> $columnSorts by BoardColumn value
* @param list<string> $expandedCards card keys ("job-12", "task-34"), oldest first
*/
public function __construct(
public BoardDensity $density = BoardDensity::Normal,
public UrgentThreshold $urgentThreshold = UrgentThreshold::Urgent,
public array $columnColors = [],
public array $columnSorts = [],
public array $expandedCards = [],
) {}
public static function defaults(): self
@@ -33,14 +39,20 @@ final readonly class BoardPreferences
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
* so a bad stored value never breaks the board.
*
* @param array<array-key, mixed> $colors
* @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 = [];
@@ -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(
BoardDensity::tryFrom((string) $density) ?? BoardDensity::Normal,
UrgentThreshold::tryFrom((string) $threshold) ?? UrgentThreshold::Urgent,
$columnColors,
$columnSorts,
array_slice($expandedCards, -self::MAX_EXPANDED_CARDS),
);
}
@@ -83,14 +98,19 @@ final readonly class BoardPreferences
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
{
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
{
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;
}
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
@@ -114,7 +134,26 @@ final readonly class BoardPreferences
$sorts = $this->columnSorts;
$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 array<string, string>|null $column_colors
* @property array<string, string>|null $column_sorts
* @property list<string>|null $expanded_cards
*/
#[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
{
/**
@@ -29,6 +30,7 @@ class JobBoardPreferenceModel extends Model
'user_id' => 'integer',
'column_colors' => 'array',
'column_sorts' => 'array',
'expanded_cards' => 'array',
];
}
}
@@ -38,7 +38,7 @@ class EloquentBoardRepository implements BoardRepository
return $model === null
? 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
@@ -48,6 +48,7 @@ class EloquentBoardRepository implements BoardRepository
'urgent_threshold' => $preferences->urgentThreshold->value,
'column_colors' => $preferences->colorValues(),
'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 Livewire\Attributes\Computed;
use Livewire\Attributes\On;
use Livewire\Attributes\Renderless;
use Livewire\Attributes\Url;
use Livewire\Component;
@@ -391,6 +392,20 @@ new class extends Component
$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
{
$column = BoardColumn::tryFrom($columnValue);
@@ -772,6 +787,7 @@ new class extends Component
@elseif ($card->isOngoing())
<x-jobs.board.ongoing-card
:card="$card"
:expanded="$this->preferences->isCardExpanded($card->key())"
:compact="$compact"
:can-move="$canMove"
:can-color="$canColor"
@@ -781,6 +797,7 @@ new class extends Component
@else
<x-jobs.board.job-card
:card="$card"
:expanded="$this->preferences->isCardExpanded($card->key())"
:compact="$compact"
:can-move="$canMove"
:can-color="$canColor"